Home / Programming / Blog article: StyleCop Tutorial

| RSS

StyleCop Tutorial

August 7th, 2008 | 11 Comments | Posted in Programming

StyleCop

StyleCop is a source analysis tool for C#. It can be used for analysing source code as opposed to compiled assemblies which is the area for FxCop. StyleCop is currently in version 4.2 and can be downloaded here. In this tutorial I will show you how to use StyleCop.

I will create a simple console application and use StyleCop to analyse the source.  This killer app just initialises a class and prints out few properties. Below is the code for the application.

public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public CustomerStatus Status { get; set; }
    public override string ToString()
    {
        string result = string.Format
            ("First Name = {0}\nLast Name = {1}\nAddress = {2}\nStatus = {3}"
            , FirstName, LastName, Address, Status);
        return result;
    }
}
public enum CustomerStatus
{
    Active,
    InActive
}
class Program
{
    static void Main(string[] args)
    {
        Customer customer = new Customer
        {
            FirstName = "John",
            LastName = "Smith",
            Address = "1 George Street, Sydney",
            Status = CustomerStatus.Active
        };
        Console.WriteLine(customer.ToString());
        Console.ReadKey();
    }
}

I can run source code analysis on the entire project via Tools menu or an individual file by right clicking on the class file.

image

By running source analysis on Program.cs I get 34 violations.

image

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.

By default StyleCop is installed at C:\Program Files\Microsoft Source Analysis Tool for C#. A quick glance over the contents of this folder look like this.

image

Notice the second last file which is Settings.SourceAnalysis. Double clicking it opens up the SourceAnalysisSettingsEditor where I can select/deselect the rules.

image

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.

image

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.

Conclusion

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. Lately StyleCop has had its own share of controversy, but it is good that Microsoft is paying attention to this nifty tool which could attract a significant user base. StyleCop can be downloaded here.

kick it on DotNetKicks.com

Technorati Tags: ,

Leave a Reply 19576 views, 1 so far today |
Tags: ,
Follow Discussion

11 Responses to “StyleCop Tutorial”

  1. Tr3v Says:

    I also found it useful to disable “Analyze designer files”

  2. Deepak Says:

    I agree. Analysing designer files can be a nuisance.

  3. harry richter Says:

    Where is the VB.Net version!!! :-(((

    Soon please!

    Regards

    Harry

  4. Mark Hildreth Says:

    My installation was in a different directory:

    C:\Program Files\Microsoft StyleCop 4.3

  5. Mike Says:

    How can I disable the SA1400 just for the static void Main() Entry point?

  6. Deepak Says:

    Mike,

    I would imagine that you should be able to put an attribute on a method to disable a StyleCop rule. But this does not seem to be the case. Have a look at this link:

    http://code.msdn.microsoft.com/sourceanalysis/WorkItem/View.aspx?WorkItemId=25

    There is a comment from someone at Microsoft

    “There is no plan now to allow this but we will leave the issue open for consideration. StyleCop rules are much more deterministic than FxCop and it is currently by design that suppressions are not allowed.”

    I think ability to suppress a rule using attributes is much desirable but I guess we will have to wait for a while.

  7. Deepak Says:

    Hi Deepak,
    Thank you. So now i know im not alone with this “problem”

  8. sandrar Says:

    Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

Trackbacks

  1. Links zum Wochenende - Ausgabe 4 - 12.09.2008 | DotNetBlog der DotNet-Blog - von Sascha Baumann  
  2. Enforcing coding standards with Microsoft StyleCop « Thoughtology  
  3. Dont’ install StyleCope 4.3, StyleCope 4.4 avaliable now « Mohamed Radwan's Blog  

Leave a Reply





Switch to our mobile site