Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if(false) vs. while(false): unreachable code vs. dead code

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?

like image 464
luukburger Avatar asked Nov 30 '13 11:11

luukburger


People also ask

What is the difference between dead code and unreachable code?

unreachable code is something that would never be executed because there is no flow control to reach the code. A dead code is something that gets (or might get) executed, but its results are never used.

What kind of test is most likely to find unreachable dead code?

Static code analysis can easily detect issues such as unreachable code or dead code in code blocks like figure 1 easily and would correctly flag them as dead code.

Why are statements unreachable?

The Unreachable statements refers to statements that won't get executed during the execution of the program are called Unreachable Statements. These statements might be unreachable because of the following reasons: Have a return statement before them. Have an infinite loop before them.

When a code block become unreachable explain?

A block of statements to which the control can never reach under any case can be called as unreachable blocks. Unreachable blocks are not supported by Java. The catch block mentioned with the reference of Exception class should and must be always last catch block because Exception is the superclass of all exceptions.


2 Answers

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.

like image 197
chrylis -cautiouslyoptimistic- Avatar answered Sep 22 '22 00:09

chrylis -cautiouslyoptimistic-


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.

like image 26
Rahul Tripathi Avatar answered Sep 21 '22 00:09

Rahul Tripathi