---
title: Static Code Analysis For Java Using Eclipse
canonical: https://0110.be/posts/Static_Code_Analysis_For_Java_Using_Eclipse
markdown_url: https://0110.be/posts/Static_Code_Analysis_For_Java_Using_Eclipse.md
id: 358
published_at: '2010-06-03T00:00:00Z'
updated_at: '2020-11-17T09:04:00Z'
author: Joren
tags:
- name: Code
  markdown_url: https://0110.be/tags/Code.md
- name: HoGent
  markdown_url: https://0110.be/tags/HoGent.md
- name: Java
  markdown_url: https://0110.be/tags/Java.md
- name: featured
  markdown_url: https://0110.be/tags/featured.md
---

# Static Code Analysis For Java Using Eclipse

This post is about the tools I use to keep the source code of [Tarsos](http://0110.be) reasonably clean, consistent and readable. Static code analysis can be of great help if you want to maintain strict coding standards and follow language idioms. Some of the [patterns they can detect for you](http://pmd.sourceforge.net/):

-   Dead code - unused variables, parameters, methods

-   Suboptimal code - wasteful resource usage

-   Overcomplicated expressions - unnecessary if statements, for loops that could be while loops

-   Duplicate code - copied/pasted code is a code smell.

-   Formatting inconsistencies, e.g. variable modifier order

And even more subtle, but equally important:

-   Resource management: is a resource handled (closed) correctly on all possible code paths?

-   Abstraction level: is it needed to expose the concrete type of an object or could an (abstract) supertype or even an interface be used instead?

-   ...

In a previous life I used .NET and the static code analysis tools FxCop & StyleCop. FxCop operates on bytecode (or intermediate language in .NET parlance) level, StyleCop analyses the source code itself. Tarsos uses JAVA so I looked for JAVA alternatives and found a few.

-   PMD & Checkstyle both operate on source code level.

-   FindBugs operates on bytecode level.

On freesoftwaremagazine.com there is an [article series on JAVA static code analysis software](http://www.freesoftwaremagazine.com/columns/destroy_annoying_bugs_part_1). It covers PMD and FixBugs and integration in Eclipse. It does not cover Checkstyle. Checkstyle is essentialy the same as PMD but it is better integrated in eclipse: it checks code on save and uses the standard 'Problems' interface, PMD does not.

To fix problems *Eclipse save actions* can save you some time. IBM has an article on [how to keep your code clean using Eclipse](http://www.ibm.com/developerworks/library/os-eclipse-clean/).

Continuous testing is also a really nice thing to have: detecting unexpected behavior while refactoring/programming can prevent unnecessary bug hunts. A [video about immediate feedback using continuous testing](http://improvingworks.com/products/infinitest/) makes this clear.

Another tip is a more philosophical one: making your code and code revisions publicly available makes you think twice before implementing (and subsequently publishing) a quick and dirty hack. Tarsos is available on github.

## References

-   [FxCop](http://msdn.microsoft.com/en-us/library/bb429476(VS.80).aspx) & [StyleCop](http://code.msdn.microsoft.com/sourceanalysis)

-   [Checkstyle](http://checkstyle.sourceforge.net/)

-   [Find bugs](http://findbugs.sourceforge.net/)

-   [PDM](http://pmd.sourceforge.net/)

-   [Infinitest](http://improvingworks.com/products/infinitest/)

-   [Github](http://github.com)


![PMD](https://0110.be/files/photos/358/pmd.png)

![Checkstyle](https://0110.be/files/photos/358/checkstyle.png)
