Code Snippets

The only thing I like more than breaking software is leaving a note that I broke the software. For example, when I first took over testing code snippets, I would attempt to break the UI by doing all kinds of unexpected things, like trying to type "Sara wuz here" in place of a code snippet name. Most of my attempts were unsuccessful, as you would expect, but eventually I did find a series of keyboard shortcuts that led to a crash.

The cool thing about software testing is that once you find one bug, there’s usually a collection of bugs nearby. All you have to do is know how to find the bug pattern. Expanding on this series of UI interactions, I ended up finding at least three crashes that day, all with "Sara wuz here" in the reproduction steps and in the screen shots for the developer to enjoy.

Fortunately, it was a Friday afternoon, so I had to avoid the developer for only a few hours. =D

Using Code Snippets

These next tips walk you through inserting and managing code snippets.

Tip 2.38: You can use Ctrl+K, Ctrl+X to insert a code snippet

The keyboard shortcut to insert a code snippet is Ctrl+K, Ctrl+X. It is bound to the command Edit.InsertSnippet, in case your mileage varies, depending on your configuration settings.

I’m going to use C# for this tip, but this tip applies to all languages that support code snippets. When I invoke the Edit.InsertSnippet command, the code snippet insertion UI pops up. The real tip here is that the snippet picker allows for type-ahead selection. Note how I started typing "#re" on the line. (Oh, the fun I had testing this . . . but I digress.)

Tip 2.38: You can use Ctrl+K, Ctrl+X to insert a code snippet

Additional keystrokes:

  • You can hit Tab to autocomplete the word. If the word happens to be the code snippet (and not a folder), pressing Tab will insert it.

  • You can also hit Shift+Tab to navigate back to the previous word (my contribution to the insertion UI).

Tip 2.39: You can insert a code snippet via its shortcut keyword

Code snippets have the support to be given a "shortcut," usually an abbreviated version of the code snippet name that you can type into the editor and hit Tab to insert.

To insert, simply type in the name of the snippet—for example, for—and then hit Tab. Note that if statement completion is open, you’ll have to hit Tab twice to insert the snippet.

In both Visual Studio 2005 and 2008, you’ll be able to see C# Code Snippet shortcuts in the statement completion window. The following screen shot is the for snippet displayed within the statement completion window. Note the snippet icon to the left.

Tip 2.39: You can insert a code snippet via its shortcut keyword

In Visual Studio 2008, you won’t see Visual Basic snippets in the statement completion window, but you will see a note in the ToolTip when you can hit Tab twice to insert the corresponding snippet.

Tip 2.39: You can insert a code snippet via its shortcut keyword

Tip 2.40: You can insert a snippet by pressing Tab Tab

I’m not a C# developer. I spent the majority of my time writing code in Visual Basic during my software testing days. Whenever I need to use C#, I heavily rely upon code snippets because I don’t recall the syntax off the top of my head.

When in the appropriate place in the editor, you can type in the keyboard shortcut of a snippet, like for.

Sara Aside

In this state, you can simply press Tab twice to insert the snippet.

Why twice? Press it once to autocomplete statement completion. If you just type f for "for", and for is highlighted, you can just press Tab once to complete the word for. Press it twice to generate the snippet, since the cursor will be at the end of the word for in the editor.

Sara Aside

And the moral of the story is "<snippet> Tab Tab."

Tip 2.41: How to browse code snippets and add new ones

All code snippets are found in the Code Snippets Manager. It is found at Tools–Code Snippets Manager. If you are using the General Development Settings, you can use Ctrl+K, Ctrl+B to bring up the dialog box.

In my opinion, the most useful aspect of this dialog box is to browse through your current snippets to learn what the shortcuts are to quickly insert the snippet into the editor.

Additionally, this is where you add (a directory of snippets) and import (a single snippet or multiple-selected snippets to a specified folder via the Import Code Snippet dialog box).

Tip 2.41: How to browse code snippets and add new ones

You should always check to see what type of snippets you are browsing by looking at the Language combo box found at the top of the dialog box. The Code Snippets Manager Language combo box is an MRU list, meaning that the last set of snippets you looked at (let’s say XML) will come up the next time you bring up the dialog box.

Tip 2.42: How to change default values and variables in a code snippet

When you insert a code snippet, the editor highlights the fields (variables, values, and so on) you can modify depending on how the code snippet was written. The idea is you modify the contents of the field and then press Tab to navigate to the next field. When you press Tab, that particular field is updated throughout the entire snippet. You can also navigate outside the field via the arrow keys to invoke the update.

Tip 2.42: How to change default values and variables in a code snippet

If you press Enter, however, you will be committing both the current change (if any) and the entire snippet, meaning you can’t use undo to get back to those highlighted fields. You would have to use your favorite refactoring method to make any additional updates.

Tip 2.43: How to share code snippets with your team

To share code snippets among others on your team:

  1. Go to Tools–Code Snippets Manager, press the Add button, and type in the UNC share name.

  2. Go to Tools–Import And Export Settings, Export, and choose to export just the Code Snippets Locations to a file.

  3. Send out that .vssettings file to those on your team. They can go to Tools–Import And Export Settings and choose Import to retrieve it.

    Sara Aside

Tip 2.44: How to insert a code snippet around a block of code

C# and XML support the Edit.SurroundWith command that will insert the desired snippet around the selected code (whether it is just a selected word, selected line, or entire function). The keyboard shortcut is Ctrl+K, Ctrl+S.

For example, the following illustration shows how to use the Surround With feature to insert a for loop around an existing for loop.

Tip 2.44: How to insert a code snippet around a block of code

The result is a for loop inside a for loop, as shown here:

Tip 2.44: How to insert a code snippet around a block of code
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.52.203