Friday, May 04, 2007

Cutting and Pasting -- Part 2

Last week I went to one extreme and said that cutting and pasting was not necessarily a good thing. I was talking about using previously written applications as templates for your new application. Let's swing the pendulum to the other end of the extreme and talk about why you should never write certain code more than once and if you do you should slap yourself on the wrist.

Validation routines are things that every application needs. If you accept data from any source you need to verify if that data is correct. Sometimes the verification is quite simple (A9A 9A9 for postal codes) whereas in other cases verification requires database calls or lengthy computational processes involving logarithms and abstract mathematics. If your application is larger than a single screen or a single process, the odds are that there are going to be some common verification methods that can be shared between these areas.

In a previous life, my specialty was crafting routines for verifying a persons Social Insurance Number in any of a dozen different programming languages. I only wrote in once in each language, however, as I (and other people) used the same code in many different places. It varied by language and platform, but each time I used it, I essentially made a call to a utility library that did the validation for me and sent back the results.

I wrote it once and used it thousands of times.

I did not copy and paste it, but I did reuse it and I guess that is where the difference is. Copy and pasting the lines of code that did the function would reuse those lines of code, but it proliferate the number of areas where that code could potentially need to change. If you find yourself writing the same lines of code more than once, you may want to consider putting them in a utility library that you can call from anyplace. It may take some extra time to initially set things up, it saves a lot of time debugging and in maintenance.

No comments: