One of the advantages of being involved in the NetCat test program is that I get to develop my skills with using the IDE. This involves working through some test cases to check that the software behaves as expected. My task was to work through the Options menu (so maybe this blog post should be titled ‘Things I learnt from the NetCat testing which I should probably have known‘)
Here are 5 things about formatting I discovered….
1. Imports can be arranged very precisely
With formatting you can precisely control if NetBeans adds a single import (ie HashMap) or the whole package (java.util). You can also get it to switch to the package if it contains more than a set number of values.
You can also define the order of the imports and it can include any Java class. In my example, I want our PDF classes first, then java.io, and then any other entries.
The preview window on the right of the dialogue lets you experiment with all the options in all these format menus.
2. Easy naming conventions
Some developers like their variables to have prefixes and suffixes for clarification. NetBeans has a whole menu section for you guys! And a really nice touch is that it will add them to the actual variables and remove them when creating Getters and Setters.
Braces are one of those topics which result in heated debate between programmers. Netbeans lets you choose when braces appear and if they appear on a new line. It also lets you choose whether to have a merged else if or separate out.
The brace selection is also applied when you format the code, so you can correct the mistakes of your less enlightened colleagues – just wait for them to come and thank-you with beaming smiles for redoing all their braces 😉
The final frontier…
Adding consistent spacing you your code makes it much easier to read but it can be fiddly to add and consistently enforce. So I get NetBeans to worry about it. All the formatting and any auto-completion will use your settings to it is very easy to have consistent spacing across all your code with minimal effort.
I aways start my java classes with the best of intentions about having a neat consistent structure. Then I tweak it,other people edit it,stuff happens… NetBeans includes an option to order the elements according to your preferences (and again it can apply it to already written code when you reformat it).
I hope this article has given you some ideas on the power and flexibility that lies under the hood in NetBeans and that you will dig deeper to see what options you can use to save time and write better code. Do you have any favourite formatting tricks?
This post is part of our “NetBeans article Index” series. In these articles, we aim to explore NetBeans in different ways, from useful hint and tips, to our how-to’s, experiences and usage of the NetBeans IDE.