CodePlex.Snippets 4.0 - Visual Studio 2010 Code Snippets for C# Developers

CodePlex.Snippets is a collection of code snippets for C# developers that enhances the core code snippets developers use today to ensure that they conform, where appropriate, to static code analysis and StyleCop rules. CodePlex.Snippets also provides 28 additional code snippets in the beta release to enhance C# developers productivity.

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 CodePlex.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()
{

}

CodePlex.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 CodePlex.Snippets release is parallelism and there are several snippets that assist C# developers with the use of thread based parallelism as well as the new task based parallelism types within the .net framework 4.0.

Within the CodePlex.Snippets beta release 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(() =>
{

}); 

In this blog post we’ve explored only seven of the code snippets from the CodePlex.Snippets beta release and hopefully you can see that these can enhance your productivity as a developer and promote more consistency when developing code as a team. If you’re using static code analysis and StyleCop then the productivity gain will be all the more apparent as you’ll spend considerably less time refactoring code to resolve issues found during code analysis.

I’m planning on releasing the release build of CodePlex.Snippets for Visual Studio 2010 soon and would encourage you to take a look at the library of code snippets and let me know what you think. Over 4,700 developers downloaded the previous version for Visual Studio 2008 and I’m looking forward to hearing from developers about this release.