Repetitive Tasks or Variations of A Task
One of the most harmful forms of copy-and-paste programming occurs in code that performs a repetitive task, or variations of the same basic task depending on some variable. Each instance is copied from above and pasted in again, with minor modifications. Harmful effects include:
- The copy and paste approach often leads to large methods (a bad code smell).
- Each instance creates a code duplicate, with all the problems discussed in prior sections, but with a much greater scope. Scores of duplications are common; hundreds are possible. Bug fixes, in particular, become very difficult and costly in such code.
- Such code also suffers from significant readability issues, due to the difficulty of discerning exactly what differs between each repetition. This has a direct impact on the risks and costs of revising the code.
- The procedural programming model strongly discourages the copy-and-paste approach to repetitive tasks. Under a procedural model, a preferred approach to repetitive tasks is to create a function or subroutine that performs a single pass through the task; this subroutine is then called by the parent routine, either repetitively or better yet, with some form of looping structure. Such code is termed "well decomposed", and is recommended as being easier to read and more readily extensible.
- The general rule of thumb applicable to this case is "don't repeat yourself".
Famous quotes containing the words task, variations, repetitive and/or tasks:
“The task he undertakes
Is numbering sands and drinking oceans dry.”
—William Shakespeare (15641616)
“I may be able to spot arrowheads on the desert but a refrigerator is a jungle in which I am easily lost. My wife, however, will unerringly point out that the cheese or the leftover roast is hiding right in front of my eyes. Hundreds of such experiences convince me that men and women often inhabit quite different visual worlds. These are differences which cannot be attributed to variations in visual acuity. Man and women simply have learned to use their eyes in very different ways.”
—Edward T. Hall (b. 1914)
“Psychobabble is ... a set of repetitive verbal formalities that kills off the very spontaneity, candor, and understanding it pretends to promote. Its an idiom that reduces psychological insight to a collection of standardized observations, that provides a frozen lexicon to deal with an infinite variety of problems.”
—Richard Dean Rosen (b. 1949)
“We are all adult learners. Most of us have learned a good deal more out of school than in it. We have learned from our families, our work, our friends. We have learned from problems resolved and tasks achieved but also from mistakes confronted and illusions unmasked. . . . Some of what we have learned is trivial: some has changed our lives forever.”
—Laurent A. Daloz (20th century)