Tackling Code Reviews using Automation
I’m currently undertaking a QA exercise on outsourced code as it gets delivered. In order to increase the productivity of the review process by reducing the defects raised and number of iterative fix cycles, I have been involved in writing tools that can be used by the external developers for them to check for compliance with specific coding standards and techniques demanded by the business.
The tools I am using for this automation are StyleCop and FxCop, Microsoft’s free code styling and managed code analysis tool. A great number of the standards we have defined are covered by the default rules that ship with these tools but there have been exceptions. For instance, we do not expect any TODO
comments in code that has been delivered.
There are several detailed tutorials covering how to write these rules so I’m not going to repeat what’s already out there (links below).
http://scottwhite.blogspot.com/2008/11/creating-custom-stylecop-rules-in-c.html http://www.lovethedot.net/2008/05/creating-custom-rules-for-microsoft.html (3 parts)
The code for my TODO
rule is below. You simply need to call this method from one of the visitor callbacks mentioned in the above posts.
private static void FlagViolationForToDoComment(
SourceAnalyzer analyzer,
CsElement element)
{
var todoComments = element.ElementTokens
.Where(e => e.CsTokenType == CsTokenType.SingleLineComment ''
e.CsTokenType == CsTokenType.MultiLineComment)
.Where(c => c.Text
.Substring(0, c.Text.Length >= 10 ? 10 : c.Text.Length)
.Contains("TODO"));
if (todoComments.Any())
{
foreach (var comment in todoComments)
{
analyzer.AddViolation(
comment.FindParentElement(),
comment.LineNumber,
CustomRules.ToDoCommentsShouldBeActionedThenRemoved);
}
}
}
One of the things to watch for is that if you install StyleCop with the MSBuild plugin that StyleCop may reside in more than one folder with one instance being registered with Visual Studio, and the other with the Explorer shell. In order to update the rules for use with both you will need to copy your rules library into each installation folder; in my case:
C:\Program Files\Microsoft StyleCop 4.4.0.14
, andC:\Program Files\MSBuild\Microsoft\StyleCop\v4.4
There’s also a StyleCop contrib project that provides a test-runner allowing unit testing of your StyleCop rules. I can’t recommend this enough - in my mind the only way to write StyleCop rules is to use TDD.