Coding standards. Why and how with C#
Tags: csharp, codingstandards, visualstudio, rider
Published: 2022-02-04 04:20:31

Sigh.  Coding standards.  

Ever have this experience?  
![Code Review Experience](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbzsld81sqrd8rp9y5v2.jpg)

After an interaction like that, you walk away feeling like this.

![Long Sigh](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/je0mo46tydfndff0ja6e.gif)

I'm sure everybody at one point in their coding career went through this when your PR was getting code reviewed.
What a shame!! 

---

## Coding standards?  Uhhh...why? 🤔
I have had both good and bad experiences being on teams with and without coding standards.  Unfortunately, this is a human problem, not a technical one; solving the human issue is WAY more complicated!

But it doesn't mean that using technology can't help improve things!!

To help eliminate the issues that come with the pull request and code review processes, we can use technology to help shift our minds away from the little things like bracing style or spacing around parentheses.  We should concentrate on things that bring value, such as architecture and code comprehension. But why do these things manually when technology can do it for us?  With the manual process out of the way, we can keep the focus on what matters most; producing value.

I love coding standards for this very reason.  We're not trying to "force" people to code the way we do. We want to get the team on the same page so we can concentrate on more important things.

Remember, there is no "wrong" way to code; just different ways to code.  It depends on a variety of things like: language, the purpose of the code and software, the people on the team, and more.

---

## Can .NET  do this with the associated tooling?
Different platforms, languages, and tech stacks have capabilities to enforce and standardize your codebases.  Each has positives and negatives and their own quirks.  Fortunately, Microsoft and the .NET stack and associated tooling such as [Visual Studio](https://visualstudio.microsoft.com/vs/) and [JetBrains Rider](https://www.jetbrains.com/rider/) have fantastic capabilities to help with this kind of thing.

---

## Uhhh...how? 🤔
It depends on what kind of tooling you use.  No worries, it's easy to do.  
In the .NET world, enforcing coding standards is done by using an **_.editorconfig_** file.

You're probably thinking, "what is an **_.editorconfig_** file"?  It's simply a text file that uses syntax similar to an **INI** file.
{% wikipedia https://en.wikipedia.org/wiki/INI_file %}

Adding this file in your project will give instructions to the tooling on what to enforce, what not to enforce, and at what level to enforce it.

Let's go over how to add an **_.editorconfig_** file to a C# project in **Visual Studio** first.  Don't worry,we'll cover **JetBrains Rider** afterward.

---

## Adding an editor config file to a project using Visual Studio (2022)
1. Right-click the project you want to add the **_.editorconfig_** file to.
2. Choose the **_Add_** menu item.
3. In the sub-menu, click the **_New EditorConfig_** menu item.


![Add editorconfig using Visual Studio](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/90ww2oni2zo5w4ize389.gif)

If you want to see the contents of the editorconfig, open the file in **VS Code** or any text editor.

Ok, sweet!  We have our **_.editorconfig_** file.  But wait! It's pretty much empty!  What the 🤬? What's goin' on here?

Let me explain.

The **_.editorconfig_** files are just text.  There is only one rule with a comment at the top of the file. The **_.editorconfig_** UI is a new feature that was introduced in the past couple years.  Even though the file is pretty much empty, the UI shows that there are many rules that aren't visible in the file.  This is because a new **_.editorconfig_** file will use all of the default values for all of the rules.  Once you change a rule value in the UI to something other than the default value, it will automatically insert that rule into the **_.editorconfig_** file.  Basically, if the rule is not in the file, then it is using the default value and there is no need to have the rule in the file.

![Add editorconfig using Visual Studio](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cictpi9ujgxta2lwm7qb.gif)

---

## Setting rules using Visual Studio (2022)
1. When a quick action with a 💡icon shows on the left side, click the little down arrow to fly out a tiny menu.
2. Choose the **_Suppress or Configure issues_** item at the bottom to display another menu flyout
3. Choose the type of configuration you want to apply

That's it!!  An entry for the configuration you have chosen will automatically be added to the **_.editorconfig_** file.  Of course, you can always just add the setting manually as well.

Refer to the GIF 👇🏼 below to see it in action.

![Adding rule with Visual Studio](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5n4h5k4pe4457bucpk3r.gif)

---

## Adding an editor config file to a project using JetBrains Rider
1. Right-click the project you want to add the **_.editorconfig_** file to.
2. Choose the **_Add_** menu item.
3. In the sub-menu, click the **_New EditorConfig_** menu item.

![Add editorconfig using JetBrains Rider](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8hcno5rezgrvmz5g68s.gif)

---

Ok, sweet!  We have our **_.editorconfig_** file.  But wait! It's empty!  Seriously? 🤦🏼‍♂️  
It's ok.  We can still setup the rules. 👇🏼
## Setting rules using JetBrains Rider

### TL;DR
**JetBrains Rider** rules can only be edited manually.

### Long Explanation 🥱
**JetBrains Rider** support for **_.editorconfig_** files, analyzing code, and enforcing standards are top-notch.  They support advanced use of the file format and let you mix **Resharper**, **.NET** specific standard rules, and **StyleCop** rules in the file.  In my opinion, it is the best in the industry.  But, as we know, every piece of software does things differently.  

But the downside is that **JetBrains Rider** does not automatically insert the rules into the config file.  
But don't worry.  I have a feeling that this feature will be coming to **Rider**.  It was recently added to **Visual Studio**. 
However, for some reason, configuring rules using action lists adds the configuration to the **_.sln.DotSettings_** file, not the **_.editorconfig_** file.  This is one of the ways that **JetBrains Rider** let's you share rules with the team by committing them to your Git repository.

That said, if somebody knows how to interactively setup rules in **JetBrains Rider** with the UI, let me know!!
I looked into the documentation and found [this](https://www.jetbrains.com/help/resharper/2022.1/Using_EditorConfig.html#edit-editorconfig-interactively), but I couldn't find the settings on how to do it.

Let's show how to manually enter a single rule.👇🏼
![Adding rule with JetBrains Rider](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3c9zhk3dxfe98l1gpeff.gif)

---

## Conclusion
Ok, let's be realistic here.  Every project has its unique requirements, oddities, and edge cases.  Because of these reasons, each project requires its own coding standards. This isn't a one-size-fits-all scenario.

I love having standards put into place because it brings my projects and me a great deal of value.  More importantly, it brings value to my users!  My open-source projects [Velaptor](https://github.com/KinsonDigital/Velaptor) and [CASL](https://github.com/KinsonDigital/CASL) require consistency and strictness due to their low-level nature.  Using the tooling and **_.editorconfig_** files helps me achieve this.

Cheers!!

If you are interested in any of my projects and how I set up and use **_.editorconfig_** files and analyzers, check out my projects and list of resources below.

---

## Resources
- [StyleCop Analyzers for the .NET Compiler Platform](https://github.com/DotNetAnalyzers/StyleCopAnalyzers)
- [Index of EditorConfig Properties](https://www.jetbrains.com/help/resharper/2022.1/EditorConfig_Index.html)
- [Overview of .NET source code analysis](https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/code-style-rule-options?view=vs-2022)
- [MS Docs / EditorConfig](https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2022)
- [editorconfig.org](https://editorconfig.org/)
- [Editor Config Cheat Sheet](https://devhints.io/editorconfig)
- If your interested in the rules that I use for [Velaptor](https://github.com/KinsonDigital/Velaptor) and [CASL](https://github.com/KinsonDigital/CASL), refer to the GISTS that I created.  Use them in your projects and tweak as necessary!!
  - [Solution Level Config](https://gist.github.com/CalvinWilkinson/60f39b137febee0c67c0c75864d1ad2a)
  - [Unit Test Project Level Config](https://gist.github.com/CalvinWilkinson/578959b4e03c90a7a954faf6ccfe6513)