Snippets

 

Have you ever seen a guy do a programming demo, type in a few letters and then suddenly a whole bunch of code appers and yo are caught spellbound by his wizardry? There is no magic involved, what you just saw, was probably something called a Snippet.

Snippets are small bits of code that makes programming faster. Snippets are used often by developers during their presentations/demos of new programming technology, who either wish to save time, keep their hold on their already sleepy audience’s attention. People like these guys. 🙂

Snippets can also be used by developers. Some developers are sometimes given the task of adding complex comments to their code. Like code comments at the top of a class. With the use of snippets, this task can be eased and done faster.

Truth of the matter is, snippets are also files, that you can make yourself. These are files with a ".snippet" extension and it follows rudimentary XML format.Here is an example of what a snippet file looks like.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>SnippetDemo</Title>
      <Author></Author>
      <Description></Description>
      <HelpUrl></HelpUrl>
      <SnippetTypes />
      <Keywords />
      <Shortcut>snip1</Shortcut>
    </Header>
    <Snippet>
      <References />
      <Imports />
      <Declarations />

<Code Language="CSharp" Delimiter="$">

<![CDATA[//==================================
// Class Name :    ClassNameGoesHere
// Made By    :    Eduardo Lorenzo
//==================================]]>

</Code>

    </Snippet>
  </CodeSnippet>
</CodeSnippets>

You can edit this file any way you want. You can use Microsoft’s NotePad, or another software that is specially made to make and edit snippets like the Snippet Editor.

You will notice that a snippet file follows the basic XML format. And there are some important tags that you should know about:

1. Title Tag – This tag holds the name or title of your snippet.

2. Shortcut tag – this tag contains the shortcut that you are going to type into your IDE to call your snippet.

3. Code Tag – contains the actual code that will appear on your IDE. Notice also that the CODE tag has a property called Language. This should contain, as its value, the language that you will be using. Snippets are non-transferable. 🙂

How to include the snippet in your IDE.

It’s actually quite simple. But first you need to make sure of a couple of things:

1. That you already have a .snippet file and that it follows the correct syntax.

2. All your snippet files are located on the same folder.

Then all you need to do is:

In your IDE (Visual Studio Team System 2008 in my case), find and click on the Tools command in your menu then click Code Snippets manager.

image

This will then bring up the Code Manager Dialog.

image

In the Code Snippets Dialog, choose the correct language that you will use from the DropDownList labelled Language, which in my case is C#.

image

Then click the Add button and browse to the folder that contains your snippets. For this example, I have my snippets in a folder named mySnippets on my Desktop. Click OK.

You will now notice that your snippet folder has successfully been included in the Code Snippets Manager.

image

Click OK and you’re done! You have added your snippet to your IDE! The snippet you have just added will now be available to all projects that you edit from now on.

To use the snippet, in your Code Editor window, whenever you need to insert your snippet, all you will need to do is type the shortcut and hit the TAB key twice. Much like a double-click, and prepare to be amazed..

Abra…

image 

Cadabra!

image

One more thing:

I have had the chance to talk to developers who have moved from VB to C# and often I hear them complain "I’m OK with C# but I think VB intellisense is better. When I type in try then hit enter, it automatically completes the try.. catch construct!"

Well, try this: In C#, type in try then hit the TAB key twice and see what happens. There you go! Now you have nothing to complain about!

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Snippets

  1. Jennifer says:

    Keep up the Tech Blogging! :D

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s