Visual Studio 2010–New and little-know features–Part 4–Code Snippets

SNAGHTML14b76cTable of Contents for this series.

Code Snippets

Code Snippets have been around since Visual Studio 2005 so I’m amazed when I show the functionality to developers that have been using Visual Studio for years and they tell me they have never seen it.

Code Snippets are feature of Visual Studio that allow you to add small pieces of code to your project using shortcuts.  This allows you to add common code elements like Property Declarations or Class Definitions, basically things that you have to type all the time to make your programs run but are pure drudgery.

So let’s say you want to create a new property on your class with a backing private variable.  This code is exactly like the thousands of other properties you’ve created over the years.  You could copy one of the other variable declaration/property declaration statement pairs that already exist in your project and paste it in then go and make changes to the names, types, etc. to make is unique.  Of course at some time you have probably done this and then forgotten to go back and make the changes thus causing your program to fail compilation, if you are lucky.  If you aren’t lucky, you changed the name but left the old Type or failed to change the Getter or Setter of the Property and your app behaves incorrectly at runtime (harder to find).

To use the Code Snippets, all you have to do is begin typing the shorthand code and IntelliSense will show the code snippets available along with any other appropriate tokens.

SNAGHTML2f0ec0

I can then use the arrow keys to navigate the list until I find the one that gives me a property and backing field.  That snippet’s shorthand code is propfull.  I select that one from the list and then press the TAB key twice to trigger snippet expansion.

SNAGHTML32758d

Once expansion completes for this snippet, you can see that the variable’s type field (the int) has a blue background an has the focus.  You can immediately begin typing in the new Type for this variable.  When you do, the snippet feature will change the type of the Property declaration to match.  Once you change the Variable type you can press the TAB key to move to the Variable Name field.  When you change the name, the statements in the body of the Property that refer to the Variable’s name will also change.  Neat, huh?

SNAGHTML3bdc3b

There are all kinds of snippets that ship in the box with Visual Studio.  To see the list all you have to do is go to the Tools | Code Snippets Manager… menu item.

SNAGHTML3dd4ad

This will bring up the Code Snippets Manager dialog where you can peruse snippets until you collapse from the sheer joy. The code snippets are first categorized by Language.  There are snippets for HTML, T-SQL, JScript, Visual Basic, C#, SQL and XML.  Each language has sub-groupings.

SNAGHTML437c80

You can also create your own snippets for commonly used pieces of code and add them to the My Code Snippets group.

SNAGHTML432961

To wrap up, Code Snippets are great for taking code that is painfully common and mind-numbingly error-prone and turns it into an activity that saves not only keystrokes, but brain cells as well.  I highly encourage you to look at the snippets that are shipped with Visual Studio. If you are interested in creating your own for you or your entire team, look at the structure of the .snippet files that back the existing snippets.

Here’s a link to the root of the MSDN Library documentation on Code Snippets.

Happy Snippeting!

 

Table of Contents for this series.