Improving your C# Skills
上QQ阅读APP看书,第一时间看更新

Writing quality code

For every performance-efficient application, code quality plays an important role. As we already know, Visual Studio is the most popular Integrated Development Environment (IDE) for developing .NET applications, and since Roslyn (.NET Compiler SDK) exposes compiler platforms as APIs, many features have been introduced that do not only extend the capabilities of Visual Studio, but enhance the development experience.

Live Static Code analysis is one of the core features that can be used in Visual Studio in developing .NET applications which provides code analysis during development while writing code. As this feature uses the Roslyn APIs, many other third-party companies have also introduced sets of analyzers that can be used. We can also develop our own analyzer for a particular requirement, and it's not a very complicated procedure. Let's look at a quick introduction on how we can use Live Static Code analysis in our .NET Core project and how it benefits the development experience by analyzing code and giving warnings, errors, and potential fixes for them.

We can add analyzer as a NuGet package. In NuGet.org, there are many analyzers available, and once we add any analyzer into our project, it adds a new Analyzer node into the Dependencies section of the project. We can then customize rules, suppress warnings or errors, and so on.

Let's add a new analyzer from Visual Studio in our .NET Core project. If you don't know which analyzer you want to add, you can just type analyzers in the NuGet Package manager window and it will list all the analyzers for you. We will just add the Microsoft.CodeQuality.Analyzers analyzer, which contains some decent rules:

Once the selected Analyzer is added, a new Analyzers node is added into our project:

In the preceding picture, we can see that three nodes have been added to the Analyzers node, and to see/manage the rules, we can expand the subnodes Microsoft.CodeQuality.Analyzers and Microsoft.CodeQuality.CSharp.Analyzers, which is shown as follows:

Moreover, we can also change the rule severity by right-clicking on the rule and selecting the severity, which is shown as follows:

In the preceding picture, rule CA1008 states that Enums should have a value of zero. Let's test this out and see how it works.

Create a simple Enum and specify the values, which are shown as follows:

public enum Status 
{ 
  Create =1, 
  Update =2, 
  Delete =3, 
} 

You will notice as soon as you write this code, it will show the following error and it will provide potential fixes:

Finally, here is the fix we can apply, and the error will disappear:

You can also use one of the popular Visual Studio extensions known as Roslynator, which can be downloaded from the following link. It contains more than 190 analyzers and refactorings for C# based projects: https://marketplace.visualstudio.com/items?itemName=josefpihrt.Roslynator.

Live Static Code analysis is a great feature that helps developers to write quality code that conforms to the best guidelines and practices.