Do you work on a team that cannot seem to follow a coding style? Or do you sometimes find yourself not following your own preferred coding style? If you fall into one of these categories or just want to force yourself to follow a standard (set by yourself or a team), then I recommend that you add a EditorConfig file to your project. It sits right on the root of your project next to your ‘readme.md’ or your ‘.gitignore’.
Now you are probably thinking to yourself, why would I want this? I could just simply keep my code clean because I am the only one writing it. Well, I can promise you that you have missed at least one thing in your code where you broke your own rules. So with that said, just create yourself a .editorconfig one time and use it on all your projects to make sure your code matches all around. As a matter of fact, I will provide you my default editorconfig to get you started if you are feeling lazy (see below)!
Okay, I’m sold…what next?
Good, glad you made it this far. It probably means that you care about clean code. Gold star for you! Now, if you were like me, you didn’t know where to start. What does it look like? Where do I go from here?
Slow down! So first, lets make a ‘.editorconfig’ in the root of your solutions folder at the same place that you would put your .gitignore or readme.md. Done? Okay cool. Lets continue.
This next part is optional but I recommend it because I love intellisense. If you are using Visual Studio and have not heard of a gentleman named Mads Kristensen then I suggest that you look him up and all the nice Visual Studio extensions that he has built. They will blow your mind! He has developed the EditorConfig Language Service and it is awesome! It is open source (Obligatory GitHub Link: https://github.com/madskristensen/EditorConfigLanguage)! This plugin will give you intellisense and will underline potential issues with your EditorConfig file. Not to mention, Mads is super nice and helpful. I submitted a bug to him on Github, then in one hour he had confirmed the bug and fixed it!
Okay now, you may start to ask yourself “How do I get started?” “Where can I figure out the rules to set?”. Don’t worry, I have you covered. I can only speak for Visual Studio but there is a nice well written Microsoft article for the different rules and how to create your own rules: .NET Coding Convention Settings For EditorConfig.
As promised, here is my sample config file. This is valid as of posting of this article in January of 2018. This file has mostly default settings but there are a few minor tweaks that I prefer.
If you are on a team that has a hard time with keeping the code clean and following some code styles, then this might be a good solution for you. For me, it is mostly just to make sure that I keep myself honest and have clean code. I highly recommend you at least try it. If you are not happy then you can always go to some other style management system. I just like this because it helps you as you are coding in Visual Studio.