Alright, I’ll admit it. I’m probably the second to last VS developer to figure out how snippets really work. I’m writing this post in the hopes that the last developer finds this…

Code Snippets is a new feature in VS 2005. I’ve been using them via the somewhat laborious Right Click… Insert Code Snippet… scroll to the appropriate snippet, double-click, and it’s inserted. Then you fill in some information. (For instance, if you insert a “for” snippet, you provide the name of the iteration variable and the length by tabbing between the fields. Press Enter and you jump to the insertion point in the loop where you can start coding.) You can put this to great use both for repetitive coding tasks like properties, events, etc. as well as during demos. (For everyone who does technical presentations, I can read faster than you can type – especially if you take into account the inevitable typos. I’d rather watch you debug through the code and explain it rather than watching you fumble your way through Intellisense.) The problem, especially for repetitive coding tasks, is the time it takes to go through the dance – Right-click… Insert Code Snippet… etc. Yes, there’s a keyboard shortcut (Ctrl-K, X), but it’s still time-consuming to find what you want in the list. Then, I noticed the “shortcut” defined for snippets. After some quick digging, I found this document that outlines the 5 different ways to insert code snippets. The best way, once you know the shortcut, is [shortcut]-TAB-TAB to insert the snippet. (i.e. for-TAB-TAB) This is fantastic for the snippets you use frequently.

I also like the simple format for snippets. You define variables for replacement by enclosing them in “$”. For example $classname$. If you repeat the same variable twice, it automatically updates when you change the first instance. You can also specify default values and prompt text. This means that it’s super simple to create your own snippets for common tasks. Roland Weigelt (creator of the AWESOME VS addin, GhostDoc) has crafted up code snippets for an NUnit test, events, properties, and debugger break. I created my own property snippet based on Roland’s for the way I prefer to define properties. The actual code for the read/write property is:

private $type$ m_$property$;

public $type$ $property$ {
  get { return m_$property$;}
  set { m_$property$ = value;}

It doesn’t take a proverbial rocket scientist to figure out how to code your own snippets.

Once you have your snippet created, simply drop it into %My Documents%\Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets and it will immediately appear in VS 2005. You can also add new locations to search for code snippets via the Code Snippets Manager (Tools… Code Snippets Manager… or CTRL-K, CTRL-B.)

Code Snipets are a fantastic addition to make a developer’s life more productive. You’ll definitely be seeing me using code snippets in upcoming presentations. Although I’m a reasonably fast touch-typist, one’s ability to type varies inversely with the number of people wathcing.