Limit Copy-and-Paste Operations
There are two types of copy-and-paste:
- You just want to copy a routine "as is," without changing it. (Sounds innocent enough.)
- You want to make modifications to the code.
Both operations are problematic and can end up changing more than originally intended.
It’s easy to copy-and-paste when you’re in a hurry. You’ve used the code before, and you know it works! The problem with copy-and-paste is that variations of the code take on a life of their own. Routines rarely stay pristine; they begin to acquire small modifications. When you need to change something for all routines, it becomes a three-step process:
- Locate all the routines.
- Change the ones that are close to the original.
- Examine and change the routines with variations.
In essence, you create many nonstandard versions of the same code.
This situation really hit me in the face when we needed to change settings on our SMTP server for a firewall installation. We had more than 20 routines that sent email—most using basically the same parameters. Some of the routines had subtle differences that had to be reviewed carefully and adjustments made before we could change all of them to a single library routine.
Let me suggest two major approaches:
- Whenever possible, use templates with replaceable parameters.
- Develop object-oriented classes to utilize common routines.
Visual Studio 2005 includes a facility called code snippets. Code snippets are pieces of code used to insert text into Visual Studio. A snippet may or may not have a replacement string. After right-clicking in the Code Editor window of Visual Studio 2005 and navigating to my personal snippets, the prompt looks as shown in Figure 2. Figure 3 shows the full picture. After inserting my code snippet, the text looks as shown in Figure 4.
If the code snippet has a shortcut assigned to it, just enter the shortcut name, press the Tab key, and voilà—it appears. For this shortcut, I assigned "propdata" as the shortcut name. This is much more fun than navigating a context menu three levels deep.
Notice how the text is poised for replacement in the inserted snippet. In this example (taken from an exercise from Doing Objects in Visual Basic 2005 by Deborah Kurata), I would simply do the first replacement of the backing variable, press Tab, enter the data type, Tab, and enter the property name. All of the other variable positions would be filled in.
Just look up "code snippets" in Visual Studio 2005, and you’ll find a host of articles to acclimate you to the process. MSDN has a handy Code Snippet Editor created by Bill McCarthy, a developer and Microsoft MVP from Australia, that prevents you from having to translate pieces of code into XML snippet format. The Code Snippet Editor helps you to create variables effortlessly and create documentation for your snippets without writing geeky XML.
By the way, who says you have to "roll your own"? You can save a lot of work by searching helpful websites. Of course, just as when using macros, proceed with caution and examine the code you’re copying.
A second idea for reducing copy-and-paste is the more object-oriented way of developing classes for common pieces of code. Initially this approach takes more generalization and development, but you reap the benefits later.