Snippin' Snippets

I recently worked on a series of Silverlight & WPF projects. Like most projects, I had to to follow a series of strict coding conventions.

The code now looks clean and persistent, but implementing new features has become a time-consuming process. To give an example:


private const string IdPropertyName "Id";
private long _Id;
public long Id 
{
  get
  {
    return _Id;
  }
  set
  {
    if (_Id == value)
    { 
      return;
    }
    _Id = value;
    RaisePropertyChanged(IdPropertyName);
  }
}

So each property that had to raise the PropertyChangedEvent had to be implemented as the snippet above.
I actually love this way of working, but defining new properties quickly became a time-consuming, highly annoying copy-paste process.
And since my PM was constantly harassing me for not reaching my Scrum-targets, I started looking for a solution.

And as the title of this blog-post suggests, I stumbled upon custom Visual Studio Snippets.

For those of you who don’t know snippets: Shame on you ! Snippets are those nifty little code fragments Visual Studio inserts when you type certain keywords.
If you Type the ‘foreach’ keyword and press tab afterwards, Visual Studio will prepare a foreach statement for you.
These snippets are actually parsed Xml-files; which you can adapt, delete and create to suit your own needs.

This is the snippet I created for my problem described above:

I'm not going to explain this snippet in detail. Just add it to the following location C:\Users%%USER%%\Documents\Visual Studio 2012\Code Snippets\Visual C#\My Code Snippets and test it out. It's all pretty straight-forward.

Offcourse you will have to replace the username, the visual studio version & code language; but I hope you get the general idea :). Save the file with a .snippet extension.

No need to restart visual studio to see this in action. Just type depprop and hit tab to see the magic !

Be sure to check out this codeplex project. It's a Snippet editor :) !