Code quality: Using CodeAnalysis.FxCopAnalyzers and Stylecop in .NET Core 2 applications

 

Introduction

Why Code quality is important? Because correctness, maintainability, and even elegance are all involved in creating great code.

Code Quality is a fascinating subject, which comprises a mixture of knowledge, experience, guidelines, principles, patterns and conventions. And if that wasn’t enough, they all should be carefully adapted to each particular team and use case.

The subject gets even more confusing when you start considering tooling. However, choosing the right tools for each team and use case will make achieving quality code simpler.

This article contains an introduction to the Code Quality subject in the .Net Core ecosystem.

CodeAnalysis.FxCopAnalyzers

There are several analyzers available, the most interesting ones in my opinion are:

  • Microsoft.AnalyzerPowerPack
  • Microsoft.CodeAnalysis.FxCopAnalyzers
  • codecracker.CSharp
  • SonarAnalyzer.CSharp
  • StyleCop.Analyzers
  • Microsoft.VisualStudio.Azure.CodeAnalysis (for Azure projects)

In this article I have chosen CodeAnalysis.FxCopAnalyzers.

It can be installed via Nuget Packages, it’s easy to install and it uses a ruleset file, you can download a sample here we will see later how to configure it: myrules.ruleset

Installation

Depending the version Visual Studio you have to be careful with what package version you have to install:

If you don’t install the right package with your current version of Visual Studio you may have issues when you compile your project.

For more information you can referer to the GitHub web page: https://github.com/dotnet/roslyn-analyzers#recommended-version-of-analyzer-packages

In my case I have Visual Studio 2017 15.3 so I have to install the version 2.3.0.

StyleCop.Analyzers

StyleCop is like CodeAnalysis, it uses Roslyn Analyzers but does only care about style programming.

Code analysis searches for patterns which may indicate a bug, while StyleCop is simply enforcing style rules, a simple convention used by your team.

Installation

After installing the Nuget package add in your solution items folder the file stylecop.json (at the same place as your ruleset file), it’s mandatory to add this file to make it work

Then edit your csproj to link your stylecop.json to the project you want to analyze like this (there is no visual tool for this in Visual Studio 2017 for StyleCop):

In the mean time add the ruleset file as follow as well:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>    
     <OutputType>Exe</OutputType>    
     <TargetFramework>netcoreapp2.0</TargetFramework>  
  </PropertyGroup>
  <ItemGroup>  
     <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.3.0-beta1" />    
     <PackageReference Include="StyleCop.Analyzers" Version="1.0.2" />  
  </ItemGroup>    

  <PropertyGroup> 
     <CodeAnalysisRuleSet>$(SolutionDir)\myrules.ruleset</CodeAnalysisRuleSet> 
     <Features>IOperation</Features> 
     <NoWarn></NoWarn>   
  </PropertyGroup>     

  <ItemGroup>    
     <AdditionalFiles Include="$(SolutionDir)\stylecop.json" Link="stylecop.json" />  
  </ItemGroup>  
</Project>

When you are done with that, you can select the rules you want to enable / disable (None, Warning, Error…):

CodeAnalysis rules:

StyleCop rules:

Then when you compile Warnings or Errors appear:

Sample of underlined code:

 

Great isn’t it? 😉

Now your turn to fix analyze your code 😮