Rule-based Static Analysis Tools for Improving Code Quality

Rule-based static analysis tools check if a program violates a predefined set of rules. They can be used to detect questionable design or implementation decisions. In this course, we will be using such tools to evaluate the quality of your project's code. For Java we will use the tool PMD, and for C# we will use the tool integrated in Visual Studio.

Tool installation

Java: PMD is accessible in many modern Java IDEs as a plugin. You can find detailed information about this on the tool website.

C#: The code analysis tool is (only) available in the Premium or Ultimate versions of Visual Studio 2010/2012. You can get a copy of either one via ETH MSDNAA.

Set of active rules for grading

We have decided on the set of rules we will use when grading the projects, one for PMD and one for Visual Studio. The rules fall into two categories: error (called "blocker" in the case of PMD) and warning, to reflect the severity of the corresponding violations. Errors represent severe design/implementation flaws; Warnings represent less severe flaws.

When grading the projects, we will deduct point(s) from your grade for each Error in the code, and for having too many Warnings. We strongly suggest you to use the tools together with the provided rules during your development to ensure a good quality of your code.

Configuring the tools to use the grading-ruleset

PMD

To only use the provided set of rules for analysis, you need to 1) remove all the existing rules in the tool, and then 2) import the downloaded ruleset. Note that the ruleset's file extension name should be ".xml", otherwise the import operation may fail.

For example, if you use the PMD plugin for Eclipse, to import the ruleset you

1) go to Windows --> Preferences --> PMD --> Rule Configuration,

2) select (by highlighting) all the rules in the list and click the "Remove rule" button to the right of the list,

PMD configuration in Eclipse

3) click the "Import rule set ..." button, then, in the pop-up dialogue, browse to and open the downloaded ruleset file (the rules defined in the file will automatically be listed in the dialogue window). Next, select (by checking) all the rules from the file and click on "OK" to finish the importing.

Importing ruleset into PMD

Finally, you can activate all the rules in the Rule Configuration window and PMD is ready to use.

Visual Studio

Code analysis is configured on a project basis. Go to Project Properties -> Code Analysis. Then select "Open" and specify a path to the ruleset we provided. You can also configure the tool to run an analysis as part of your build process, but that's optional.
Here is an example of Settings page:

Code Analysis in project

To run a code analysis, select "Run Code Analysis" in the project's context menu.