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.

Comments (4)

  1. Fredrik Mörk says:

    Looks like you got a copy/paste error; the code sample for da.snippet should probably be a Debug.Assert statement (not Trace.Assert)?

  2. dohollan says:

    Hey Fredrick,

    Thanks for catching the copy/paste error which is fixed now 🙂

    – Doug

  3. Axel Rietschin says:

    What happened to http://gotcodesnippets.com and all its content, by the way? The dw and tw snippets in your pack are byte-for-byte equivalents to the snippets I've posted over there back in 2008 (save for the <author> tag…)

  4. dohollan says:

    Hey Axel,

    I'm afraid I wasn't very familiar with that website and don't know where it has gone today. The 82 snippets in the project were authored entirely by me although a snippet resulting in a one line of code, such as those for Debug.WriteLine and Trace.WriteLine, is likely to mimic those written by others.

    Let me know what you think of the other 80 snippets though as I'm planning on releasing new versions of the library each month.

    Regards,

    – Doug