Code Analysis – Further Rules
This is step 5 in a series of articles, starting with Seven Steps To Quality Code – Introduction.
This step is simply a case of turning the ratchet a little more and locking in further quality gains.
Previously, my suggestion for existing projects has been to set the Code Analysis rule set to “Minimum recommended rules” (or in Visual Studio versions after 2010, “Managed Recommended Rules”) in order to keep the number of rule violations to a minimum. Now I am going to suggest that the rules are tightened by using a rule set or combination of rule sets that check for further violations.
Ultimately, our goal is to have the “All Rules” rule set enabled on all code, but in practice this may not be achievable for legacy code. What we can do, is work towards this so that we can catch the more important issues in our code. For example, the effort of implementing the globalisation rules in legacy code is not going to give you much bang for buck (unless of course globalisation has become a required feature!).
A great feature of Code Analysis is that we can progressively add further rules in order to increase the range of issues that are checked. The rule sets that are available, however, do not give us a sequential order that we can progressively move through because particular rule sets focus on particular issues. We can achieve the same effect though by using the option of progressively applying multiple rule sets. You can do this by selecting the “Choose multiple rule sets” option on the Code Analysis tab of a project’s properties screen, as shown in the screenshot below:
A comprehensive list of the rule sets available can be found on the Microsoft site:
Visual Studio 2010 Code Analysis Rule Sets
Visual Studio 2013 Code Analysis Rule Sets
To avoid getting swamped by too many violations and to allow a “small bite at a time” approach, I suggest considering 1 project (i.e. csproj file) at a time and performing the following procedure:
- Run the “All Rules” rule set on the project. If you consider the number of violations to be manageable, use the “All Rules” rule set. This is the perfect situation to be in, ignore the further steps in this list and proceed to fix the violations.
- If “All Rules” is looking like a step too far, progressively add in the following rule sets one at at time, fixing the violations and checking in as you go:
Visual Studio 2010 Visual Studio 2013 Microsoft Basic Correctness Rules Basic Correctness Rules rule set for managed code Microsoft Basic Design Guideline Rules Basic Design Guideline Rules rule set for managed code Microsoft Extended Correctness Rules Extended Correctness Rules rule set for managed code Microsoft Extended Design Guideline Rules Extended Design Guidelines Rules rule set for managed code
- At this point, you may be in a position to apply the “All Rules” rule set and have a manageable number of violations. Alternatively, if that still produces too many violations, you may have a particular need for one of the remaining focused rule sets (security or globalization rules) and wish to apply one of those. (Actually, by the time you get to this stage the likelihood is that if you have a lot of violations under “All Rules” they will all be globalization related).
Once this step is followed for a solution, you will be in a great place for continuing development in a high quality environment with automatic guards in place to keep it there.
In step 6 we’ll look at peer reviews – something that everyone knows about but all too often they are the easiest thing to discard in a development process.