Main Content

Do not copy and paste code - there are much better ways

Archive - Originally posted on "The Horse's Mouth" - 2009-12-26 09:47:34 - Graham Ellis

If you find yourself using copy and paste to duplicate a piece of code, take a step back and think again. You're doubling your maintenance work, with two separate sets of code to look after and keep in step from that day forwards.

Are you going to give me the "but I have to change one copy a bit" excuse? Please don't ... in identifying which bits need changing, you have identified the parameters for a named block of code ... which will be known as a sub / subroutine / function / macro / method / procedure / command depending on which language you're writing in.

There's an example in [this bit of code] which I wrote yesterday in Perl, looking for Christmas topics within place names (Well - as someone else said ... "the programs on the TV weren't worth watching") ...

And if you want to try the code out, there's a web based derivative [here]. Enjoy!