Jaa


StyleCop Compliant Visual Studio 2010 Code Snippets January 2011 Release

Visual Studio Snippets is an open source collection of code snippets for the C# programming language, providing StyleCop compliant versions of the 38 code snippets that ship within Visual Studio along with 44 additional StyleCop compliant snippets. You can download an Excel spreadsheet containing details of all 82 code snippets here.

As mentioned in my previous blog post about the Visual Studio Snippets collection, you can immediately see the difference if you take a moment to consider the svm.snippet that creates the following code out of the box. Within the November 2010 release, discussed within this blog post, were numerous snippets for developers writing multi-threaded code.

static void Main(string[] args)
{

}

StyleCop rule 1600 requires that the method must have a documentation header and StyleCop rule SA1400 requires that the method must have an access modifier. The Visual Studio Snippets version has been refactored to resolve these two issues.

/// <summary>
/// Defines the program entry point.
/// </summary>
/// <param name="args">An array of <see cref="T:System.String"/> containing command line parameters.</param>
private static void Main(string[] args)
{

}

Visual Studio Snippets provides refactored versions of all 38 code snippets that ship with Visual Studio and hopefully the example with the svm.snippet above illustrates the benefit of using these refactored code snippets.

Within the January release there are 4 new code snippets, bringing the total number of code snippets in the library to 82!

ta.snippet allows us to easily call the Trace.Assert overloaded methods to generate code such as the following, with the ability to then alter the assertion condition and subsequent message.

Trace.Assert(true, string.Empty);

da.snippet allows us to easily call the Debug.Assert overloaded methods to generate code such as the following, with the ability to then alter the assertion condition and subsequent message.

Debug.Assert(true, string.Empty);

propn.snippet allows us to easily create a property supporting the INotifyPropertyChanged interface and will generate code such as the following.

/// <summary>
/// Gets or sets the name of the person.
/// </summary>
/// <value>The name of the person.</value>
public string Name
{
get
{
return this.name;
}

    set
{
if (value != this.name)
{
this.name= value;

            this.OnPropertyChanged("Name");
}
}
}

ts.snippet allows us to easily create a TransactionScope to enable a code block to be transactional, generating the following code. The ts.snippet allows you to also use the Surrounds with… option to encapsulate several lines of code within a transactional scope.

using (TransactionScope scope = new TransactionScope())
{

    scope.Complete();
}

As I release new versions of the Visual Studio Snippets and Visual Studio Templates projects I would encourage you to give feedback on them or even just let me know that you are using them and hopefully they are making you or your team more productive. If you’re on Twitter you can also contact me at @dougholland and use the #vssnippets or #vstemplates tags.