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:
Which is real handy, but what if you wanted to include a comment to surround that piece of code?
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#.
- 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.
- 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
- C:\Users\yournamehere\Documents\Visual Studio 2010\Code Snippets\Visual C#
- C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#