How To Enable Stylecop Validation Of Visual Studio Build With A Custom Ruleset

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 now 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: in what order things should be laid out, how you should name things, spacing etc.. These rules will help ensure your code base 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 code base 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, the biggest thing they moan about is 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, which meant people usually would work locally and then at the end run Stylecop and refactor things  This process does come with a few major downsides.  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 and then check-in without retesting you potentially can break the build (I have definitely been guilty of this).  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 then my suggestion is to enable Stylecop validation on your Visual Studio build. This means developers have to use and refactor to Stylecop standard constantly. After two weeks people will generally start to code in a Stylecop way, as they get instant feedback about the violations they break.

Enabling Stylecop is easy. in your project make sure that you have Stylecop installed, right click on your project and select 'properties'

Make sure you enable the 'Enable Code Analysis on Build'.  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 having a 'this' qualifier is stupid.  Also, if I'm not creating a public facing API I don't like doc strings.  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.

At the bottom of the screen above, 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.

Enjoy!

 

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top