C# Coding standards using ReSharper

By , May 10, 2010 6:41 pm

Having coding standards can help improve code readability and maintenance across a team, but they can be a pain if they have to be manually followed and enforced. This article describes how we used ReSharper 5 to make applying coding standards a lot less painful.

This article:

  • Assumes your dev team uses the excellent refactoring and all-round code goodness tool that is ReSharper. If you’re not familiar, then please do check it out. It really is very good…
  • Describes how to get ReSharper settings automatically propagated around the team.
  • Does not discuss coding standards. That’s up to your team to argue bitterly come to an amicable agreement about. :)

Whilst ReSharper is not a panacea for coding standards, it is very effective for defining rules such as:

  • Naming standards: Private instance variables should be named like “_camelCase”.
  • Code formatting: Braces should appear on the next line after a statement.
  • Language usage: Warn on redundant use of the “.this” qualifier.
  • Many Many other settings…

There are a large number of settings that can be configured in ReSharper, and then automatically applied using ReSharper’s excellent Code Cleanup feature, or used to analyze source code to find misdemeanours. Bind Code Cleanup to a keyboard shortcut, and a lot of issues such as formatting and redundant code can be taken care of in an instant. With a little bit of investment (we spent a couple of hours), standards can be configured, and more importantly they can be automatically propagated around the development team through source control.

These are the steps we went through to set up ReSharper:

  1. Configuring coding standards.
  2. Automating code cleanup.
  3. Fine tuning.
  4. Sharing ReSharper settings with the team.
  5. Sorting the existing codebase out.

Configuring coding standards

First of all a set of coding standards has to be agreed on. Whilst these may grow and be refined over time, getting the bulk of them agreed and configured up-front is the most productive way of doing it.

What we did:

  • Got the big points-of-difference discussed and agreed upon e.g. should we use the ‘var’ and ‘this’ keywords? These topics should come easily to most teams, but reaching agreement and compromise is often another matter…
  • Got the team together and loaded up our Visual Studio solution. We used a single machine for the initial stages so we could see how settings affected our code.
  • Went through ReSharper settings (e.g. Inspection Severities and Formatting Style) changing them until they matched the agreed coding standards.
  • Ensured agreed standards were enforced in ReSharper through code formatting rules and displaying warnings (“ReSharper Amber”) for non-compliance.

Coding standards are now configured in ReSharper. If a code file doesn’t get the ReSharper green light, then it isn’t following the standards.

Having ReSharper give amber and red lights for code that fails rules is all well and good, but can ReSharper help us to automate any of this at all? Yes, with ReSharper’s Code Cleanup functionality.

Automating code cleanup

One of the great features in ReSharper is Code Cleanup. This allows code formatting rules, and a selection of code re-writing rules to be automated on a: single file, folder, project, solution, or a mixture of these. It’s really quite flexible.

ReSharper ships with a couple of code cleanup profiles, but a custom profile can be more easily shared amongst the team. Custom profiles can be created in ReSharper options. Add a new code cleanup profile, and set “Profile to use with silent cleanup” to it, as below:

ReSharper Settings: Custom code cleanup profile

The above screenshot shows the code cleanup profile we use here at GojiSoft. Amongst other things it will remove redundant code (basically anything that is configured in ReSharper as “Redundant”) and automatically apply code formatting rules. Who wants to go through code manually removing unused using statements, namespace qualifiers and other general crud – exactly - use Code Cleanup instead.

Setting “Profile to use with silent cleanup” to your new custom profile means that a ReSharper keyboard shortcut will invoke it and clean up your code automatically. Great, but also very dangerous. Depending on what the context is at the time, it can either clean up your current file or start to clean up your whole project or solution!

We removed the global ReSharper code cleanup shortcut, and replaced it with the Visual Studio shortcut “Format Document Ctrl K, Ctrl D”. This way it’s easy to format a *single* file as you go along. This shortcut can be set up in Visual Studio options:

Tools –> Options –> Environment –> Keyboard

ReSharper Settings: Remove gloabl code cleanup shortcutRemove ReSharper global “code cleanup” shortcut

ReSharper Settings: ReplaceVisual Studio document format shortcut 

Replace Visual Studio “document format” shortcut

Make sure that the “Use shortcut in” drop-down has “Text Editor” set as the context. This reins code cleanup to the text editor, and prevents any more of its megalomaniacal tendencies to cleanup every file in your solution…

Fine tuning

Ok, so now a fair amount of time has been spent setting up ReSharper, but will automated code cleanup act on our files just the way we like it? It’s time to fine tune those settings.

  • Select a bunch of files in solution explorer, right-click and select “Cleanup code…”.
  • Use your source control client\favourite diffing tool to see what changes have been made.
  • If you’re not happy with the results of automated code cleanup, revisit the ReSharper settings.
  • Rinse and repeat until happy.

We now have ReSharper and code cleanup set up just the way we want it, but only on a single machine. How do we go about automatically sharing these settings with the rest of the team?

Sharing ReSharper settings with the team

The built-in support for sharing ReSharper settings is a little patchy, and can’t really be done completely without having to go through a manual settings export/import loop. Getting the whole team in sync with a manual solution isn’t really going to work very well in the long run.

Fortunately ReSharper has plugin support, and ReSharper Settings Manager comes to the rescue. This plugin allows all ReSharper settings to be saved to a single file which can be placed under source control. The plugin will automatically update ReSharper settings if an updated settings file is detected e.g. when an updated settings file is gotten through source control.

ReSharper Settings Manager: Configuring a shared settings file

  • Click OK, to close the ReSharper options dialog.
  • To verify that a shared settings file is being used, open the settings manager dialog from the Visual Studio menu:

ReSharper –> Tools –> Manage Settings…

ReSharper Settings Manager: Configuration

  • Select the shared settings for your solution from the “Manage Settings” dialog and click Save.
  • You should now have a shiny new shared ReSharper settings file in the root of your solution e.g. “MySolution.R#Settings”. This file contains all of your ReSharper settings.
  • Add the new settings file to source control.
  • Get everyone on the team to install the ReSharper Settings Manager and get latest from source control to get the shared configuration file.
  • Settings will now be automatically propagated around the team.
  • Spread the word about automated code cleanup and setting up the shortcut.
  • Everyone must now “Ctrl K, Ctrl D” and make sure files are ReSharper green before checking into source control. Failure to do so will result in having an arm ripped off and being beaten around the head with the soggy end. :)

NB When you want to change the shared ReSharper settings: first change the settings, then open the “Manage Settings” dialog (ReSharper –> Tools –> Manage Settings…), select the shared settings file and click Save. The changes can then be committed to source control for sharing with the rest of the team.

Sorting the existing codebase out

This step is optional, but you may wish to sort out the existing code base to make sure it meets the coding standards. The ReSharper feature “Find Code Issues” can be used from solution explorer to find code files where ReSharper rules have failed.  We used divide and conquer to work through folders in our projects, and fixed up all the warnings. If you have a team member with a conspicuously tidy desk with nicely lined up pencils, they may even enjoy doing this task all on their own… :)

11 Responses to “C# Coding standards using ReSharper”

  1. Anup says:

    Thanks for the note on ReSharper Settings Manager. ReSharper 5.0 is a good improvement over 4.5, but it has been missing a better way to share all those inspection severity settings (I *think* they are working on it?). So, will check out this settings manager in the meanwhile!

  2. Carel Lotz says:

    Thanks – great post. We pretty much did the same for our environment although I wasn’t aware of the R# Settings manager which I will test drive soon. I find that having the team coding by these rules makes you focus on the right things when doing code reviews – the actual business logic.

    • Tim Lloyd says:

      Hi Carel – glad you liked the post. We put the effort into getting ReSharper to manage the bulk of our coding standards for exactly the reasons you allude to – having different coding formatting\standards variations across the team is distracting during code reviews. ReSharper gives us an automated way of applying standards, so is not a grind for everyone. :)

  3. Hi Tim,

    Thanks for this wonderful hands-on post on a ReSharper usage scenario. It’s always exciting to read how a development team is able to take advantage of R# without experiencing significant issues.

    • Tim Lloyd says:

      Hi Jura – Nice to hear from JetBrains. We’ve been happy users of ReSharper for a good few years now, and also recently become happy users of TeamCity here at GojiSoft (but that’s for another post). Looking forward to getting to grips with the new TeamCity release. :)

  4. Rodney says:

    Hello Tim, (There’s only one Tim Lloyd ;)

    I’m implementing coding standards and conventions in our team using R# – thanks for the post…


  5. indr says:

    Thank you!

Leave a Reply