Code Analyzers

During my search for Profilers I stumbled upon some good Java code checkers or Code Analyzers and though most of you may be already aware about them would like to share my thoughts on the same. Please feel free to correct me or add your inputs to this.

Talking of Code Review or Analysis I wonder if somebody would even think about going for the wearisome task of manually locating lapses in use of Proper Syntax, code indentation, unused variables/imports, naming convention and Javadoc comments without using any automated tool to do the task. Apart from the task of deciding about what code optimizations need be done or detecting any logical blunder rest can perhaps be safely left to these tools. Moreover more ambitious programmers can even look for these tools to operate in tight integration with their favorite IDEs. Fortunately my favorite Netbeans has many modules already available of which three are my personal favorites : PMD, Jalopy and Checkstyle, perhaps in that order.

All the three have various plugins available for various IDEs. Jalopy is a code formatter and beautifier and is highly configurable using code snippets though it does not comprehend well line spacing and tabs for files edited by other IDEs say Kawa or Textpad. Checkstyle is more about code formatting and nit-pickingly looks for white spaces, tabs, position of braces and javadocs and configuration is difficult (as far as I know certainly not configurable through Netbeans GUI). Out of these PMD deserves my high regard, here is a synopsis of what it can detect:

  • Unused local variables, parameters and private methods (very useful)
  • Empty catch blocks
  • Empty ‘if’ statements
  • Duplicate import statements
  • Classes which could be Singletons
  • Short/long variable and method names

A lesser known but very powerful code checking tool is FindBugs. Too bad it has no plugin available for Netbeans though. While the conclusions of the tool about unread fields is not very accurate, it detects what many others don’t such as:

  • Null pointer de-reference detector
  • Ignored returns from method
  • Unclosed I/O streams
  • Objecting usage of == or != for String comparison

Here are some other tools:

  • SourceMonitor: Provides size and complexity metrics for your source code.
  • JLint: A Code verifier
  • QStudio also seems pretty good to me. As per their site it is available for download with a year license at no cost.

P.S.: This post finds mention here, here, here and here.

By Debashish Chakrabarty

Debashish Chakrabarty, a Software Consultant based at Pune, has been on the Internet since the “geocities” days but opened his Blogger account in October 2002. Apart from this blog he also maintains a Hindi blog NuktaChini.

9 replies on “Code Analyzers”

Im writing a simple program in Java to read from MSAccess and transfer the same to Excel sheets.
MSAccess database contains huge “text” as one of its fields. and After transer, im getting some junk characters in output wherever there is a newline orcarriage return. can anyone tell me how to eliminate those junk characters from my output file.


[Trackback] Code Analyzers

Jalopy is a source code formatter for the Sun Java programming language.

PMD scans Java source code and looks for potential problems…

Checkstyle is a development tool to help programmers write Java code that adheres to a codin…

You might want to try out IDEA (IDE from JetBrains) as it has most of the functionality you describe built in.

This one costs money but has an extensive range of audits and metrics which are configurable

Also, Eclipse is quite nice at detecting and highlighting common problems as mentioned above you edit, such as unused local variables, parameters and private methods, empty catch blocks

Dependency Finder is an open source tool for analyzing compiled Java and track inter-dependencies down to the method level. It can also show aggregated views at class and package level, and pinpoint specific items with regular expressions. The toolkit includes an API diff tool, for comparing succesive versions, and an embryonic OO metrics tool. It may not be as fully integrated with IDEs as the commercial competition, but it’s FREE.

Thanks for your input guys. Few more tools (not free though) are JStyle and JProbe but the latter demands a standalone application or atleast one class in the package that has main method. For testing your custom API classes or for testing individual classes I think a combination of PMD + CheckStyle + Findbugs would be all you need. JStyle is also cool and comes with a detailed help document that explains each coment with code examples.

JLint is *really good* for finding dodgy code.
It can identify potential synchronization deadlocks from source files as well as picking up a whole mountain of potential problems that the human eye just wouldn’t see.

The CVS version of checkstyle also has many metrics such as cyclomatic complexity, etc. There is also a tool called simian a colleage of mine showed me which checks for copy and pasted code. PMD also has a tool called CPD for finding copy and pasted code.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.