"Just How Messed Up Is My Code?"
By Geertjan on Jul 25, 2013
More often than not, you know your code is pretty messed up. The excuse you have is that you inherited it from someone else. That, of course, doesn't change the fact that your code is messed up. It changes how responsible you feel about it but, as you'll soon be reminded by someone, the fact that you now own the code means that it's your responsibility now. The fact that your code works doesn't mean that you understand it and that's because the code is messed up in numerous different and interesting ways. The fact that you don't understand how it works means that it's hard to maintain it.
So, wouldn't it be handy to know how many messy methods you have, and where they are found in your application, that is, where exactly are the worst culprits, those that are just really wildly long with multiple nested clauses and loops and return statements everywhere? And what about all those classes with bizarre numbers of constructors? Or the constructors where dozens of parameters are received or classes that are hopelessly intertwined with other classes? Then at least you'll know where the main problems are found.
Enter NetBeans code metrics, introduced in NetBeans IDE 7.4 which, at the time of writing, is in its Beta phase. Go to Source | Inspect in the main menubar of NetBeans IDE. Choose a "Scope", i.e., do you want to search for messed up code in the current file, package, project, or all open projects? Then, in the Configuration drop-down, skip over the predefined configurations and choose "Default" at the end. Now the "Manage" button becomes enabled; click it. In the Configurations drop-down, select New, and name the configuration, "Java Code Metrics", under Analyzer choose "NetBeans Java Hints", and then check "Java Code Metrics". Take a look at the long list of inspections available for code metrics, all introduced in NetBeans IDE 7.4. Each of them can be configured, i.e., you can decide what "too much" and "too many" means:
Click OK. Now you'll have a new "Java Code Metrics" configuration. Click Inspect. Now, within the specified scope, all the messed up code is identified:
Click on an item in the view above to jump to the offending code. Then, fix it! Yes, fixing messed up code is a manual process. Hey, NetBeans IDE can't do everything for you...
Note: I initially tried the above in NetBeans IDE 7.4 Beta, and it didn't return results, though when I tried it in the latest development build, the results above were shown. So it appears that problems with this functionality have been fixed since the Beta was released, therefore download the latest development build if you want to play with this functionality. Your feedback and experiences with this would be great to hear about.