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, updated 2010-01-06)
Associated topics are indexed under
Q101 - Object Orientation and General technical topics - Programming Principles [3551] Some terms used in programming (Biased towards Python) - (2011-12-12)
[3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
[3456] Stepping stones - early coding, and writing re-usable code quickly - (2011-09-24)
[3026] Coding efficiency - do not repeat yourself! - (2010-11-02)
[2964] An introduction to file handling in programs - buffering, standard in and out, and file handles - (2010-09-21)
[2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
[2878] Program for reliability and efficiency - do not duplicate, but rather share and re-use - (2010-07-19)
[2769] Easy - but for whom? - (2010-05-18)
[2737] Improving your function calls (APIs) - General and PHP - (2010-04-24)
[2586] And and Or illustrated by locks - (2010-01-17)
[2510] The music of the stock market - (2009-11-22)
[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)
P204 - Perl - Conditionals and Loops [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
[3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
[2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
[2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)
[2351] Ternary operators alternatives - Perl and Lua lazy operators - (2009-08-12)
[1727] Equality and looks like tests - Perl - (2008-07-29)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[1607] Learning to program in Perl - (2008-04-11)
[1477] Decisions - small ones, or big ones? - (2007-12-18)
[1468] Lexical v Arithemetic testing, Bash and Perl - (2007-12-11)
[1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
[930] -> , >= and => in Perl - (2006-11-18)
[353] Wimbledon Neck - (2005-06-20)
P209 - Subroutines in Perl [3574] Perl functions such as chop change their input parameters - (2012-01-10)
[3066] Separating groups of variables into namespaces - (2010-11-24)
[2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[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