5 Java Formatting options in NetBeans you probably missed

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

NetBeans imports menu

 

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.

Naming

3. Braces

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 😉

braces

4. Space

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.

spacing

5. Order

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).

Order

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.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (TwitterFacebook and Google+) or the  Blog RSS.

Related Posts:

The following two tabs change content below.

Mark Stephens

System Architect and Lead Developer at IDRSolutions
Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.
Markee174

About Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>