
TIRED reviewing the C# code in my current Airlines Project....
As the blog name says... Yes.., I am a little lazy :)
And to my luck, I heard about the Microsoft Stylecop (from my school friend) which would solve this purpose . So, gave a try !!
And yes... I found it very useful, as it lessened the burden of reviewing the code. Thanks to Microsoft! :)
Let me explain you, what I have learnt about this Stylecop!
What is Stylecop?
- Style cop is the source anlysis for C# code
- It enforces a set of style and consistency rules on the code, and reports the code which is not following the rule.
- It can be run from inside of Visual Studio or integrated into an MSBuild project.
Goal of StyleCop:
- To allow you to produce elegant, consistent code that your team members and others who view your code will find highly readable.
- Style cop takes a one-size-fits-all approach to code style, layout, and readability rules.
Difference between the Style cop and Fxcop:
- StyleCop is similar in many ways to Microsoft Code Analysis (specifically FxCop).
- FxCop performs its analysis on compiled binaries, while StyleCop analyzes the source code directly.
- For this reason, FxCop focuses more on the design of the code, while StyleCop focuses on layout, readability and documentation.
The Rule:
StyleCop comes with a set of default rules analyzers covering approximately 200 best practice rules. These rules are full compatible with the default layout settings in Visual Studio 2005 and Visual Studio 2008.
They are:
- Layout of elements, statements, expressions, and query clauses
- Placement of curly brackets, parenthesis, square brackets, etc
- Spacing around keywords and operator symbols
- Line spacing
- Placement of method parameters within method declarations or method calls
- Standard ordering of elements within a class
- Formatting of documentation within element headers and file headers
- Naming of elements, fields and variables
- Use of the built-in types
- Use of access modifiers
- Allowed contents of files
- Debugging text
Download:
Just Install the Stylecop from here. By default, this installs in the location : C:\Program Files\Microsoft StyleCop and automatically gets integrates with the Visual Studio.
a Sample:
Let me, for better understanding create a simple console application and use StyleCop to analyze the source. This killer app just initializes a class and prints out few properties.
Let me, for better understanding create a simple console application and use StyleCop to analyze the source. This killer app just initializes 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 categorized 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. And I suggest you to give a try!
No comments:
Post a Comment