Go Pro!

Copy and Paste Rule of Thumb

Reference > Science > Technology > Beginner Programming Tips

In today's tip, I'm going to share with you some advice it took me a long time to figure out. For those who had "formal" training in computer programming, you probably had someone drill this into you thoroughly. If not, you need to pay attention to this. This is "Doug's Copy and Paste Rule of Thumb." Here it is:

Every time you highlight a section of code and copy it to paste somewhere else in your program, you should ask yourself, 'Should this be a function?'

It's a simple rule, and if you implement it, it will make your programs not just smaller and neater, but easier to read, and easier to maintain.

Let's say I'm performing a calculation to find the gravitational force between two bodies, separated by R1 meters, with masses M1 and M2. (Don't worry, you don't need to know anything about gravitational forces to understand the point I'm making!) My code would look like this:

Dim M1 As Double
Dim M2 As Double
Dim R As Double
Const GravConst = 0.00000000006673
Dim Force As Double

'Calculate Gravitational Force
Force = GravConst * M1 * M2 / R 2

That's it...just one line of code. But odds are, if I have that line of code in my program, it's probably because my program is all about gravity, or astronomy, or something related to physics. And if that's the case, is it likely that this is the only time I'm going to use that equation?

No, somewhere down the line I'll need it again, and I'll think to myself, "Now where was that line of code? I need it again." So I'll go find it, copy it, and paste it elsewhere in my program.

But here is where my "rule of thumb" comes into play. The moment I start highlighting that line of code I say to myself, "Wait a minute! Should I turn this into a function?" Then I think about it, and I realize, "Yes, I'm going to be using this a lot; I should make a function..."

Const GravConst = 0.00000000006673

'Calculate Gravitational Force
Function Force(M1 As Double, M2 As Double, _
          R As Double) As Double
   Force = GravConst * M1 * M2 / R 2
End Function

Now, whenever I need to calculate a force, I don't need to go looking for that line of code again; I just call my function.

Earlier in this tip I said this would help improve the program by making it easier to read and easier to maintain. It's easy to see that it makes it easier to read; when I come back to this program a month from now, I won't be looking at a bunch of mathematical equations and wondering to myself, "What does that do?" Instead, I'll be looking at a bunch of function calls to a function that obviously calculates a force, since that's what the function is called.

But how does this make the code easier to maintain? Simple. Suppose I found out later that my equation for gravitational force was incorrect. Now I need to go back and fix my program. Fortunately, the gravitational force equation is only in one place in the program, so I fix it once, and the change is made everywhere! But suppose I hadn't made a function. How many times would I have to change that equation?

I shudder to think about it. 

Time to Go Back...

Take a look at some old programs you have written. Look through them carefully. Can you find any places where you should have pulled a section of code (or even just a single line!) out and turned it into a function? 


Write a function that converts miles to feet (there are 5280 feet in a mile).
Write a function that calculates n! (Hint: 5! = 5x4x3x2x1).
Write a function that takes the square root of a number, or returns FALSE if the argument is negative.
Assign this reference page
Click here to assign this reference page to your students.
Handling Plurals and SingularsHandling Plurals and Singulars
Randomizing an ArrayRandomizing an Array

Understanding Coronavirus Spread

A Question and Answer session with Professor Puzzler about the math behind infection spread.

Blogs on This Site

Reviews and book lists - books we love!
The site administrator fields questions from visitors.
Like us on Facebook to get updates about new resources
Pro Membership