Configuring coding conventions in Visual Studio

Configuring coding conventions in Visual Studio

When developing software in a team it’s useful to have certain rules on how to write code. Consistency in your code will improve readability and maintainability. With EditorConfig you are able to enforce coding guidelines without having to install additional tools. It even works across IDE’s!

Visual Studio allows you to configure coding style rules. Based on this configuration, Visual Studio will suggest code improvements or show an error. Unfortunately there was no easy way to share this configuration with your team. This is why support for EditorConfig was added to Visual Studio 2017.

With EditorConfig you are able to specify code rules in a .editorconfig file. Visual Studio will automatically check if your code is meets the rules and show violations if it doesn’t. If you already have configured coding rules in your personal preferences, by default EditorConfig will override your rules. Adding your EditorConfig file to source control will allow you to easily share the configuration and have versioning for your guidelines. The configuration can be used for a wide variety of languages and IDE’s. For the IDE’s that are not supported out-of-the-box you can download a plug-in.

Support for EditorConfig is already available in Visual Studio for Windows, and is available in the latest preview of Visual Studio for Mac (7.5 Preview 5). Microsoft is still extending the support and the .NET community is working on EditorConfig to support more .NET features as well. If you want to contribute, please check out the website.

Getting Started

Visual Studio will look for a the .editorconfig in your solution. When a EditorConfig file was detected, Visual Studio will start enforcing the rules on the files at the same hierarchical level and below. This will allow you to override EditorConfig files if needed by adding configuration for different levels. If you want to use a single configuration for the entire solution, simply add “Root=true” to the configuration file. After editing your EditorConfig file, you need to reload your files for the new rules to take effect.

To simplify writing of your EditorConfig files there is the EditorConfig Language Service extension. This will improve IntelliSense, syntax highlighting, visualization and more. The EditorConfig from the Roslyn project might be a good starting point.

Bas de Cort

About Bas de Cort

Bas is a software developer from The Netherlands with a great passion for mobile and innovative technology. This blog will mostly cover mobile technology, especially Xamarin.