Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Different criteria for some 'unreachable code'?

 
David Sabate
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I'm trying to understand why in some situations the compiler seems to check if certain code is unreachable or not.
From the below 2 examples the compiler will detect unreachable code in different lines depending of the loop condition:





But this is not the case if we have a conditional statement? Why the compiler does not detect that there is still unreachable code also in both examples?





Thanks in advance,
 
K. Tsang
Bartender
Posts: 3521
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Given your example, the "while(true)" and "if(true)" lines are exactly the same. They will "always" throw the exception, making the next line b=37 unreachable.

On the contrary the "while(false)" and "if(false)" do the opposite - never throw exception and can assign b=37.

If you did not explicitly use "true" or "false" in the checking, the compiler may allow you to compile. But runtime is another story. Able to compile doesn't mean able to run successfully.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this has been asked before on this forum. I think the If-block gets 'preferential treatment'. For years, a common idiom in many languages has been this:



Then, when you want to debug, you change one value in one spot.

With things like log4j, this is really sort of obsolete, but you don't want to change the compiler in a way that can break existing code, nor do you want to annoy 'old school' programmers who have been doing exactly this sort of thing for possibly decades.
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Sabate wrote:
I'm trying to understand why in some situations the compiler seems to check if certain code is unreachable or not.

But this is not the case if we have a conditional statement? Why the compiler does not detect that there is still unreachable code also in both examples?


Interestingly, the compiler does detect the unreachable code for the condition statement. It is just designed to not complain about it....


Why? There is a use-case where unreachable code is actually important. For example, what if you wanted to do this?



basically, you like to do stuff during testing, but once it is all tested, you want to set a flag and all debugging stuff is turned off. The unreachable code checks would have required you to go back and comment out the code.

So, to allow this use-case, the Java language is designed to ignore unreachable code for condition statements.

Henry

 
Mauro Mazzucco
Ranch Hand
Posts: 32
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its because the assertion mechanism was only available in the java 1.4, before that we used to do that for debugging
Now please use assertion instead
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic