if(false) vs. while(false): unreachable code vs. dead code
Asked Answered
W

3

32

I tried the following in Eclipse:

  • if (false) {}: warning 'dead code'
  • while (false) {}: compilation error 'unreachable code'

I was wondering whether there is a real 'reason' for this difference. I already found this...

Unreachable code compiler error

...but why not allow while (false) for the same debugging purpose?

Washbowl answered 30/11, 2013 at 11:54 Comment(0)
C
33

The JLS section on unreachable code explains the rationale. Essentially, Java normally shouldn't use conditional compilation like C routinely does with #ifdef, but there are some situations (such as debugging, and in particular backward binary compatibility) where allowing the compiler to entirely strip out code is needed, and so the specific construct if(false) is permitted for that purpose.

Colostomy answered 30/11, 2013 at 11:57 Comment(7)
+1 but why not allow while (false) for the same debugging purpose? I didn't get that part?Yevette
@MarkRotteveel I'm not always in the mood for JLS archaeology... most interesting man memeColostomy
Does that mean that if(false) {} is the only exception where the compiler 'allows' unreachable code?Washbowl
@NarendraPathai while(false) doesn't really make sense for debugging; in a debugging (or binary-compatibility) situation, you'll be either turning off sections or alternating between version (such as the Android compatibility libraries do). In both of these cases, any loops would remain in place, and it's best to keep the exception to the reachability rule as narrow as possible.Colostomy
@Washbowl Not if(false) literally, but if(compile-time-false-expression).Colostomy
@NarendraPathai The why is explained at the end of the linked paragraph in the JLS: conditional compilationPuleo
Thanks! On a side note, while(true) is another unreachable code scenario, for the code that follows the loop. :-)Glazing
A
9

You must read the Unreachable Statements. Although with while(false) the compiler will throw an error but with if(false) it wil show a warning to the user.

Although if (false) was kept in Java to simulate C/C++ preprocessor #if 0

The specification says that:

if (false) { x=3; }

does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as "unreachable" in the technical sense specified here.

The rationale for this differing treatment is to allow programmers to define "flag variables" such as:

static final boolean DEBUG = false; and then write code such as:

if (DEBUG) { x=3; } The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.

Allier answered 30/11, 2013 at 11:58 Comment(5)
The compiler won't throw an error for if (false {}. That's the point.Washbowl
I still have the downvote because the compiler will not raise an error in the case of if(false).Colostomy
@RahulTripathi while(false) is a compile-time error. if(false) may produce a warning but is explicitly not a compile-time error.Colostomy
@luukburger:- Yes you are correct. I got it wrong. I have updated my answer and the rationale why the compiler will not throw an error!Allier
It's worth noting that if (compileTimeConstant) {...} makes sense as a construct, and while(true) {...} and do {...} while(compileTimeConstant) make sense, but while(compileTimeConstant) {...} doesn't make sense in any case [other than the literal-true one] that couldn't be better written as if (compileTimeConstant) { while(true) {} }.Excusatory
B
0

However, the 'do-while loop' will compile and execute.

do {
    System.out.println("its work!");
} while (false);                       //compile and execute
Bureaucrat answered 4/12, 2020 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.