Imperfect Short-Circuit Evaluation

kevinthenerd

Platinum Member
Jun 27, 2002
2,908
0
76
Short-circuit evaluation in Java is imperfect. The code below breaks if I remove the method declarations or if I change the left-to-right order of the conditionals.

Code:
public class pears {
  public static void main(String[] args) {
    if (!(0==1 && unicorn()==pixiedust())) {
      System.out.println("The short-circuit evaluation worked!");
    }
    if (1==1 || unicorn()==pixiedust()) {
      System.out.println("The short-circuit evaluation worked!");
    }
  }
  public static int unicorn() {
    return 1/0;
  }
  public static int pixiedust() {
    return -1/0;
  }
}

This leads me to two higher questions about programming in general.

1. Do all interpreters or JIT compilers do a full syntax check before looking for this sort of optimization? (Is syntax-checking always a separate process before run-time optimization?)
2. Is short-circuit evaluation always performed left-to-right? Do any modern compilers ignore critical errors if the code can correctly continue without them?
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
Short-circuit evaluation in Java is imperfect. The code below breaks if I remove the method declarations or if I change the left-to-right order of the conditionals.

Code:
public class pears {
  public static void main(String[] args) {
    if (!(0==1 && unicorn()==pixiedust())) {
      System.out.println("The short-circuit evaluation worked!");
    }
    if (1==1 || unicorn()==pixiedust()) {
      System.out.println("The short-circuit evaluation worked!");
    }
  }
  public static int unicorn() {
    return 1/0;
  }
  public static int pixiedust() {
    return -1/0;
  }
}

This leads me to two higher questions about programming in general.

1. Do all interpreters or JIT compilers do a full syntax check before looking for this sort of optimization? (Is syntax-checking always a separate process before run-time optimization?)
2. Is short-circuit evaluation always performed left-to-right? Do any modern compilers ignore critical errors if the code can correctly continue without them?

1. Most compilers WON'T do this optimization because it changes the way the code behaves. Compilers are supposed to make the code behave exactly as written. That being said, code does not compile if there is a syntax error.
2. Depends on the language, in C like languages (such as Java) yes. Here is an example.

Object val = null;
if (val != null && val.hasMagic())
doStuff();

This is a common sort of expression in languages like java and c++. You first check to see if the object exists, then you use a function that is contained in the object. Reversing the order causes things to break.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
The code x = 1/0 ; does not contain a syntax error, it is perfectly valid code. It's just code that at run-time will always generate an arithmetic exception.

Some compilers don't bother to save you from yourself when writing silly code like that because the code that people write for production use will be something like x = y / z ; where the values of y and z are not known in advance.

Microsoft's C++ does catch this: error C2124: divide or mod by zero
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
The code x = 1/0 ; does not contain a syntax error, it is perfectly valid code. It's just code that at run-time will always generate an arithmetic exception.

Some compilers don't bother to save you from yourself when writing silly code like that because the code that people write for production use will be something like x = y / z ; where the values of y and z are not known in advance.

Microsoft's C++ does catch this: error C2124: divide or mod by zero

You missed the point. Those functions should never get called due to short circuit evaluation.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
> You missed the point. Those functions should never get called due to short circuit evaluation.

The OP said:
"The code below breaks if I remove the method declarations or if I change the left-to-right order of the conditionals."

Short-circuit evaluation is still working properly if the divide-by-zero exception only happens when you move the functions left of the other conditions.


OP then added:
"1. Do all interpreters or JIT compilers do a full syntax check before looking for this sort of optimization? (Is syntax-checking always a separate process before run-time optimization?)
2. Is short-circuit evaluation always performed left-to-right? Do any modern compilers ignore critical errors if the code can correctly continue without them?"

Perhaps I misread the questions, but OP seemed to be thinking that the compiler would be evaluating all tests at once instead of left-to-right, and should have treated "1 / 0" as a syntax errror.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,356
4,052
75
In general, it's best to avoid running functions in a conditional that have "side effects". Throwing an exception counts as a side effect. If you want to ensure that all parts of a conditional run, i.e. not short-circuit the evaluation, then use a single "&" or "|".
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
In general, it's best to avoid running functions in a conditional that have "side effects". Throwing an exception counts as a side effect. If you want to ensure that all parts of a conditional run, i.e. not short-circuit the evaluation, then use a single "&" or "|".

One minor addition, & and && don't work the same, they should be used with caution. For example, 1 && 2 == 1, 1 & 2 == 0. When used for logic, & should only be used on stuff that evals to 1 or 0.... Well, unless you understand how a bitwise and is working.
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
Short circuit evaluation is not short circuit compiling. Even if the compiler were to be able to recognize that 1 never equals zero, it still has to parse the other side of the function simply to determine where the && function ends.

And I'm fairly certain that any compiler will do syntax checking before it does any compiling, let alone performance optimizations. If it didn't, maintenance programmers everywhere would be calling for crucifixions. I don't know of any language where you can have invalid code in your program and still have it compile.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |