Wednesday, September 10, 2008

Style Cop and VS Defaults

I have been playing with Microsoft new(ish) code analysis tool StyleCop over the last couple of nights (god forbid I use it on our work code base!). Its a pretty cool tool in the same vein as FXCop but with more of a focus on general code layout etc. In this regard its pretty cool. It gives you warnings for when rules are broken & it integrates into VS quite nicely. Unfortunately there is no clean way of integrating it into my NAnt scripts as there is no exe to call. Apparently you can put it into your MSBuild scripts, but that's kinda weak that they have not provided an exe for other means... any who...

It also highlights the fact that VS default templates do not adhere to the StyleCop rules. If you run StyleCop over a standard code base you will see a bunch of warnings straight off the bat. Firstly there are no file headers on any standard C# file.

An XML type header need to be placed in every file. This to me stinks of C headers... are we not a bit past this? Is this really required? There are copyrights on the project in the assembly file, do we need the clutter on every single file (and repeated in the AssemblyInfo.cs twice)?

Well if you do then feel free to add a header similar to:

// <copyright file="AssemblyInfo.cs" company="FullStack">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>Rhys Campbell</author>
// <email>rhysc@fullstack.co.uk</email>
// <date>2008-09-10</date>
// <summary>Contains assembly information.</summary>

It also points out the VS, by default, places the using statement on the outside of the namespace which the pigs don't like!

Fortunately there is an easy fix to these and other default behaviours: Templates.

Visual Studio has configurable templates for the files it creates, so you can modify the standard output to what you want. These template reside as zip file in the C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\ folders and are actuall cached near by in the ItemTemplatesCache folder. You can make your own personal templates and by default they hang out in C:\Users\Rhys Campbell\Documents\Visual Studio 2008\My Exported Templates\.

Say for example we wanted to trim the  StyleCop warnings of our projects down a bit then the above examples could be changed from

using System; 
using System.Collections.Generic; 
$if$ ($targetframeworkversion$ == 3.5)using System.Linq; 
$endif$using System.Text; 
namespace $rootnamespace$ 

class $safeitemrootname$ 
    { 
    } 

To something like :

// <copyright file="AssemblyInfo.cs" company="FullStack">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>$username$</author>
// <email>$username$@fullstack.co.uk</email>
// <date>2008-09-10</date>
// <summary>   </summary>

namespace $rootnamespace$ 

using System; 
public class $safeitemrootname$ 
    { 
    } 
}

To do this just crack open a new class, make the changes to the layout you want using template parameters in logical places (i.e. class, namespace, date etc) and File > Export to Template... *

You can select the namespaces required and even pick a pretty icon for your new template. This is particularly good if you use a bunch of standard classes (Tests, NHibernate, WCF all spring to mind) and it should speed things up nicely :)

To use you new template Just Add a new item to a project and under all the standard templates will be a "My Templates" section with your new tempate happily residing.

*If your Export to Template, like mine, is not there, go to Tools > Customise and drag the command from the depths of the hidden file options on to the file menu drop down (i.e. Commands > Categories=File > Command = Export Template..)

1 comment:

Lee Campbell said...

All sounds like a bit of a crock. I was looking forward to it, then I don't care for the usings being in the namespace & I don't want a copyright on each file. Surely M$ should either change their default templates or ship the "corrected" ones with style cop.

ctrl+shift+D is fine with me.