LANGUAGES: C# | VB.NET
ASP.NET VERSIONS: 2.0
Great Solutions for Source Code Snippet Libraries
By Ken McNamee
Several times per day, I find myself leafing through books or searching the Internet for an article or blog entry describing how to solve some problem, or what the syntax is for iterating through the nodes of an XmlDocument, or some such thing. You see, my brain is pretty much full these days and my memory banks seem to get garbage-collected every night so any knowledge that doesn t get used very often is not going to be retained for long. To counter this dilemma I used to collect a large number of Internet bookmarks and code snippet files to help fill that knowledge gap. However, I suspect that other developers share this experience because I recently discovered several far better code snippet solutions than mine.
Visual Studio .NET 2003 Tools
Visual Studio .NET 2003 introduced a toolbar tab called the Clipboard Ring, which simply kept a copy of the last 15 pieces of text that were copied to the Clipboard. This feature can be very convenient if I need to use the same code snippets over and over again in the same development session. However, the major problem with it is that the Clipboard items are not persisted between development sessions and I m out of luck if the code snippet I need is 16 items back in the history, because it s no longer in the Clipboard Ring.
So the Clipboard Ring is a tool I sometimes find useful, but not at all as a good code snippet library solution which is what I m really after. That s where CodeKeep (http://www.codekeep.net) and CodeXchange (http://www.codexchange.net) come in. These two free, community-based tools are actually Visual Studio .NET 2003 add-ins that provide access to online source code libraries to which anyone can contribute. It s a great concept, and one that I encourage everyone to try out because the more developers that get involved, the stronger and more diverse the source code library will become.
CodeKeep is very simple to use. All you need to do is create an online account with CodeKeep, install the free Visual Studio .NET 2003 add-in, and log in. From within VS.NET you can bring up the CodeKeep Snippet Search dialog box, which will allow you to look at snippets that you have added yourself or search for snippets that other developers have added. In the example shown in Figure 1 you can see I ve searched for mail because I keep forgetting the syntax for sending an e-mail from within ASP.NET. As it turns out, the Send Mail snippet is exactly what I m looking for.
Figure 1: The CodeKeep snippet search dialog box allows the developer to browse through the snippets they ve added, as well as search for snippets that others have added.
However, I also see that someone uploaded a snippet on how to send e-mail programmatically through my Yahoo account. I didn t even know this was possible, so I ll try to remember that trick for later. Who am I kidding? I ll forget about that snippet about three seconds after I finish this article which is why I wish CodeKeep had the ability to bookmark a snippet so I don t have to search for it again later. As a bookmark workaround, I can copy the snippet and store it as my own in my online CodeKeep library. In fact, I can do this with any of my own code snippets so that I no longer need to worry about copying things into text files or saving bookmarks to Internet sites that might not be online the next time I need them.
CodeXchange works very similarly to CodeKeep, with the main difference between them being the contents of their respective snippets database. CodeXchange might be a little more polished in terms of the UI and features, but the difference is minor. One feature I do like is the ability for developers to comment on and rate snippets. I have both add-ins installed, and find that I use both, so I get a broad coverage of any topic in which I m interested.
Visual Studio 2005 Tools
While CodeKeep and CodeXchange are great, with Visual Studio 2005 you no longer need to resort to third-party solutions because VS 2005 includes a code snippet feature that is even superior to anything available for VS.NET 2003. In VS 2005, code snippets are actually stored as XML files that define not only the text of the snippet, but also provide the capability for dynamically generated sections that you can customize quickly and easily.
Take, for example, the source XML for the VB.NET PropRead snippet shown in Figure 2, which very simply allows the developer to insert a read-only property. However, if you look at the code section, you ll see that it contains such tokens as $PropertyName$, $PropertyType$, and $PrivateVariable$, which map to the Literal and Object nodes in the Declarations section of the XML.
Public ReadOnly Property $PropertyName$()
Figure 2: Code snippets in Visual Studio 2005 are stored as XML files, which define the code and which sections are dynamic.
To use this snippet in your code, all you need to do is right-click the section where you want it to go, select Insert Snippet from the context menu, and you should see a snippet IntelliSense dropdown. In the case of the PropRead snippet, you ll want to navigate to Common Code Patterns, then Properties and Procedures, and then select the Define a ReadOnly Property snippet, which will insert it into your code. You can also hit Ctrl-K, Ctrl-X to get the snippet IntelliSense dropdown.
If that snippet navigation sounds a little bit cumbersome, there is an even easier way to use snippets. If you already know the shortcut keyword, you can simply type it in and hit the Tab key to immediately insert the snippet. As you can see from the snippet in Figure 2, the Shortcut is defined in the Header section and is equal to PropRead for this example. Once inserted, any dynamic sections of the snippet are highlighted in green and you only need to tab through those sections and type over the tokens to apply your own values. With the PropRead snippet, for example, the first token is $PrivateVariable$, and I may type in m_FirstName for a property that will be named FirstName.
While the structure of a snippet file is not that difficult to figure out, you may want to use an editor to create your own snippets. Visual Studio 2005 doesn t have its own editor, but Microsoft does offer the standalone Visual Basic Snippet Editor, which you can see in Figure 3 (and download from http://msdn.microsoft.com/vbasic/downloads/tools/snippeteditor). While the name might imply that it only works with Visual Basic, rest assured that it does provide support in its Options dialog box for C# and J#, as well. It s a simple, yet effective, GUI for customizing all aspects of a snippet file, which you can use yourself or use for an entire development team.
In addition to the Visual Basic Snippet Editor, there is another tool called Snippy that you can download from http://www.gotdotnet.com/codegallery/codegallery.aspx?id=b0813ae7-466a-43c2-b2ad-f87e4ee6bc39. Snippy was also created by someone at Microsoft and is not much different from the VB Snippet Editor.
Figure 3: Visual Studio 2005 may not include a snippet editor, but this free editor you can download from Microsoft makes customizing and creating snippets fast and simple.
I hope I ve given you a taste for how convenient and powerful code snippets can be and how much more productive your development time can become by using them. Keep in mind that snippets are only limited by the fact that they define one continuous source code string. It s possible to create a snippet 1,000 lines long that defines a class with a bunch of properties and methods, and maybe a related collection type for the class that contains special logic for adding objects to it and indexer logic for getting objects out of it all in a type-safe manner. What you choose to do with it is up to you, but the power is there for you to harness and the tools are there to make your job that much easier.
Ken McNamee is a Senior Software Developer with Vertigo Software, Inc., a leading provider of software development and consulting services on the Microsoft platform. Prior to this, he led a team of developers in re-architecting the Home Shopping Network s e-commerce site, http://www.HSN.com, to 100% ASP.NET with C#. Readers can contact him at [email protected]