In this post we are going to see how some of the Visual Studio 2019 code analysis features can help us to improve the quality of our code and save us a lot of time in the process of cleaning up and organising our code.

You are probably familiar with tools like Re-sharper, and know how useful they are not only to automatically detect code smells, but to suggest fixes. Well, Visual Studio includes some of that functionality out of the box: code styles and cleanup code.

Let’s see a basic example of what Visual  Studio code analysis can do for us. Take a look at this picture:

Visual Studio 2019 marks unused using senteces
It seems we do not really need System.Text and System.Threading.Tasks

That image represents an everyday situation for a .NET developer. After spending hours tweaking a particular part of the code, we are left with group of unused using sentences.  Naturally, that won’t break our code, but nonetheless those lines of code are utterly unnecessary, and from a point of view of  legibility they are not doing us any favours. We could manually remove the superfluous using statements: doable but tedious. Notice also that if Visual Studio were not to automatically warn us about such details,  many of those style flaws would be easily overlooked and therefore not fixed.

The code analysis feature of Visual Studio helps us by first identifying and signalling the issue: the unused using sentences are greyed out, and second by providing useful fixes. When we move the mouse cursor over them a light bulb appears: if we click the light bulb, we are given some suggestions about how to deal with the problem. As shown in the following  picture VS is offering us the option to Remove Unnecesary Usings.

Visual Studio code analysys suggestions
This is a very simple example of how the Visual Studio code analysis works to help us to produce better quality code. Now let´s move into how we can make use of the cleanup code feature to increase our productivity.

Visual Studio 2019 Code Cleanup

In the previous examples we saw one of the more common tiny flaws that can appear in our code: unused using statements. But a real project can contain many more styling issues: variables not declared using the var keyword, class names that do not follow the proper naming conventions, interfaces not starting with the standard upper case “I”, brackets placed awkwardly, etc.

Fixing all those issues manually as we have seen before is too time confusing, therefore is specially lucky for us, developers, that Visual Studio includes a tool like Code Cleanup which allows us to automatically apply various fixes to our code.

If we click the broom icon at the bottom of the Visual Studio we can access the Configure Code Cleanup Tool.

configure Code cleanup

Visual Studio 2019 includes a list of fixer for some common code issues like the ones we have mentioned before. Code Cleanup includes a couple of Profile, and we configure them by adding/removing the available fixers:

Code cleanup profiles

Running Code Cleanup with any of the available profiles will automatically apply to the file we are working on, the fixes we have set up for the profile being run. This is much more quickly and efficient that running the actions manually one by one!

If we need to refactor not just the current file we are working one, but a large number of different files in the solutions we run Code Cleanup at solution level: right click the root node in the Solution Explorer window and it the menu that appears select Analyze and Code Cleanup > Run Code Cleanup 

Customize Visual Studio code analysys settings

Since every company has is specific code styling conventions it may be that the default Visual Studio settings for code analysing do not match those preferences out of the box. Some companies prefer to use braces even when they apply to a single line, other companies prefer not to use them on such cases.

No braces:

if (condition)
   Console.WriteLine(text);

Same code using braces:

if (condition)
{
   Console.WriteLine(text);
}

Some companies may want to prefer all methods with a this. other companies prefer to omit it, etc. If Visual Studio kept warning you about things that according your company conventions are OK, and didn´t warn you about usages that go against your company coding preferences, it wouldn´t be very useful. Would it?

We configure the coding styles from Tools > Options > Text Editor > C#:

Configure the code style preferences

From this configuration window we can tweak the coding preferences to our heart´s content; we can also play with the severity level: if we set it to suggestion we will get a message in the Error list.

Error list window

If we set it to a warning the code will still compile, but with warnings. If we set the severity to error, the code will not compile till we fix the issue.

Lastly let´s keep in mind that this configuration applies to our Visual Studio. We can click the Generate .editorconfig file from settings button – see image above – to export that configuration to a file and place it in the solution folder. The configuration in the file, will override whatever configuration we have in Visual Studio. So it will be available for any person working with that solution.