About us

My photo
Laziness is the mother of efficiency. We believe we are efficient guys!!

Friday, March 4, 2011

Code snippets - Part 1

Code snippets - Part 1

Introduction

In this post, we are going to see what code snippets are, types of code snippets and usage of the snippets in Visual Studio.

A code snippet can be defined as a piece of (of course) compilable code which can be added/inserted into a program by using a shortcut.

Types of Code snippets

We can broadly classify code snippets into two types, based on the providers.

1. Default snippets

provided by Microsoft ported along with Visual Studio

2. Custom snippets
These kind of snippets will be created by end users or downloaded from web. There are good number of web resources where you can download and use the code snippets for free!

We can also classify the snippets based on their nature.

1. Expansion
These are the most known type of snippets. These snippets will just expand into the actual code. No special magics other than this. The below mentioned snippets cw, and mbox are some examples to this type

2. SurroundsWith
These type of snippets will help us to cover a set of existing statements with the code snippet. For example, if you want to cover a bunch of statements under a try catch block, then select the piece of code, right click, select “Surrounds with...” menu option. Ctrl + K, S is the shortcut key. From the available list, select “Try” snippet. Now check the code window. Wow! now your code is guarded with Try catch block!! Some more good examples of this type are “If,For,” and looping statements.

Note : A same snippet can be of both the above types i.e it can expanded also it can surround a set of statements. In general, if a snippet is of SurroundsWith type, it will be mostly an Expansion type also.

In this post, we are going to see all the above four types, how to create our own snippets and web resources where we can download snippets.

Let us begin with a couple of simple code snippets which most of us may know. Below are available with Visual Studio.

cw elaborated to Console.Writeline();
mbox elaborated to global::System.Windows.Forms.MessageBox.Show("Test");

In the above case, cw and mbox are called as the shortcuts of the code snippets.

How to insert a snippet into code?

OK. Now let’s explore how we can insert code snippets into code window. When you are in the code window, use any of the below to add/insert a code snippet into code.

1. From Context menu :

We can use the Insert snippet... menu item available in the context menu of the code window. This will be useful when we are not sure about the shortcut of code snippet.


Just right-click anywhere in the code window (make sure you are not inside a comment!!) and select Insert snippet... menu item in the context menu. This will list you the available snippets groups (for example NetFX30 and Visual C#). Then select the category and you can see the available snippets in the intellisense view. Browse through and select the desire snippet. You can have a description (if available) for the snippet in a tooltip. Now you will have the actual code for the snippet you chose. Always watch the shortcut of the snippets so that it will be easy in future to insert snippets and save your time!


2. Using keyboard shortcuts

If you are a great fan of keyboard or wish to save sometime instead of using the mouse buttons, then go ahead using the keyboard shortcuts Ctrl+K, X . This will show the same list mentioned in the above case. (This key combination can be found in the context menu also :) so not a big deal)


3. Directly typing in the code

Once you are familiar with the snippet shortcuts, then you can right away type them in the code window and intellisense will list you the snippet. Just press the key, Visual studio will bring you the entire code associated with the snippet.


Note : Once the code of the snippet is inserted into the code window, some of the parts of the code will be highlighted in gray and will have the focus. This will help us to enter the required values easily. For example, the MessageBox snippet will place the cursor in the message area so that we can start entering the actual message we want to show. This will be very interesting and helpful for some advanced snippets such as for and foreach.

Advantages of using a code snippet

Before moving further, let us stop and analyse whether code snippets are really useful.

Code snippets help us greatly in

reducing the effort of developers

especially when the same piece (or similar pattern) of code is used in multiple places. Best example will be statements used for logging, for showing messages to user, exception handling, etc

.

improving the code readability

The code generated from using the snippets will be readable and comply to the coding standards (provided if the code snippet is created in such a way).

To be honest, I can see only these two reasons to use code snippets, however, since these are good enough to use the snippets, let’s move further.

In our next part of this post, we can further explore on the below topics.

Creating custom snippets - which will be interesting and sometimes fun.
Samples - sample snippets that we have used earlier and currently.
Snippet editors - some of the editors avaliable in market which help to create professional snippets with less effort.
Snippets available in web - why to struggle unnecessarily when there are plenty of snippets available already in the web for free? so let’s explore them as well.

Till that, just play with some default snippets :) Note : you can use the Code Snippets Manager dialog under Tools menu (Ctrl K, Ctrl B) to check various code snippets available. (If you try further, you can start creating your own snippets also!)

No comments:

Post a Comment