Extended code folding in Eclipse
The problem
“I program Java. I use Eclipse to do it. I like concise code. I work with open-source projects. Sometimes I need to read someone else’s code. Sometimes (more so than not) people write ugly code or hard-to-read code at least. When I say “ugly” and “hard-to-read” I usually think of long methods.”
Is there a solution?
If you fit the above description then you might want to use code folding. If you had a fair share of navigating through code, you probably already know about it. In a nutshell, code folding is an IDE feature which allows you to collapse (and later expand) a block of code that you do not want to look at all the times, thus leaving more screen real-estate for looking at code that matter to you at a given moment. Sounds useful? Well, it is!
However, you may not be happy with the code folding support bundled with Eclipse since it works with (nested) classes, methods but does not work with control flow structures (if, while, for, try-catch-finally, etc.). This full-blown code folding support that includes any code block including control flow structures I call extended code folding. You may have seen NetBeans and other IDEs sporting this feature and you wished you had it in Eclipse, your favorite IDE.

Solution indeed
Well, there’s a solution for this missing feature in the form of Eclipse plugin called Coffee Bytes Code Folding over at RealJenius.com. All you need to do is add the following update site:
http://eclipse.realjenius.com/update-siteOnce installed go Preferences->Java->Editor->Folding and in Select folding to use drop-down menu choose newly installed Coffee Bytes Java Folding. This plugin, just as Eclipse’s built-in code folding support, allows you to see the folded code by hovering your mouse over a plus sign. While I’m there, let me suggest you to turn on line numbers via Preferences->General->Editors->Text Editors->Show line numbers as it will make it easier to not only recognize folded code sections but also to tell what line are you looking at without actually having to click at the precise line in code and read the number from status bar. This is good for linking compiler/run-time errors and what line your colleague is talking about with less effort in exchange for some 20-30 pixels. I also recommend using Modern icons for plus/minus folding handles so that you can easily distinguish whether you’re using extended or built-in code folding.
After using this plugin for a couple of minutes, I’ve noticed one thing I didn’t like. When a code block is folded, braces (a.k.a. curve brackets) are not hidden as they are with the built-in Eclipse code folder. Namely, the closing brace remains standing in a separate line thus needlessly wasting one line without providing any meaningful information.
Shortcuts
Shortcuts are your friends as they reduce the distance your mouse (and your hands) need to move (and programmers should do everything they can to avoid RSI). Also, each shortcut you learn improves your ninja skills. You may want to learn them:- Ctrl + <Numpad Plus> - Expand a single node
- Ctrl + <Numpad Minus> - Collapse a single node
- Ctrl + <Numpad Multiply> - Expand (toggle) all nodes
- Ctrl + <Numpad Divide> - Toggle folding (turn code folding on and off)
- Ctrl + Shift + <Numpad Multiply> - Reset structure
- Ctrl + Shift + <Numpad Divide> - Collapse all
Final thoughts
This plugin will also allow you to define custom regions (as Microsoft Visual Studio .NET has had since first version), which Jeff Atwood (of Coding Horror and StackOverflow fame) calls glorified comments with which I fully agree. Having that in mind and understanding that “folding is used to sweep code under the rug” in mind, you should really use code folding ONLY on other people’s code that you have no control over. At the same time, you should make sure that you do not fold your own code as you may be led to believe your code is easy to read and you’ll end up creating horribly long methods and code blocks that are hard to read. Well it will be hard to read for people not using code folding, which are many. After all, nobody should be force-fed with a bunch of crutches plugins anyway. :-)
Conclusion (and a pattern)
So what’s the takeaway here? It’s a pattern to keep in mind:
If code requires code folding, that code must be bad.
Why do I say this? Because folded code hides what’s inside thus violating Separation of concerns principle since folding hiding code may lead you to squeeze all kinds of logic inside a single folded line.
