DEV Community

Cover image for Enforcing a consistent code quality and style in your team-wide .NET projects
Andrea Grillo
Andrea Grillo

Posted on

Enforcing a consistent code quality and style in your team-wide .NET projects

Each developer has his/her own style of coding, usually inherited from personal preferences, habits and years of experience. Just think of naming conventions, indentation, spaces, curly braces, blank lines, etc.: there are many elements that can be typed in various ways, and none of them are wrong but just different.

Especially in big teams, mixing different styles has some consequences:

  • codebase is not coherent due to lot of discrepancies in code style
  • hundreds of compiler warnings/suggestions and you may miss the important ones
  • new developers don't know what style they should use to write code in early days
  • PR reviews may become a fight between people with different point of views
  • reading the code could be difficult - just think about the naming convention that should help the reader to understand the variables scope

Then, the goal is creating a team style rather than a personal style. Moreover, keeping the solution free of warnings is definitely a good move.

I understand that enforcing code style rules in a team is important. But how can we achieve that?

Just few years ago, Microsoft (finally) added the .editorconfig support to Visual Studio. This file defines a set rules that override the local Visual Studio settings. So you can work on multiple projects owned by different teams with their own rules without having to change every time your local Visual Studio settings.

And above all, the whole team has the same settings.

Last but not least, it is possible to define rules about code quality as well. To give you an idea, it is possible to throw a compiler error if an IDisposable object is not properly disposed, or when an async method is not awaited.

NOTE: Visual Studio Code needs this extension to work with .editorconfig files.

Starting with .editorconfig files is quite simple: create a file named .editorconfig. and put it in the solution folder; then add it to the Visual Studio solution by right clicking on the solution file -> Add -> Existing Item:
Image description

Image description

Visual Studio displays an UI when you try to open the .editorconfig file, but personally I don't like it. I think it is way quicker to manually edit it - usually I use VS Code.

So, open the file and set it as the top most .editorconfig file by setting:

root = true
Enter fullscreen mode Exit fullscreen mode

Then, you can set any rule you would like to set. Personally, I start with the indentation rules for any file in the solution, and then I go deeper with more specific rules for each language and namespace. For example:

[*] # Any file uses space rather than tabs
indent_style = space

# Code files
[*.{cs,csx,vb,vbx}] # these files use 4 spaces to indent the code
indent_size = 4
insert_final_newline = true
charset = utf-8-bom

# XML project files
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]
indent_size = 2

# XML config files
[*.{props,targets,ruleset,config,nuspec,resx,vsixmanifest,vsct}]
indent_size = 2

# JSON files
[*.json]
indent_size = 2

# Powershell files
[*.ps1]
indent_size = 2

# Shell script files
[*.sh]
end_of_line = lf
indent_size = 2

# Dotnet code style settings:
[*.{cs,vb}]
Enter fullscreen mode Exit fullscreen mode

You can set hundreds of rules, of any type. On Microsoft Docs there is an (almost) complete list of available rules that you can try locally and find the balance that suits best for your needs.
Each rule can have a specific severity: you can treat a warnings as errors or demote them to suggestions, or even disable them. Be careful, the most bottom rule has the precedence over the rest.

NOTE: By default, Visual Studio only analyses open documents. If you wish to scan the entire solution go in Tools -> Options -> Text Editor -> C# (or your favorite language) -> Advanced -> Set Background Analysis Scope to Entire Solution This doesn't affect MsBuild, that instead needs to enable analysers in your csprojs by checking EnableNETAnalyzers and EnforceCodeStyleInBuild!

<PropertyGroup>
  <TargetFramework>net6.0</TargetFramework>
  <EnableNETAnalyzers>True</EnableNETAnalyzers>
  <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
</PropertyGroup>
Enter fullscreen mode Exit fullscreen mode

Namespace rules

Sometimes you want to define some rule (or exception) for a specific project or namespace. For example, I want to enforce the suffix Async to any asynchronous method in the solution but for unit tests and controllers.
Luckily .editorconfig files allow you to achieve a solution quite straightforward. Basically, you have two ways:

  • defining exceptions in the root .editorconfig file
  • use inheritance writing others files

Define exceptions in the root file

At the bottom of the .editorconfig, define the namespace of where you would like to apply the exception. For example:

[src/**{.UnitTests,/Controllers}/**.cs]
# Async methods should have "Async" suffix
dotnet_naming_rule.async_methods_end_in_async.severity = none
Enter fullscreen mode Exit fullscreen mode

Using inheritance

Inheritance is a good alternative. Do you remember the root property we set at the beginning? That means that the given .editorconfig is applied to the whole solution. But however it is possible to create other .editorconfig files in subfolders that will inherit all the rules from the root file. Rules defined here takes the precedence. Getting back the previous example, you can navigate to the Controllers/UnitTests folders and create a new file. Do NOT define the root=true property and add this:

# Async methods should have "Async" suffix
dotnet_naming_rule.async_methods_end_in_async.severity = none
Enter fullscreen mode Exit fullscreen mode

Shall I manually change any code formatting error?

This is a tricky question with several solutions.

The first solution is to use the built-in Visual Studio feature - Code clean up. Personally I don't like it for the following reasons:

  • it has a specific shortcut (CTRL+K, CTRL+E) that developers may forget to use every time they edit a file (we work under pressure sometimes!)
  • it's annoying
  • it must be configured and the configuration is saved locally and there is no way to share it with your team.

For these reasons, I had been using Productivity Power Tools, an (historical) extensions developed by Microsoft. It provides a feature to format the entire file when the file is saved (through UI or the shortcut CTRL+S). It makes simply impossible to forget to format your file before committing it in your source control.

But, unfortunately Microsoft decided to remove this feature from the PPT 2022 version - apparently without any reason, there are plenty of negative feedbacks for this.

Luckily, a developer named Elders has published an extension called Format document on Save and it does simply what its name says: it formats the document when the user save it!

I want to sort and remove unnecessary using statement in my project, but I have to do it manually even if I set the rule. Any way to automate it?

Yes, you can do it!
Going in Tools -> Options -> Format Document on Save you can set the commands to be executed when user saves the file.
In order to format the file and then sort and removing using statements, set it as follow:
Image description

Good, but any team member shall remember to set this feature on?

Nope! This extension is able to read a file named .formatconfig that you can check in your source control and share with your team. It doesn't need to be added to the Visual Studio solution, but just to stay in the same level of the solution file. Local user preferences are ignored.

Here's an example of definition:

root = true

[*.*]
enable = true
enable_in_debug = true
command = Edit.FormatDocument Edit.RemoveAndSort
allowed_extensions = .*
denied_extensions =
Enter fullscreen mode Exit fullscreen mode

Anything familiar? 🐱‍💻

Working example

Last thing to do is leaving you a link to a repository where you can find a fully working solution and especially an .editorconfig file with lot of rules defined. You can get the remaining ones on Microsoft Docs.

GitHub

Enjoy! 🎉

Discussion (0)