Consistent C# coding styles in .NET Core and VS 2019

Are you a part of champion C# team who has developers from Different background (JavaScript, Python, Java, Ruby etc.)?

Some of them may prefer their own ways of indentations, placing curly brace locations (JavaScriptish), even not putting them at all (Python-ish).

Curly Brace Conventions by the JavaScript community.
But, C# community prefers that the opening or closing brace within a C# statement, element, or expression should be placed on its own line.


Members from your team have different perspectives over C# coding styles however your team want everyone to follow agreed guideline as if code is written by a single developer and it’s readable?

Are you tired of seeing/receiving linting comments in the C# code reviews?

Multiple violations: Space required after if and closing parenthesis, Extra parenthesis


Do you want to automatically find and fix those as a part of C# code self-reviews?

Omission of curly brace introduced a serious bug. return Buzz will gets always executed.

remove unused variables, remove unused methods, remove unused namespaces and MANY MORE.


Answer to these questions are combination of tools:

  • .EditorConfig
  • StyleCop

If you have a single or multiple project make sure you create a .editorconfig at root level (Solution level) so that all the rules are applicable to all the projects under a solution.

Steps:

  • Open your project solution in the visual studio 2019.
  • Right click on your solution file

A solution with multiple projects (For you it could be 10 or 100 projects)


  • Click on New EditorConfig. It will create a file with pre-configured rules.


  • Install StyleCopAnalyzers using the NuGet command line or the NuGet Package Manager in Visual Studio 2019 for all the projects.


  • Right click on solution and Click build. Now it will start showing fixable things as warnings in your IDE’s “Error List” window.
Error List window in the visual studio showing fixable warnings.

Raise the bar


You can even push yourself and set few rules as errors in the .editorconfigs

Option from IDE to set some guideline as Error and other options.
Set obvious suggestions as an error and some as none to reduce the noise.


Do you want to try it before applying it to your project?

Here is the git repo link:

https://github.com/way2datta/.net-practices/tree/master/Linters




Dattatraya Kale

Aspiring agile software craftsman, clean code, polyglot, in love with different programming paradigm. I am on a never-ending journey towards mastery of software.

Leave a Reply