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.
- 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 bitterlycome 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:
- Configuring coding standards.
- Automating code cleanup.
- Fine tuning.
- Sharing ReSharper settings with the team.
- 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:
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
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…
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.
- Download and install ReSharper Settings Manager.
- Open the Visual Studio solution on the machine used to set up the coding standards rules.
- Open ReSharper options and configure ReSharper Settings Manager to use a single configuration file per solution:
- 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…
- 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…