This week, a post on the Joel on Software forum asked if Copy-Paste can be considered code reuse. This one caught my eye, since I went through an exercise just last week to automate exactly this sort of thing.
I hate copy-paste programming. Generally, if you’re doing this, it means you should refactor that code to encapsulate it and just call the routine where needed. Last week, though, for a variety of really bad reasons, I had to insert a bunch of repetitive code, and I’d already extracted and encapsulated everything I could.
“This calls for Snippets,’ I said.
Have you seen Snippets? They’re the tribbles of Visual Studio development. Microsoft introduced this cute little toolbox where you can keep all your little chunks of magic, and then recall them and drop them into your current context at a moment’s notice. If they’re so cute and tiny and wonderful, then, what could be bad about trib — er, Snippets?
All light-hearted ribbing aside, I think it’s worth pointing out one last time that most of the time you think you need to copy and paste a bunch of times, you probably really need something different. Find a base class to overload, or even use a custom attribute. But every once in a while, you really do need to be able to paste in some code and change it up a little — over, and over, and over. In these cases, Snippets are a pretty nice way to do it.