Home Math Games Word Games Strategy Reference Junior Games Daily Puzzles Printables Problems Members

Home Reference Programming Tips

Each page contains a helpful programming tip and exercises which encourage beginners to use what they've learned in a different situation.
Index Previous    Next    Teachers    About   

Copy And Paste Rule Of Thumb

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 in to 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 alot; 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.

Other Scenarios

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?

Index Previous    Next    Teachers    About   

"Beginner Programming Tips and Tricks" is written by Douglas Twitchell, and hosted at The Problem Site.

Contents copyright 2005 by Douglas Twitchell. Contents of this page may not be reproduced without permission of the author. For information on using this site in a classroom situation, please visit the Teachers page.

More programming information and other tips can be found at Virtu Software's Ask Doug site.

 



 
Search For More Resources Search For More Educational Resources

 

Top Games On This Site

 
Adders!
Scramble
Entrapment
Secret Word
Trio Match
 

Just a few of the educational game resources available on The Problem Site!

 
 






Home       All Games       Problems       Contact       Site History       Privacy Policy       Member Page