In this set of lectures, we wish to draw upon many things which we have seen earlier. By focussing on these experiences in a unified way, we hope to learn one very important lesson about programming.
At times, you may notice similarities between some code fragment you have already written and some code that needs to be written. It may seem very convenient to use a fragment of the existing code to aid you with the new code. Generally, this may lead to a decision to cut-and-paste that fragment to another location of the program, after which some minimal alterations might be needed. (BTW, the term cut-and-paste is probably not appropriate, as the original fragment remains; probably copy-and-paste is more fitting, but we will stick with tradition)
Have you ever found yourself in such a situation? If so you are to be commended for having the intuition for recognizing similarities between the processes; this demonstrates strong skills in abstraction. Unfortunately, the use of such cut-and-paste coding to take advantage of the similarities was almost surely a poor design. As a general rule cut-and-paste coding is never the best way!
You are also more likely to create bugs and to have them go longer without being noticed. If you do have to adapt each copy individually, it is quite easy to make a mistake on one copy but not another.
A lesser concern is that the actual length of your source code becomes unnecessarily long in this way. Though computer memory is not necessarily scarce, it can be in some settings, and the software program itself must be written to disk to be saved, and must be loaded into main memory to be executed. If you land a job where they pay you per line of code that you produce, great; but in all other cases you should take pride in how few lines of code you have written, not how many.
For more reading on this, see:
Another benefit is that you are more likely to find bugs when a single elegant code fragment is driving it all. This is because any bug will be a major bug, and likely to be discovered quickly. A single bug in one part of a cut-and-paste solutiion might go unnoticed for longer because its effect is not as obvious.
Why cut-and-paste as in:
System.out.println("I will not chew gum in class.");
System.out.println("I will not chew gum in class.");
System.out.println("I will not chew gum in class.");
System.out.println("I will not chew gum in class.");
System.out.println("I will not chew gum in class.");
System.out.println("I will not chew gum in class.");
System.out.println("I will not chew gum in class.");
System.out.println("I will not chew gum in class.");
System.out.println("I will not chew gum in class.");
System.out.println("I will not chew gum in class.");
System.out.println("I will not chew gum in class.");
System.out.println("I will not chew gum in class.");
when the repetition can be expressed with a loop.
Or similarly, why cut and paste the chorus of the song, as in:
System.out.println("Picture yourself in a boat on a river");
System.out.println("With tangerine trees and marmalade skies");
System.out.println("Somebody calls you, you answer quite slowly");
System.out.println("A girl with kaleidescope eyes");
System.out.println("");
System.out.println("Cellophane flowers of yellow and green");
System.out.println("Towering over your head");
System.out.println("Look for the girl with the sun in her eyes");
System.out.println("and she's gone");
System.out.println("");
System.out.println("Lucy in the sky with diamonds");
System.out.println("Lucy in the sky with diamonds");
System.out.println("Lucy in the sky with diamonds, ah");
System.out.println("");
System.out.println("Follow her down to a bridge by a fountain");
System.out.println("Where rocking horse people eat marshmallow pies");
System.out.println("Everyone smiles as you drift past the flowers");
System.out.println("that grow so incredibly high");
System.out.println("");
System.out.println("Newspaper taxies appear on the shores");
System.out.println("Waiting to take you away");
System.out.println("Climb in the back with your head in the clouds");
System.out.println("and you're gone");
System.out.println("");
System.out.println("Lucy in the sky with diamonds");
System.out.println("Lucy in the sky with diamonds");
System.out.println("Lucy in the sky with diamonds, ah");
System.out.println("");
System.out.println("Picture yourself on a train in a station");
System.out.println("With plasticine porters with looking glass ties");
System.out.println("Suddenly someone is there at the turnstile");
System.out.println("The girl with kaleidescope eyes");
System.out.println("");
System.out.println("Lucy in the sky with diamonds");
System.out.println("Lucy in the sky with diamonds");
System.out.println("Lucy in the sky with diamonds, ah");
System.out.println("Lucy in the sky with diamonds");
System.out.println("Lucy in the sky with diamonds");
System.out.println("Lucy in the sky with diamonds, ah");
when you can write a method:
private void chorus() {
System.out.println("Lucy in the sky with diamonds");
System.out.println("Lucy in the sky with diamonds");
System.out.println("Lucy in the sky with diamonds, ah");
}
and then write the song as:
System.out.println("Picture yourself in a boat on a river");
System.out.println("With tangerine trees and marmalade skies");
System.out.println("Somebody calls you, you answer quite slowly");
System.out.println("A girl with kaleidescope eyes");
System.out.println("");
System.out.println("Cellophane flowers of yellow and green");
System.out.println("Towering over your head");
System.out.println("Look for the girl with the sun in her eyes");
System.out.println("and she's gone");
System.out.println("");
chorus();
System.out.println("");
System.out.println("Follow her down to a bridge by a fountain");
System.out.println("Where rocking horse people eat marshmallow pies");
System.out.println("Everyone smiles as you drift past the flowers");
System.out.println("that grow so incredibly high");
System.out.println("");
System.out.println("Newspaper taxies appear on the shores");
System.out.println("Waiting to take you away");
System.out.println("Climb in the back with your head in the clouds");
System.out.println("and you're gone");
System.out.println("");
chorus();
System.out.println("");
System.out.println("Picture yourself on a train in a station");
System.out.println("With plasticine porters with looking glass ties");
System.out.println("Suddenly someone is there at the turnstile");
System.out.println("The girl with kaleidescope eyes");
System.out.println("");
chorus();
chorus();
In the above two examples, the repetition was exact. Of course, we have seen how variables and arrays can be used in conjunction with a loop so that the action expressed by the body of the loop varies from one iteration to the next, or how parameters can be sent to a method so that the precise behavior is effected.