Jaa


StyleCop Compliant Visual Studio 2010 Code Snippets & Templates

Visual Studio 2010 provides 38 code snippets for the C# developer, while Visual Studio Snippets provides enhanced versions of those base snippets along with 31 additional code snippets in the October 2010 Visual Studio Snippets Release. Within the Visual Studio Snippets collection all of the code snippets are compliant with StyleCop rand static code analysis rules.

Still using Visual Studio 2008?

Visual Studio Snippets was formerly known as CodePlex.Snippets and that project is still available for developers using Visual Studio 2008 and was downloaded by more than 4,700 developers!

Visual Studio Templates, a companion project on CodePlex, provides enhanced versions of the C# language project and item templates used within Visual Studio 2010. Within the October 2010 Visual Studio Templates Release there are templates for core Windows projects such as the Console Application, Windows Forms Application, WPF Application. Silverlight and Silverlight for Windows Phone 7 templates are also provided in this release. Web projects will be supported in the next release.

Let’s take a moment to consider the svm.snippet that creates the following code out of the box:

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)
{

Another commonly used code snippet is the ctor.snippet used for creating a class constructor and the SA1600 and SA1400 rules have been addressed there also.

/// <summary>
/// Initializes a new instance of the <see cref="T:Singleton"/> class.
/// </summary>
public Singleton()
{

}

Visual Studio Snippets also provides the cctor.snippet for the static type constructor. SA1400 is not applicable here because the language specification prohibits the use of access modifiers on static type constructors and the C# compiler will issue error CS0515 results if one is discovered.

/// <summary>
/// Initializes static members of the <see cref="T:Singleton"/> class.
/// </summary>
static Singleton()
{

Another tenant of the Visual Studio Snippets release is parallelism and there are several snippets that assist C# developers with the use of thread based parallelism as well as task based parallelism.

Within the Visual Studio Snippets collection is a version of the lock.snippet that resolves the anti-pattern of locking on this by instead locking on a private field with in the class. In fact there is also a sync.snippet that creates the following code for a private field object that can then be used for thread synchronization and the lock.snippet defaults to locking on the private field created by the sync.snippet. StyleCop also requires documentation headers for fields and as such the sync.snippet complies with this requirement.

/// <summary>
/// Defines a private thread synchronization object for the <see cref="T:Singleton"/> class.
/// </summary>
private object synchronizationObject = new object();

lock.snippet then creates the following code:

lock (this.synchronizationObject)
{

It’s difficult, if not impossible, these days to purchase a computer with a single-core processor and with the available parallelism in the hardware comes a responsibility for developers to, where appropriate, realize this parallelism in the code they write. C# developers today are available to embrace this parallelism either in a coarse grained way, directly using threads, or in a more fine grained way with tasks.

Coarse grained parallelism can be embraced using the thread.snippet which uses a lambda function to create and start a thread.

Thread thread = new Thread(() =>
{

});

thread.Start();

Fine grained parallelism can also be embraced, this time using the task.snippet.

Task.Factory.StartNew(() =>
{

}); 

dec.snippet and inc.snippet provide code snippets to increment or decrement values in a thread-safe way using Interlocked.Decrement and Interlocked.Increment.

Interlocked.Decrement(ref value);

Interlocked.Increment(ref value);

Visual Studio Templates, as mentioned above, is a companion project on CodePlex providing refactored versions of the C# language project and item templates used within Visual Studio 2010. In the following figure you see the C# source generated for a Console Application.

You’ll observe that the source header is provided, required by StyleCop rule SA1633, and automatically includes the user name and the company name. Visual Studio Snippets provides the head.snippet that will insert the same file header too.

StyleCop rule SA1600 requires types and methods to have Xml comments and so the refactored project template provides these comments for the Program class and the Main method.

ConsoleProject

Visual Studio Templates also provides templates for developers writing code for Windows Phone 7 and here is an example project showing the source code generated for the Windows Phone application and that there are no violations of StyleCop rules using this template.

Windows Phone Application

I’ll release new versions of the Visual Studio Snippets and Visual Studio Templates monthly and would encourage you to give me feedback on these collections of snippets and templates either by contacting me through this blog or on the CodePlex sites for the projects. If you’re on Twitter you can also contact me at @dougholland and use the #vssnippets or #vstemplates tags.

Comments

  • Anonymous
    April 12, 2012
    Will MSFT in VS2011 be compliant with StyleCop ?