- C#
- Xamarin
When you develop a project you probably work within a team. Each developer has his own programming habits. To avoid having a code that looks different in each class we need to find a way to harmonize it.
StyleCop
is a C# source code analyzer that allows you to enforce a set of style and consistency rules. You can adapt the rules that you don’t want to check depending on your needs. This kind of tools helps you to have a code:
To add StyleCop
to your project you have different possibilities:
StyleCop.MSBuild
NugetI will focus this tutorial on the first solution, the advantages of this one are:
StyleCop
configuration with your projectFirst of all add the StyleCop.MSBuild
Nuget package to the project you want to analyse.
Now, for example let’s create a new class called MyViewModel.cs
here is what is basically generated:
using System;
namespace StyleCopDemo
{
public class MyViewModel
{
public MyViewModel()
{
}
}
}
If you compile your project you will immediately see a lot of warnings in this class:
Let’s add a new file called Settings.StyleCop
to your project, this one will help you to configure and adjust the rules you want or not in your project.
Let’s add a default configuration:
<StyleCopSettings Version="4.3">
<GlobalSettings>
<StringProperty Name="MergeSettingsFiles">NoMerge</StringProperty>
<StringProperty Name="Culture">en-US</StringProperty>
<CollectionProperty Name="RecognizedWords">
<Value>Dll</Value>
</CollectionProperty>
</GlobalSettings>
<Parsers>
<Parser ParserId="StyleCop.CSharp.CsParser">
<ParserSettings>
<BooleanProperty Name="AnalyzeDesignerFiles">False</BooleanProperty>
</ParserSettings>
</Parser>
</Parsers>
<Analyzers>
<Analyzer AnalyzerId="StyleCop.CSharp.DocumentationRules">
<Rules>
</Rules>
<AnalyzerSettings>
<StringProperty Name="CompanyName">https://github.com/YourCompany</StringProperty>
<StringProperty Name="Copyright">MS-PL</StringProperty>
<BooleanProperty Name="IgnorePrivates">True</BooleanProperty>
<BooleanProperty Name="IgnoreInternals">True</BooleanProperty>
<BooleanProperty Name="IncludeFields">False</BooleanProperty>
</AnalyzerSettings>
</Analyzer>
<Analyzer AnalyzerId="StyleCop.CSharp.NamingRules">
<AnalyzerSettings>
<CollectionProperty Name="Hungarian">
<Value>as</Value>
<Value>do</Value>
<Value>id</Value>
<Value>if</Value>
<Value>in</Value>
<Value>ip</Value>
<Value>is</Value>
<Value>mx</Value>
<Value>my</Value>
<Value>no</Value>
<Value>on</Value>
<Value>to</Value>
<Value>ui</Value>
<Value>vs</Value>
<Value>x</Value>
<Value>y</Value>
<Value>z</Value>
</CollectionProperty>
</AnalyzerSettings>
</Analyzer>
<Analyzer AnalyzerId="StyleCop.CSharp.OrderingRules">
<AnalyzerSettings>
<BooleanProperty Name="GeneratedCodeElementOrder">False</BooleanProperty>
</AnalyzerSettings>
</Analyzer>
</Analyzers>
</StyleCopSettings>
Depending on what you want and what you need you can disable some rules. Let’s take an example, if you look at the screenshot above we can see that StyleCop
is complaining for different rules. The rule SA1633 correspond to this warning message:
SA1633 : CSharp.Documentation : The file has no header, the header Xml is invalid, or the header is not located at the top of the file.
If you want to disable this rule you just need to:
Then you can disable it by specifying it inside our Rules tags:
<Rule Name="FileMustHaveHeader">
<RuleSettings>
<BooleanProperty Name="Enabled">False</BooleanProperty>
</RuleSettings>
</Rule>
Now if you recompile, the warning will disappear. It’s time for you to experiment it in your projects! I created a demo project just to show you a simple example.
You will find full source code in this Github repository.
Happy coding !
You liked this tutorial ? Leave a star in the associated Github repository!