[VS2010] Introduction to StyleCop



StyleCop is a tool that analyzes C source code # and applies a set of style and consistency rules. You can run from within Visual Studio or integrated into a project of TeamBuild. It is quite useful when you want to ensure that the “aesthetic of your code” is consistent among themselves as well as having a good design of the architecture of your solution, respect the rules of design SOLID. This post is based on the 4.5RC version which can be downloaded from here.

A detail to keep in mind is that during the installation of the product, you can select the integration with Visual Studio 2010and also if you want the necessary files for MSBuild to add validations of StyleCop for compilations of Team Build.


For this example, I selected the 2 options.

We are on the first example of code that let us see what says StyleCop thereon. If we create a class library project with your class by default we find the following code:

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   6: namespace StyleCopDemo01
   7: {
   8:     public class Class1
   9:     {
  10:     }
  11: }

And StyleCop tells us with 6 Warnings of the following details to take into account:


If we “move” the using within the Namespace, as indicated by the recent warnings, the code is in the following way:

   1: namespace StyleCopDemo01
   2: {
   3:     using System;
   4:     using System.Collections.Generic;
   5:     using System.Linq;
   6:     using System.Text;
   8:     public class Class1
   9:     {
  10:     }
  11: }

And actually, now we just have 2 warnings for those who worry about:


Add a bit of code over the example:

   1: public string Foo(int firstArgument, int secondArgument)
   2: {
   3:     var res = "no";
   4:     if (firstArgument == secondArgument)
   5:     {
   6:         res = "yes";
   7:     }
   8:     if (firstArgument > secondArgument)
   9:     {res = "maybe";}
  10:     return res;
  11: }

And now we’ll see how we have to worry about the fifth after the keys, the spaces between the elements between the keys, etc.


By default, StyleCop has enabled all the rules that have, which can be quite annoying; in particular with regard to the comments, as it not only recommend add tags in comments in all classes, but it also recommends headers in classes, sections of copyright, etc.

If you want to configure the set of rules with which we work, we can do so from the project in question. Select the same, deploy the contextual menu and select the option [StyleCop Settings]


In this form, we will see the different types of rules that apply during the analysis of StyleCop. My recommendation > disable those of documentation Open-mouthed smile


In the coming posts a little more about StyleCop and Team Build.

Greetings @ Here

The Bruno


One thought on “[VS2010] Introduction to StyleCop

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.