If you are working in a big team, Stylecop is a great tool to ensure your team writes code in a consistent way (to learn more about Stylecop read this. I recommend that anyone reading this post who isn't using Stylecop within their C# projects should install it ASAP. Enabling Stylecop within your project is pretty easy and in this guide, I'll show you how, so read on.
Why You Should Use Stylecop
Stylecop's main aim is to ensure the syntax of your source code is written against a predefined set of rules. These rules define syntax issues like:
In what order things should be laid out
How you should name things
Spacing and tabbing rules
Docstring rules
These rules will help ensure your codebase is written in a very similar manner, which should make it easier for people to understand later on. If you're not taking advantage of Stylecop then your codebase is missing out, however, I get why some teams are reluctant to implement it. One of the biggest complaints I hear from developers who play around with Stylecop for the first time, is they moan about how much effort it takes to follow the guidelines.
I've worked on projects where Stylecop validation was only triggered as a task on the build server. This meant people would work locally and only run Stylecop when they thought they were finished. This workflow is not ideal. First, it's easy to break the build, e.g. you do your work, test it, run Stylecop refactor and then check it in. If you refactor to apply a rule there's a chance you might introduce a bug. Meaning you can potentially break the build (I have definitely been guilty of this) by accident easily. Second, if you leave it to the end, it starts to feel like a chore.
A Recommended Way Of Using Stylecop
If you care about code quality and you want to implement Stylecop, my suggestion is to enable Stylecop validation on your Visual Studio build. This means developers have to work towards and refactor to Stylecop standards constantly. After about two weeks the team will generally start to code in a Stylecop way, as they get instant feedback about the violations they break. Be warned there might be a lot of moaning in those two weeks!
Enabling Stylecop is easy. In your project make sure that you have Stylecop installed. To enable it, right click on your project and select 'properties'
Make sure you enable the Enable Code Analysis on Build
option. This should mean that everyone will be forced to work with Stylecop.
How To Set The Stylecop Ruleset Your Project Uses
When you use Stylecop, my recommendation is to customise the rulesets to work for your team. Personally, I think being forced to add the this
qualifier is stupid. Also, if I'm not creating a public-facing API I don't like being forced to add docstrings. From my experience, these get outdated very quickly and they don't contain any extra information I couldn't find from reading the method declaration myself. Swagger is a better option anyway! To modify the rules is also easy:
At the bottom of the screen, you'll see an Open
Stylecop setting button. Click that and select browse
. From here, you pick a custom ruleset to use. Remember the ruleset needs to be checked into your source control within your solution too! Happy Coding 🤘