Thursday, September 3, 2009

How to fold code in NetBeans

NetBeans editor has always had several nice features, one of which is the possibility to fold code thus eliminating unnecessary noise when working. Normally you can collapse methods, comments, imports, javadocs, and so on just clicking the + sign on the left (or using the CTRL + KeyPad- combination). To expand the collapsed section you can obviously click on the - or use the CTRL + KeyPad+ combination.

NetBeans also offers you the possibility to arbitrarily collapse contiguous lines of code: all you have to do is select the lines you want to collapse and press ALT + Enter, thus showing available suggestions given the context (in our case
Surround with //<editor-fold defaultstate="collapsed" desc="comment">...)

I also found an interesting post on code folding, so I'd like to add my two cents on the subject.

I personally think that all imports in Java classes should be explicit, as I want to precisely know what I'm importing. If I only see a couple of lines with asterisks I might think everything is fine, but that could be hiding the fact that I'm importing a thousand classes, which is definitely a smell. That said, I think this can be slightly different, say, when using Java persistence (or in similar situations): I surely want to see all "important" imports (nice alliteration, huh?), but I might want to hide persistence related imports. This would lead me to a hybrid hiding strategy, which can be confusing, but would hide unnecessary noise in the code, which is good.

About method collapsing I pretty much agree with Dustin, but every now and then I happen to collapse method when I work, e.g. if I want to see different portions of code at the same time on a narrow screen - otherwise NetBeans has the Clone Document feature that lets you reposition a copy of the code you're working on pretty much anywhere on the screen(s) - and I cannot/don't want to move methods around. Of course there should not be too many methods between the portions of code that I want to inspect. Anyway, I always use this kind of folding as a temporary solution except for "non-important" or standard code, e.g. plain setters and getters in simple beans, unlike Dustin who likes to keep all executable code shown - even if I can see his point.


Mr.IT said...

I'm not able to see the option fold.....

.MOz said...

Which NetBeans version are you using? I think it all started with 6.1 so if you are using an older one you should upgrade (well actually you should upgrade anyway... you don't want to miss all the new features!)

Sangita Sahoo said...
This comment has been removed by the author.
Wright Williams said...

The PMP Certification establishes a common language among project managers and helps each other work within a common framework. Once you have the PMP, you need to consider how you're applying the processes, tools, and techniques to projects. I took a training course for my preparation in and got ready for the exam on day 5!