|
StyleCop Tutorial
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.

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

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.

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

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.
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.
9 Responses to “StyleCop Tutorial”
Trackbacks
- Links zum Wochenende - Ausgabe 4 - 12.09.2008 | DotNetBlog der DotNet-Blog - von Sascha Baumann September 12th, 2008
- Enforcing coding standards with Microsoft StyleCop « Thoughtology March 5th, 2009
Leave a Reply
Popular Post
Tag Cloud
Code Snippets
- Get Current Windows User In C#
- Get Width And Height Of Image In C#
- Get Windows Registry Size With WMI And C#
- Reverse Array Elements Using C#
- Convert Hexadecimal To Number In C#
- Get Free Disk Space Using T-SQL
- SQL Server 2008 – Get All Indexes In A Database
- Get Name Of Current Executing Assembly In C#
- Get CD Or DVD Drive Information Using WMI And C#
- Get Last Row From Table Using LINQ To SQL



August 7th, 2008 at 9:16 pm
I also found it useful to disable “Analyze designer files”
August 7th, 2008 at 9:22 pm
I agree. Analysing designer files can be a nuisance.
August 19th, 2008 at 11:44 pm
Where is the VB.Net version!!! :-(((
Soon please!
Regards
Harry
September 9th, 2008 at 12:00 am
My installation was in a different directory:
C:\Program Files\Microsoft StyleCop 4.3
September 11th, 2008 at 1:38 am
How can I disable the SA1400 just for the static void Main() Entry point?
September 11th, 2008 at 10:51 am
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.
September 12th, 2008 at 12:16 am
Hi Deepak,
Thank you. So now i know im not alone with this “problem”