Protecting your program
Short Circuit Analysis
You are probably already familiar with the concept of short circuiting. If you have a lamp plugged in to the wall but you cross the wires at a point before the light, the electricity will take the shorter path (i.e., through your crossed-wire connection instead of through the light). This can cause hot wires and blown fuses or circuit breakers for reasons you learn about in your Physics course. However, for purposes of the discussion in this topic, a short circuit is one that takes the shortest path to complete an action in programming code, and you will see that here.
One of the capabilities of the Java programming language is the ability to implement a short circuit process on logical conditions. Consider the following test condition. This is an exaggerated condition, but it will help explain the use of short circuiting. If you had to find out if a class average was above a certain level, you could implement the following if statement.
// check for calculated average student grades > 80
if( studentGrades / numStudents > 80.00 )
{
// set high class average flag to true
highClassAverage = true;
}
First of all, you should not implement mathematical calculations in the same place where you are testing a result. As it turns out in your later studies, there will be some comparable conditions to this that cannot be helped. So, as a short commentary here, do not implement mathematical operations in a conditional expression unless the programming operation absolutely requires it. In other words, this process was conducted by an Academic Professional; do not try it at home.
Now, as for the tested condition, you would find that if the students' grades divided by the number of students (i.e., the class average) was greater than 80.00, then the class would be credited with having a high average.
However, under this rather special condition, what would happen if, as part of a larger program, one data set was passed through without any students (i.e., the numStudents variable holds zero)? As you can see, division by zero would be attempted, and the program would be halted by the operating system. Good programming requires that you watch for any condition that might stop the program, and this happens to be one of them. Here is a solution to the condition.
// check for more than zero students
// and calculated grade average > 80
if( ( numStudents > 0 ) && ( studentGrades / numStudents > 80.00 ) )
{
// set high class average flag to true
highClassAverage = true;
}
Now this might not look very helpful because it looks like you are going to divide by numStudents again. And you would, except for the short circuit process. If the conditional test numStudents > 0 fails (i.e., evaluates to false), there is no chance that the whole expression can evaluate to true. Remember that for an AND condition, both or all parts must evaluate to true for the whole expression to evaluate as true.
The Java programming language recognizes that if the first item does not evaluate to true, it does not matter if the other items are true or false. The larger expression -- containing the initial false term -- is unquestionably going to evaluate to false. With that in mind, the conditional expression is stopped as soon as the first false result is found, and the studentGrades / numStudents mathematical operation is never implemented. This is a powerful way to implement effective conditional tests without exposing the program to potential problems.
The OR operation works in a comparable way. Since you (and the compiler) know that when expressions and Boolean values are ORed together, it only takes one of them to make the expression true. Thus, the Java programming language watches for the first item in a list to be true, and stops testing as soon as it is found. Consider the following.
bool a = false;
bool b = true;
bool c = true;
bool d = false;
if( a || b || c || d )
{
conIO.printString( “At least one of the Boolean values was true” );
conIO.printEndline();
}
During a program run, the program would test the first item (i.e., variable a) and find it false, so then it would test the second item (i.e., variable b). When it found b to true, it would do no more testing, and would go ahead and branch the program operations into the code block to provide the displayed output. The OR short circuit is handy for reducing program overhead and time but generally speaking, the AND short circuit is the one that can protect your program from crashing. You will see powerful uses for this operation in one or more courses after this one.
This brief topic was used to point out the use of short circuit actions to protect programs. While operating systems tend to protect programs from crashing more than they used to, it is still incumbent on the programmer to find ways to protect programs from crashing or providing incorrect results.
Watch this video to observe and try some coding that addresses logical short-circuiting. As a note, the example in this text will cause some programs to crash. However, as operating systems evolve, they protect themselves from this kind of thing, which is good. Thus, the example given here actually works, in a weird kind of way, in some newer operating systems. As a result, another example is provided in the video.