Fluent API and Code Formatting in NetBeans IDE
Join the DZone community and get the full member experience.
Join For FreeConsider for example a piece of code using Dynamic Jasper:
DynamicReport dr = builder .setPageSizeAndOrientation(Page.Page_A4_Landscape()) .setDefaultStyles(null, null, headerStyle, detailStyle) .setUseFullPageWidth(true) .build();
It won't do much, but demonstrates the enhanced readability of fluent APIs. Unfortunately, if you press old trusty Alt+Shift+F in the IDE, it will reformat the above to:
DynamicReport dr = builder.setPageSizeAndOrientation(Page.Page_A4_Landscape()).setDefaultStyles(null, null, headerStyle, detailStyle).setUseFullPageWidth(true).build();
Not so clear anymore, right? This is exactly the point of Wilfred Springer in an open letter to Geertjan Wielenga, justifying why he remains loyal to Eclipse. Wilfred's letter contains an excellent example of the usefulness of the fluent API in which form enhances function with indentation. While he will likely remain with Eclipse after this post (my solution doesn't work with the intricate indentation of his example), I have a simple solution for simple cases (look mom, no annotations):
DynamicReport dr = builder// .setPageSizeAndOrientation(Page.Page_A4_Landscape())// .setDefaultStyles(null, null, headerStyle, detailStyle)// .setUseFullPageWidth(true)// .build();
Simply add // at the end of each line, and NetBeans IDE will honour your code structure. This is certainly not ground-breaking stuff, but should be handy to people like me, who only discovered it today.
Opinions expressed by DZone contributors are their own.
Trending
-
How to Submit a Post to DZone
-
Avoiding Pitfalls With Java Optional: Common Mistakes and How To Fix Them [Video]
-
Extending Java APIs: Add Missing Features Without the Hassle
-
Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers
Comments