For large teams working in same code base, enforcing coding rules and conventions can be difficult. Code analysis tools are very helpful to ensure necessary coding conventions are being followed naturally.
The idea behind code analysis is to examine a piece of code against recognized set of rules. These rules include spelling checks, possible null references exceptions, matching of parameter names in method overriding, forbid catching general exceptions, so on and so forth.
Here is a complete list of rules by Microsoft.
There are two types of code analysis tools:
– Static Code Analysis: Examines the code without compiling. Ex: Visual Studio Code Analysis.
– Dynamic Code Analysis: Examines the code while it is being typed. Ex: Resharper
In this article I will be discussing how to apply Visual Studio Code Analysis rules to your project. This works with Visual Studio 2012.
Applying Code Analysis Rules to your project
– Right click on your project and select properties.
– Go to Code Analysis tab
– From drop down menu select a rules from available ruleset. For ex, Microsoft Minimum Recommended Rules.
– This will apply the selected code analysis ruleset to your project.
Running code Analysis
– To run code analysis on demand, select Analyze > Run Code Analysis option.
– Depending upon settings in your ruleset, Error or Warnings will be reported in Code Analysis window.
– Alternatively, you can also enable code analysis upon Build. Right click on Project > Properties and go to Build tab. Select checkbox, Enable code analysis on Build.
– This option will report code analysis errors every time the project is built.
Note that, enabling code analysis can result in tons of errors/ warnings in your code. This does not necessarily mean all are potential bugs. Again, the examination is performed against the set of rules selected in ruleset. There might be a case when you would like to customize the rule set or create a new one.
Creating your own Ruleset
– Right click on project and add new item,
– From project templates select Code analysis ruleset
– Apply basic set of rules such as Microsoft Minimum Recommended settings.
– Check / Uncheck rules as per your requirement.
– Chose whether you want to report anomaly in the code as Error or Warning against each rule.
– Save the change.
– You can now apply your customized ruleset to the project as described above.
Code analysis is a good place to report spell checking errors. It is generally a good idea to enforce all spell checking rules during code analysis. However, you might have certain domain specific nomenclatures in the code that are not in the dictionary. It would be annoying to see code analysis fail for this reason. The workaround this is to maintain a custom dictionary of words that are accepted as valid during code analysis.
Creating and applying Custom Dictionary
– Create and save custom dictionary as an xml file.
<?xml version=”1.0″ encoding= “utf-8”?>
– Right click on your project and select Add Existing Item.
– Navigate to the custom dictionary xml on your disk and Add as a link.
– Right click on the file in Solution Explorer and select Properties.
– Select type as Code analysis dictionary
– From now on, the words described in Custom Dictionary will be considered as valid words during spell check.
Code analysis is a simple yet powerful tool to enforce coding conventions, catching spelling checks and other obvious bad coding practices, I strongly recommend applying code analysis rules to your project which makes your life a whole lot easier and catches nasty bugs early in development process.