Style

  • Define Code Conventions (Naming, spaces vs. tabs, etc.)

  • Re-use existing conventions!

  • Follow them!

  • Use tools to check them

Maven use:

  • Put in <reporting> section

Checkstyle

Example 5. Example Checkstyle Maven Configuration
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <configLocation>checkstyle.xml</configLocation>
        </configuration>
      </plugin>

An example checkstyle.xml can be found at: http://wmsx.googlecode.com/hg/build-tools/src/main/resources/wmsx/checkstyle.xml

Checkstyle 4 vs 5

  • The checkstyle maven plugin until version 2.3 is based on checkstyle 4.

  • Current eclipse checkstyle plugins is based on checkstyle 5

  • both are incompatible (slight changes in checkstyle.xml)

Fix: Use newer checkstyle plugin. Problem: Not officially available yet! Must set plugin location!

Example 6. Example Checkstyle 5 Maven Configuration
...reporting...plugins...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>2.5-SNAPSHOT</version>
        <configuration>
          <configLocation>checkstyle.xml</configLocation>
        </configuration>
      </plugin>
...
  <pluginRepositories>
    <pluginRepository>
      <id>apache-snapshots</id>
      <name>Apache Snapshot repository</name>
      <url>http://repository.apache.org/snapshots</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </pluginRepository>
  </pluginRepositories>

PMD

Common Programming checks for code style.

Yes, all the standard rules make sense!

Plugins available for Eclipse, Netbeans, Maven

Example 7. PMD Configuration for Maven
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <linkXref>true</linkXref>
          <targetJdk>1.5</targetJdk>
          <sourceEncoding>utf-8</sourceEncoding>
          <rulesets>
            <ruleset>/rulesets/basic.xml</ruleset>
            <ruleset>/rulesets/braces.xml</ruleset>
            <ruleset>/rulesets/codesize.xml</ruleset>
            <ruleset>/rulesets/clone.xml</ruleset>
            <ruleset>/rulesets/design.xml</ruleset>
            <ruleset>/rulesets/finalizers.xml</ruleset>
            <ruleset>/rulesets/imports.xml</ruleset>
            <ruleset>/rulesets/strings.xml</ruleset>
            <ruleset>/rulesets/migrating_to_15.xml</ruleset>
            <ruleset>/rulesets/optimizations.xml</ruleset>
            <ruleset>/rulesets/sunsecure.xml</ruleset>
            <ruleset>/rulesets/unusedcode.xml</ruleset>
          </rulesets>
        </configuration>
      </plugin>

Findbugs

Example 8. Findbugs Maven configuration
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>findbugs-maven-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <threshold>Low</threshold>
          <effort>Max</effort>
          <omitVisitors>FindDeadLocalStores</omitVisitors>
        </configuration>
      </plugin>

NCSS

  • Non Commented Source Statements

  • Simplified: The number of ;

  • Better comparable than LOC (lines of code)

Example 9. NCSS Maven configuration
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>javancss-maven-plugin</artifactId>
        <version>2.0-beta-2</version>
      </plugin>
    </plugins>
  </reporting>