Thursday, February 28, 2008

Using Code Snippets

I love code snippets. They save lot of time and effort, and if used properly, will improve productivity. As explained in MSDN, you can use code snippets to type a short alias, and then expand it into a common programming construct. The code snippet list can be activated by typing CTRL + K, then X (while holding the CTRL key). Pressing TAB or ENTER after typing the name of the code snippet will insert the selected snippet at the cursor position. Using the code snippets through IntelliSense auto-completion method is more easy. All you need to do is to type the shortcut for the snippet that you want to add to your code, and press TAB, TAB. You can also use the Insert Snippet context menu in Visual Studio IDE, to insert snippets at the current cursor position.
I always felt that Surround-with Code Snippets are really nice. Using this, we can surround a block of code with a snippet, such as an exception handler. I have seen guys not writing proper exception handling blocks, because of laziness. Sometimes, we may even miss the fact that there is a special case such as a potential exception in the already written code. The surround-with code snippets are handy in such situations. To insert surround-with code snippets through keyboard shortcut, select the text to surround in the code editor and type CTRL + K, then S. Now select the required snippet from the list using mouse or by typing the name of the code snippet and press TAB or ENTER to insert the snippet. You can also insert snippets through the Edit menu by selecting Edit -> IntelliSense -> Surround With. The third method is to insert snippets by selecting the Surround With command from the context menu in Visual Studio IDE.
Few links are given below, for further reference.
Also, it will be a good idea to check John Sheehan's blog - http://john-sheehan.com/blog/ for some tips and tricks.

No comments: