Question

In: Computer Science

It is straightforward to copy-paste code to achieve repetitive actions, what is the downside of such...

It is straightforward to copy-paste code to achieve repetitive actions, what is the downside of such an approach?

Solutions

Expert Solution

Copy-and-paste programming, sometimes referred to as just pasting, is the production of highly repetitive computer programming code, as produced by copy and paste operations. It is primarily a pejorative term; those who use the term are often implying a lack of programming competence. It may also be the result of technology limitations (e.g., an insufficiently expressive development environment) as subroutines or libraries would normally be used instead. However, there are occasions when copy-and-paste programming is considered acceptable or necessary, such as for boilerplate, loop unrolling (when not supported automatically by the compiler), or certain programming idioms, and it is supported by some source code editors in the form of snippets.

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".

Related Solutions

R studio questions Write up your answers and paste the R code Copy and paste all...
R studio questions Write up your answers and paste the R code Copy and paste all plots generated. First create a sample drawn from a normal random variable. R has many distributions for which you can get probabilities and draw random numbers. We are going to use the normal. Go to help in R and type in rnorm. You will see a write up for functions associated with the normal distribution. dnorm is the density; pnorm is the probability distribution...
Having a difficult time writing this code up. ( JAVA based ) Will Copy and paste...
Having a difficult time writing this code up. ( JAVA based ) Will Copy and paste the whole solution I was left with. Thank you in advance ! Lab 5 – Class Statistics Write a program which will uses the file Lab5Data.txt containing student names and the points they had earned at the end of the class. The program should use one or more arrays to create a report with the following information: -A table containing the student name, total...
Please don't copy and paste from other website. Outline the key fiscal and monetary policy actions,...
Please don't copy and paste from other website. Outline the key fiscal and monetary policy actions, respectively, of New Zealand against the COVID-19 shocks(already outlined below) use appropriate macroeconomic models and theories covered in this course to explain their expected impacts. Discuss any limitations of these policy actions. (~600 words) TIPS: ⮚ If there are many policy actions, you could list all of them but focus on the key ones for discussion. ⮚ The government may take multiple actions (e.g....
4. Create a new project AccountPolymorphism and copy and paste the code from the attached AccountPolymorphism.cpp...
4. Create a new project AccountPolymorphism and copy and paste the code from the attached AccountPolymorphism.cpp file. Compile and run. You will see that it is not showing output for Base class pointer to base class object data, not showing derived class output completely(missing word "Saving" in the output) and not showing any output for Base class pointer to derived class object(Saving). Fix these issue and submit the output. // AccountPolymorphism.cpp : This file contains the 'main' function. Program execution...
Directly copy the source code and paste into the Word file. Screenshot of running result must...
Directly copy the source code and paste into the Word file. Screenshot of running result must be presented. 1. (20 points) Write the “Hello, world!” program. 2. (30 points) Write a program to calculate the sum from -5 to10. Use the for loop to do the calculation. 3. (20 points) Write a complete C++ program that asks the user to enter the necessary information about the cylinder, calculate the volume in a function (named as calculate_vol, using reference to pass...
Using R Studio Use the two iid samples. (You can copy and paste the code into...
Using R Studio Use the two iid samples. (You can copy and paste the code into R). They both come from the same normal distribution. X = c(-0.06, 1.930, 0.608 -0.133,0.657, -1.284, 0.166, 0.963, 0.719, -0.896) Y = c(0.396, 0.687, 0.809, 0.939, -0.381, -0.042, -1.529, -0.543, 0.758, -2.574, -0.160, -0.713, 0.311, -0.515, -2.332, -0.844, -0.942, 0.053, 0.066, 0.942, -0.861, -0.186, -0.947, -0.110, 0.634, 2.357, 0.201, -0.428, -1.661, 0.395) (a) Report 95% confidence interval for the mean of X. Should we...
Copy and paste the below code EXACTLY as shown into your Java environment/editor. Your task is...
Copy and paste the below code EXACTLY as shown into your Java environment/editor. Your task is to fill in the code marked as "...your code here...". A detailed explanation follows the code. import java.util.*; public class OddManOut {    public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("How many random Integers to produce?"); int num = sc.nextInt();    ArrayList<Integer> randomInts = createRandomList(num); System.out.println("The random list is: "); System.out.println(randomInts);    removeOdds( randomInts ); System.out.println("The random list with only...
Part 2– R work (must be done in R) Copy and paste your R code and...
Part 2– R work (must be done in R) Copy and paste your R code and output into a word document, along with your written answers to the questions, and upload to Canvas.   Follow these instructions to import the necessary dataset: Before opening the dataset needed for this problem, you’ll need to call the “car”package.  Run the following line of code: > library(car) Now you can import the “Prestige” dataset and use it to answer the question below. Name the data...
PYTHON CODE Step one: Copy and paste these lyrics into a multistring line "Do you ever...
PYTHON CODE Step one: Copy and paste these lyrics into a multistring line "Do you ever feel like a plastic bag Drifting through the wind Wanting to start again? Do you ever feel, feel so paper-thin Like a house of cards, one blow from caving in? Do you ever feel already buried deep Six feet under screams but no one seems to hear a thing Do you know that there's still a chance for you 'Cause there's a spark in...
Run the following R code (copy and paste) to create some data: out1 <- rep( c(0,0,1),...
Run the following R code (copy and paste) to create some data: out1 <- rep( c(0,0,1), 3 ) out2 <- rep( c(1,0,1), 3 ) counts <- c(18,17,15,20,10,20,25,13,12) This is a variation on the data in the first example on the “glm” help page in R. The counts variable is our response variable and will be modeled as a Poisson variable, the out1 predictor variable will measure the difference between outcomes 2 (baseline) and 3, and out2 will measure the difference...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT