Photo by pxhere.com

Use StyleCop to improve your code quality

Improve your code quality easily!

Posted by Damien Aicheh on June 20, 2019 · 5 mins

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.

Introducing StyleCop

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:

  • Readable
  • Maintainable
  • Easy to follow for new developers

To add StyleCop to your project you have different possibilities:

  • Using the StyleCop.MSBuild Nuget
  • Using the Visual Studio extension

I will focus this tutorial on the first solution, the advantages of this one are:

  • You keep your StyleCop configuration with your project
  • If a new developer arrive in your team, he won’t need to install the Visual Studio extension
  • When you get the project, you just need to restore the Nugets and you are good to go

Setup StyleCop

First 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:

ViewModel warnings

Configure StyleCop

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:

  • Go to the StyleCop repository
  • Do a search with the rules identifier, in our case it’s SA1633
  • Find the associated rules name, in our case it’s FileMustHaveHeader

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 !

Do not hesitate to follow me on to not miss my next tutorial!