• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Maybe "unreachable code" should be "dead code" in page 70, (Java OCA 8 Programmer I Study Guide)

 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read from some of Roel's posts that unreachable code is different from dead code. One of them:
Roel De Nijs wrote:Very important: dead code is not the same as unreachable code! If your code has unreachable code, you'll get a compiler error; if your code has dead code, the code compiles successfully.


On page 70, the second print statement of example maybe is a dead code according to this manner
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:I read from some of Roel's posts that unreachable code is different from dead code. One of them:
Roel De Nijs wrote:Very important: dead code is not the same as unreachable code! If your code has unreachable code, you'll get a compiler error; if your code has dead code, the code compiles successfully.

This is of course true (otherwise I would not have posted such a statement ), but it's only from the compiler's point of view! In the code snippet on page 70, the authors just want to explain that if hourOfDay is less than 11, you'll never get "Good Morning" printed on the console, it will always be "Good Afternoon" because (as stated in the study guide) the order of the conditions is very important. So from that perspective the code is unreachable (and thus pretty useless). But that's from a developer's or logical point of view, not the compiler's point of view. Let's look at what the compiler thinks about this code next.

In this code snippetline1 is unreachable from a developer's point of view. From the compiler's point of view: line1 is not dead code, nor unreachable code. So the above code will compile successfully without warnings and errors!

If you mark the local variable hourOfDay as final, it's another story.Now the compiler indicates all code after the closing curly brace of the 1st if statement as dead code. So that means the code will compile successfully with warnings! Thus from the compiler's point of view, all this code is considered to be dead code

Hope it helps!
Kind regards,
Roel
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote: This is of course true (otherwise I would not have posted such a statement ), but it's only from the compiler's point of view! In the code snippet on page 70, the authors just want to explain that if hourOfDay is less than 11, you'll never get "Good Morning" printed on the console, it will always be "Good Afternoon" because (as stated in the study guide) the order of the conditions is very important. So from that perspective the code is unreachable (and thus pretty useless). But that's from a developer's or logical point of view, not the compiler's point of view.

Everything is clear now. Thanks a lot, Roel

Roel De Nijs wrote: line1 is unreachable from a developer's point of view. From the compiler's point of view: line1 is not dead code, nor unreachable code. So the above code will compile successfully without warnings and errors!

At first glance it seems simple, but it is not such as

Roel De Nijs wrote:Now the compiler indicates all code after the closing curly brace of the 1st if statement as dead code. So that means the code will compile successfully with warnings! Thus from the compiler's point of view, all this code is considered to be dead

Do IDE inform us about warnings?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:
Roel De Nijs wrote:Now the compiler indicates all code after the closing curly brace of the 1st if statement as dead code. So that means the code will compile successfully with warnings! Thus from the compiler's point of view, all this code is considered to be dead

Do IDE inform us about warnings?

An IDE normally shows the compiler warnings as well. And when you are actually developing production code, it can provide much more warnings about your code style (e.g. unused private data members).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic