Friday, September 25, 2009

DevTnT 13 – StyleCop for ReSharper

In my previous post, I talked about StyleCop and how it helps you to enforce a consistent source code style throughout your project. However, StyleCop only flags violations but does not fix them – even though most of the times the fix is trivial.

Enter StyleCop for ReSharper, a plug-in for ReSharper that will show StyleCop violations in real time, and offers a number of quick fixes (light bulbs) for most cases.

stylecopresharper

StyleCop for ReSharper includes an automatic update detection mechanism and will prompt you if a new version is available.

DevTnT 13 – StyleCop

Most people agree that having a consistent coding style throughout a solution is beneficial to the development process.  In the past it has been difficult to enforce a style, though.  StyleCop is one answer to that problem.

StyleCop – the tool formerly known as Source Analysis – is a tool that analyzes C# code and warns about violations against “a common set of best practices for layout, readability, maintainability, and documentation of C# source code.” It can be run from inside Visual Studio, and can also be integrated into MSBuild scripts.

When run inside Visual Studio, StyleCop integrates with the Error List tool window, and double-clicking on a warning will take you to the appropriate spot in your source files.

stylecop

It is possible to configure which rules StyleCop should check, however it is not possible (yet) to modify existing rules or define your own rules.

StyleCop can also be used as part of your build process using the out-of-the-box MSBuild integration.

DevTnT 13 – GhostDoc

GhostDoc is a plug-in for Visual Studio that can automatically generate XML comments. GhostDoc uses a configurable rule set to derive meaningful comments for constructors, events, properties and events.

For example: XML comments for constructors should start with “Initializes a new instance of the <classname> class.” GhostDoc can automatically generate that comment.

A method called “button1_Click” will be commented with “Handles the Click event of the button1 control”, complete with comments for the parameters.

A property “NameOfTheUser” will be commented “Gets or sets the name of the user”.

The rules of GhostDoc can be extensively customized, but in my experience this usually isn’t necessary because the default rules are pretty good.

GhostDoc_Config_771x637

One disadvantage of GhostDoc is that there is no official support for documenting an entire class (or project or solution) at once. There is an unofficial Visual Studio macro that does exactly this, though: http://jb-brown.blogspot.com/2008/09/using-ghostdoc-in-macro-to-comment.html.