4

If I ask a user to input an int, and need to check if it is within the range of indices of an array before checking the array at that index to see if it's not null, would that be an example of "short circuiting"? Because if the array size is only 5 and the user inputs 15, then I would get an ArrayIndexOutOfBoundsException. But if I first check if the number input is 0-4 inclusive and then check the array index last, it will be guaranteed to be between 0-4 inclusive. So my question is: is this an example of "Short Circuiting"? I'll rephrase what I'm saying in code...

import java.util.Scanner;

public Class Reservation{

    Customer[] customers = new Customer[5];
    Scanner input = new Scanner(System.in);
    //some code

    private void createReservation(){

        System.out.print("Enter Customer ID: ");
        int customerIndex;
        if(input.hasNextInt()){
            customerIndex = input.nextInt();
            //is the if-statement below a short-circuit
            if(customerIndex < 0 || customerIndex >= 5 || customers[customerIndex] == null){
                System.out.print("\nInvalid Customer ID, Aborting Reservation");
                return;
            }   
        }
        else{
            System.out.print("\nInvalid Customer ID, Aborting Reservation");
        }
    //the rest of the method
    }
}
  • 1
    Short-circuiting generally refers specifically to the behavior of the `&&` and `||` operators when it can be determined that the right-hand operand doesn't need to be evaluated. – user2357112 Dec 03 '13 at 02:55
  • Wikipedia entry on [short circuiting](http://en.wikipedia.org/wiki/Short-circuit_evaluation) – MarsAtomic Dec 03 '13 at 02:58
  • "Short circuiting" is a highly context-dependent term. Most familiar with the `&&` and `||` operators, but also a term in compiler control flow optimization and in certain cases of optimizing computations. In general it's any scheme where there's a "quick exit" or "bypass" that avoids some operation when it can be shown that it's unnecessary. – Hot Licks Dec 03 '13 at 03:34
  • [Yes/no questions about an example are not a good fit for this site](http://meta.stackoverflow.com/questions/258630/where-is-the-line-for-yes-no-questions). Answers to such questions are rarely useful to anyone except the original asker. The purpose of this site is to create a useful repository of high quality questions with answers. Instead of asking "is this an example of _X_", ask "what is _X_". What makes you think your code is **not** short circuiting? – Raedwald Feb 26 '16 at 13:13
  • Possible duplicate of [What is short circuiting and how is it used when programming in Java?](http://stackoverflow.com/questions/9344305/what-is-short-circuiting-and-how-is-it-used-when-programming-in-java) – Raedwald Feb 26 '16 at 13:14

2 Answers2

1

Yes, this is a valid example of using short-circuiting correctly:

if(customerIndex < 0 || customerIndex >= 5 || customers[customerIndex] == null)

This code works only under the assumption that || stops evaluating as soon as it gets a true - otherwise, customers[customerIndex] could be reached with an invalid index, triggering an exception.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Yes, because if any of your comparisons, going from left to right, are true, then the remaining comparisons to the right do not need to be evaluated.

Another example would be:

if(node != null && node.value.equals("something"))

In this case, if node == null, short-circuiting occurs, since the && requires two true values and the first is false, it does not evaluate the second comparison.

Steve P.
  • 14,489
  • 8
  • 42
  • 72
  • Thanks to you as well. I will also vote up your answer when I get sufficient rep. –  Dec 03 '13 at 03:06