StyleCOP

 

What is it

 

It is a source analysis tool for C#. It can be used for analyzing source code as opposed to compiled assemblies which is the area for FxCop. StyleCop analyzes C# code with a set of rules to check whether the code follows the rules. It is a Microsoft tool (run from inside of Visual Studio or integrated into an MSBuild project).

 

Where to find

 

It can be downloaded from http://stylecop.codeplex.com/

 

Why to use

 

StyleCop is a light weight source code analysis tool which fills in the necessary gap. One of the benefits of StyleCop can be realised while conducting code reviews.

 

How to use

 

If you click on the downloads section, you will see three setups, one is the StyleCop setup which actually installs the software and the other two are CHM help files. One help file talks about the rules and the other about the API exposed by StyleCop.

Once you install StyleCop, nothing will be seen as in start menu. When you open any project of C# and right click on the project, you should see two menus in the project as shown in the figure below – one which helps us select the rules, i.e. the Stylecop settings menu and the other which runs these rules on the C# project.

 

If you just run the stylecop, you should see broken rules in output window as shown in the figure below:

 

 

Depending on your style or your coding standards, you will either agree or disagree with detected violations. For example in the result above I do not agree with rule SA1200 “All using directives must be placed inside of the namespace”. I have never declared my using statements inside the namespace and I do not intend to do so in future. So what can I do? I can easily modify the rule set. Let’s see how we can do this.

Right click the project and go to StyleCop Settings.

I can turn off rule SA1200 “All using directives must be placed inside of the namespace” and then if I run analysis again, my result set will not show it as a rule violation.

 

As you can see from screenshots above that rules are categorised into different categories such as Documentation Rules, Layout Rules, Naming Rules etc. This makes is easy work with rules in a logical way.