Writing good code has always been important and I don’t think there is a single developer out there who would disagree with that. But the big issue here is that ‘good’ can be subjective, what is it that defines ‘good’ code? One developer’s idea of good code could be entirely different from another, and most likely is, even though it probably shouldn’t be. How many hours do developers dedicate to how code should be written, rather than bashing out code that may well work, but still suffers from any one or more of this bunch of horrors; Poor Commenting, Inappropriate Naming, Inconsistent Spacing and Indentation, No Error Checking, Repetition, Lack of Clarity, Overly Complicated Design, Hidden Assumptions, Failure to Use Standard Libraries (.NET Framework), Little or No Testing Support (Unit Tests) .
Luckily for C# developers there is a way you can automatically check for a lot of these problems and have them highlighted when you build your project in Visual Studio. Enter StyleCop the self-styled, overly ambigious, but nevertheless essential tool to any C# developer’s toolkit. The fashion police of the code world can be installed locally or incorporated into a CI build process (MSBuild) to enforce the adherence of coding standards. Personally, I think it’s a revelation and I have seen clients build this into their requirements from the start of a development project. Whilst it may not be industry standard, it is catching-on and every developer worth his or her salt should incorporate it into their build process.
So, first, what is it? Well let’s have a look at what Wikipedia has to say…
“StyleCop is an open source static code analysis tool from Microsoft that checks C# code for conformance to StyleCop’s recommended coding styles and a subset of Microsoft’s .NET Framework Design Guidelines. StyleCop analyses the source code, allowing it to enforce a different set of rules from FxCop (which, instead of source code, checks .NET managed code assemblies) . The rules are classified into the following categories:
StyleCop includes both GUI and command line versions of the tool. It is possible to create new rules to be used.”Â 
Second, how do you get it into your Visual Studio solution? Well it’s really quite simple and here’s how.
1. Head over to the home of StlyeCop and grab yourself a copy of the MSI,
3. Check that the install is successful, for Win 7 64-bt machines, there should be a file called StyleCop.Targets located in a new StyleCop folder which is in the standard MSBuild folder, i.e. C:\Program Files (x86)\MSBuild\StyleCop.
4. Also check that it has installed all relevant files in its own directory, i.e. there should be a StlyeCop directory at C:\Program Files (x86)\StyleCop [version number] and a corresponding StyleCop.dll file.
At this point the user has options as to what to do with the files that a required in Visual Studio (StyleCop.Targets & StyleCop.dll), but in my particular situation I decided to copy the files I needed to a separate directory in my VS solution. This is not necessary, but at the time I had a folder of 3rd party tools in the solution and I also put StyleCop there for completeness.
The next stage is to modify VS projects to pick up StlyeCop at compile time and this is how you do that.
1. Right-click the project in the solution from the Solution Explorer tab
2. Select ‘Unload Project’
3. Right-click again on the project
4. Select ‘Edit [project name]’
5. Navigate to near the end of the file to a location just above the commented lines
<!– To modify your build process, add your task inside one of the targets below and uncomment it…
6. Add the Import tag to which points to the location of the StyleCop.Targets file, i.e.
<Import Project=”..\..\My Folder\StyleCop\StyleCop.Targets” /> where ‘My Folder’ is the location where the StyleCop.Targets and StyleCop.dll files have been copied to. If they have not been copied anywhere, then this location is the install location of StyleCop.
7. Save and close the file
8. Right-click on the project and select ‘Reload’
9. To ensure the files have been picked up correctly, re-start VS
10.Building solution should now show StyleCop warnings.
By default all rules are applied and whilst this may seem a little pedantic it’s certainly a good way to start. Rules can be relaxed and new rules added, however this is a great place to start, so get your code policed now!