Software: VS 2005 Code Snippets
As an engineer my first reaction to seeing Microsoft’s code snippets was....Why? I figure that if some piece of code was used more then once, then it needed to go into either a function, method, or possibly a new class. The whole point of encapsulation is to make sure that if the code snippet has a bug in it or needs enhancement...you only need to change it in one place. But if you stop thinking of the snippet as a code snippet but instead as a template snippet, it then makes sense. I definitely see the value of having a quick way to create common language structures, such as switch statements, loops, classes, etc... That ability will save a lot of time both in the initial phase when the code is being written and the testing phase, when the engineer makes sure the code works. (A good way to eliminate those errant semi-colons.)
So even though this engineer didn’t immediately see the value of the code snippet, this marketing person did. I’ve watched Dan use the code snippets during developer demonstrations to speed up sample building. Why retype code that you’ve already demonstrated? Since I have my first public presentation next week, I’ve been busy getting ready. And that includes being able to build the sample code that I’m demonstrating. I don’t like to use technology I don’t understand so I’ve been trying to figure out how code snippets work.
My first question is: “Where do my code snippets live?” After a little experimentation I discovered that the snippets can be dragged into and out of the Toolbox. Cool. But when I closed that project and opened another one, my snippets weren’t in the new project. Not cool. I’m doing something wrong. The reason that I wasn’t seeing them in my second project was because Toolbox works differently these days. Tools are only available if the file opened in the work area can use the tool. To see my snippets I needed to have a .cs file active. To see the GUI tools I needed a Windows Form page (.resx). I don’t like it but then I’m a little old fashion and a little set in my ways, so change can sometimes be hard.
So where are my code snippets stored? I eventually found that my snippets are stored in “\Documents and Settings\%Current User%\Local Settings\Application Data\Microsoft\VisualStudio\8.0\toolbox.tbd”. And that what I am calling a snippet is actually not a snippet. I suspect from the documentation that it’s a custom text control. See ”Visual Studio General Tab, Toolbox”.
So what are code snippets? See ”Visual Studio How to: Manage Code Snippets”. These are a different beast then the custom text controls in Toolbox. Since they are integrated with IntelliSense you can access them directly from your code. Read “Creating and Using IntelliSense Code Snippets” as a starting point for getting a better understanding of this new technology.
For my current project I’m going to stick to the custom text controls but I do think it’s worth looking into Code Snippets.