Things To Do On a Rainy Day – Create Visual Studio Code Snippets

Code Snippets

I’d like to write a few blog posts sprinkled in regarding great features, practices, methodologies, and other goodies that are often overlooked when under the gun to develop a project, but by knowing about them, and occasionally executing them, you can actually save time, frustration, and produce much greater code.

Structured code snippets fall into the category of writing code for the purpose of writing better code. The simple act of writing code that doesn’t directly grow the project you are working on is why code snippets often aren’t created. In an ideal world however, I believe code snippets are an invaluable asset to maintain code consistency and reusability.

Code Snippet Library

Some programmers maintain code snippet libraries that they reuse for all of their projects. There are a ton of advantages to this, but I do want to add a piece of advice. If your company has it’s own library share it amongst all of your developers so everyone begins writing code exactly the same way. This increases the maintainability of code as well as helping in distribute work-load across employees. If you work solo, my advice would be to create your own library. Don’t snag one from some site because 99/100 times I believe most developers don’t use it. If you create the code snippets yourself (and they’re easy to do), then you are much more likely to remember to use them when the time comes (and what’s the point in having it if you don’t remember to use it).

Project-specific Code Snippets

Another great way to use code snippets is when creating a brand new project. Distribute these code snippets to all developers working on this project and you will notice that your code will be much more consistent from the start. Even if you are working by yourself, some times you have a tendency to slightly change or leave out pieces of code (especially at the wee hours in the morning, if you’re like me). Code snippets helps protect against this.

An Example

The following is a short example I use once in a while when writing class libraries.

<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
          <Title>My Class Outline</Title>
          <Author>Nicholas Barger</Author>
          <Description>A basic outline of (nearly) every object class I create.</Description>
        </Header>
        <Snippet>
          <References>
              <Reference>
                  <Assembly>System.Windows.Forms.dll</Assembly>
              </Reference>
          </References>
          <Declarations>
            <Literal>
              <ID>id</ID>
              <ToolTip>Unique ID</ToolTip>
              <Default>int</Default>
            </Literal>
            <Literal>
              <ID>class</ID>
              <ToolTip>Name of the Class</ToolTip>
              <Default>classname</Default>
            </Literal>
          </Declarations>
          <Code Language="CSharp">
              <![CDATA[#region "Internal Variables"

        #endregion

        #region "Properties"

        #endregion

        #region "Constructors"
        public $class$()
        {
            //scratch constructor
        }

        public $class$(int $id$)
        {
            LoadData($id$);
        }

        public void LoadData(int $id$)
        {
            //Load from DAL
        }
        #endregion]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
    

The only thing to note here is that you want to begin writing your code snippet between the …CDATA[ and ] and to create declarations for areas which need to be filled in (denoted by the $id$ or $class$) – these are found in the <Declarations /> section.

There are of course more advanced things you can do with code snippets, but let’s keep it basic for the sake of brevity (homework).

How To Use Code Snippets

Once you’ve created a code snippet just drop it into the appropriate directory, usually something like: Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets or similar. Finally, within Visual Studio, go to an appropriate area such as a new class file in the above example and press Ctrl-K, Ctrl-X; you can now navigate through the dropdown menu of code snippets to find and execute your own. Areas marked in green are your declarations and remind developers to change those values.

Hope this helps and gives you something to do before that next big project, or on a slow rainy day!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s