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!
(written 2009-12-26 09:47:34)
Associated topics are indexed under
Q101 - Object Orientation and General technical topics - Programming Principles [2586] And and Or illustrated by locks - (2010-01-17)
[2415] Variable names like i and j - why? - (2009-09-22)
[2327] Planning! - (2009-08-08)
[2310] Learning to write high quality code in Lua - (2009-07-30)
[2228] Where do I start when writing a program? - (2009-06-11)
[2022] Pre and post increment - the ++ operator - (2009-02-03)
[2001] I have not programmed before, and need to learn - (2009-01-19)
P105 - Perl - Repeating Blocks of Code [1607] Learning to program in Perl - (2008-04-11)
P209 - Subroutines in Perl [2069] Efficient calls to subs in Perl - avoid duplication, gain speed - (2009-03-07)
[1921] Romeo and Julie - (2008-12-04)
[1860] Seven new intermediate Perl examples - (2008-10-30)
[1850] Daisy the Cow and a Pint of Ginger Beer - (2008-10-21)
[1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
[1784] Global - Tcl, PHP, Python - (2008-09-03)
[1782] Calling procs in Tcl and how it compares to Perl - (2008-09-02)
[1202] Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24)
[1163] A better alternative to cutting and pasting code - (2007-04-26)
[969] Perl - $_ and @_ - (2006-12-07)
[775] Do not duplicate your code - (2006-06-23)
[588] Changing @INC - where Perl loads its modules - (2006-02-02)
[531] Packages in packages in Perl - (2005-12-16)
[357] Where do Perl modules load from - (2005-06-24)
[308] Call by name v call by value - (2005-05-11)
[96] Variable Scope - (2004-10-22)
Some other Articles
Adding retrospective ALT attributes to IMGOn a short walk from homeWeb site traffic - real users, or just noise?Perl and the Common Gateway Interface - out of fashion but still very useful?Do not copy and paste code - there are much better waysChristmas Day ...Ten years ago, we moved to Melksham SpaThe great thing about snow ....How well do you know Melksham - a quiz for ChristmasScraping content for your own page via PHP