Using Code Snippets: The real deal

So your colleagues send out an email requesting information on how you manage code snippets and I thought that my colleagues were talking about code snippets.  Silly me, just like when someone asks me for a Word Template, I create and send a Word Template with the .DOTX extension.  Of course that isn’t what people are asking for, so like templates, my teammates were asking for how to manage snippets of code.

 

The real deal on code snippets is this:

You can easily make code snippets that can function automatically, like when you type in the word: “if” and then hit tab-tab in Visual Studio it will create an “if” statement:

image

Which is real handy, but what if you wanted to include a comment to surround that piece of code?

image

Here is how you do it:

For Code Snippets I usually use the snippet files and show the way to create snippets by copying an existing code snippet, discussing the way to create a “surround” with and where to place the code snippets.

I find that using Code Snippets is an easy way to reuse my comments and code for examples.  To show people how to use code snippets (with my audience I have to assume that they have never done any programming, so apologies if this seems somewhat simplistic).

This will work for Visual Basic and ASP.NET(code behind), but not C, CPP nor F#.

  1. Example process: Usually have my comments that I use to clarify my code inside of a snippet file that has the “Tab-Tab” functionality set.  It is easy to do, here is the XML that you could use to create code snippets:
    • First start with an existing code snippet, for instance I start with an if statement.
  2. Snippet code with will work when I type in Comments and then press tab-tab, this code snippet will also do a surround with the flower type of comments, which for my audience allows them to recognize that these are my comments and are for demo purposes only.
    • The surround doesn’t quite indent correctly initially, so if you know how to fix that let me know, but the “Tab-Tab” insertion works nicely
  3. This is easy to manage as you place your comments in the Visual C# Snippets under Visual Studio 2010, by default you place it here (for C#, VB has it’s own folder):
    • C:\Users\yournamehere\Documents\Visual Studio 2010\Code Snippets\Visual C#
  4. Here is the example code, I used the “if” snippet from the default installation path for snippets:
    • C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#

 

Code Snippet

  1. <?xml version="1.0" encoding="utf-8" ?>
  2.   <CodeSnippetsxmlns="https://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3.   <CodeSnippet Format="1.0.0">
  4.     <Header>
  5.       <Title>Comments</Title>
  6.       <Shortcut>Comments</Shortcut>
  7.       <Description>o</Description>
  8.       <Author>Sam Stokes</Author>
  9.       <SnippetTypes>
  10.         <SnippetType>Expansion</SnippetType>
  11.         <SnippetType>SurroundsWith</SnippetType>
  12.       </SnippetTypes>
  13.     </Header>
  14.     <Snippet>
  15.       <Declarations>
  16.         <Literal>
  17.           <ID>expression</ID>
  18.           <ToolTip>Blah Blah Comment</ToolTip>
  19.           <Default>true</Default>
  20.         </Literal>
  21.       </Declarations>
  22.       <Code Language="csharp">
  23.         <![CDATA[
  24.             /*************Speaker Comments*****************
  25.             /* Comment Blah Blah Blah
  26.             /*********************************************/
  27.  
  28.  
  29.         $selected$ $end$
  30.     /**************End Speaker Comments *********************/]]>
  31.       </Code>
  32.     </Snippet>
  33.   </CodeSnippet>
  34. </CodeSnippets>