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!
Some other articles
Q101 - Programming Principles What are callbacks? Why use them? An example in Python Remember to ask the question before you listen for the answer Hungarian, Camel, Snake and Kebab - variable naming conventions Learning to program - what are algorithms and design patterns? Writing the perfect program in Tcl? Rooms available tonight - how to code an algorithm from first principles We not only teach PHP and Python - we teach good PHP and Python Practice! Test Driven Development in Python - Customer Comes First Seamless, integrated IT - we have a long way to go! Web and console - same principle, same code - Ruby example Lesson 1 in programing - write clean, reuseable and maintainable tidy code Storing your intermediate data - what format should you you choose? From Structured to Object Oriented Programming. Object oriented or structured - a comparison in Python. Also writing clean regular expressions Some terms used in programming (Biased towards Python) Dark mornings, dog update, and Python and Lua courses before Christmas What order are operations performed in, in a Perl expression? Stepping stones - early coding, and writing re-usable code quickly Coding efficiency - do not repeat yourself! An introduction to file handling in programs - buffering, standard in and out, and file handles Looking up a value by key - associative arrays / Hashes / Dictionaries Program for reliability and efficiency - do not duplicate, but rather share and re-use Easy - but for whom? Improving your function calls (APIs) - General and PHP And and Or illustrated by locks This article The music of the stock market Variable names like i and j - why? Planning! Learning to write high quality code in Lua Where do I start when writing a program? Pre and post increment - the ++ operator I have not programmed before, and need to learn P209 - Subroutines in Perl Learning to use existing classes in Perl Perl functions such as chop change their input parameters Separating groups of variables into namespaces Passing a variable number of parameters in to a function / method Fresh Perl Teaching Examples - part 2 of 3 This article Efficient calls to subs in Perl - avoid duplication, gain speed Romeo and Julie Seven new intermediate Perl examples Daisy the Cow and a Pint of Ginger Beer Perl - Subs, Chop v Chomp, => v , Global - Tcl, PHP, Python Calling procs in Tcl and how it compares to Perl Returning multiple values from a function (Perl, PHP, Python) A better alternative to cutting and pasting code Perl - $_ and @_ Do not duplicate your code Changing @INC - where Perl loads its modules Packages in packages in Perl Where do Perl modules load from Call by name v call by value Variable Scope P204 - Conditionals and Loops Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) Showing what programming errors look like - web site pitfall Flowchart to program - learning to program with Well House Does a for loop evaluate its end condition once, or on every iteration? Increment operators for counting - Perl, PHP, C and others Are you learning Perl? Some more examples for you! For loop - checked once, or evety time? Ruby v Perl comparison and contrast This article Ternary operators alternatives - Perl and Lua lazy operators Equality and looks like tests - Perl Saying NOT in Perl, PHP, Python, Lua ... Learning to program in Perl Decisions - small ones, or big ones? Lexical v Arithemetic testing, Bash and Perl Smart English Output - via PHP and Perl ? : operator -> , >= and => in Perl Wimbledon Neck