Why is unreachable code an error java
Now, make little modification to above example. Replace the if block with while block. Compile it and see what happens. This example gives compile time error : Unreachable code. But why? Why it does not complain about unreachable code after if block.? This is because,The rules of unreachable code for different blocks like constructors, methods, try, catch, if, for, while etc are defined individually in the Java Language Specification.
According to Java Language Specification, The conditional expression of If block will no be evaluated at compile time to decide the unreachable code. You can check the rules of unreachable code here. Your email address will not be published. Notify me of follow-up comments by email. Notify me of new posts by email. Ask Question. Asked 11 years, 1 month ago. Active 4 years, 3 months ago. Viewed k times.
Is this just Java trying to be a Nanny, or is there a good reason to make this a compiler error? Improve this question. Mike Mike Java isn't entirely consistent about this either. For some control flows that cause dead code, but Java doesn't complain.
For others it does. Determining what code is dead is an uncomputable problem. I don't know why Java decided to start something it couldn't finish. Add a comment. Active Oldest Votes. Improve this answer. Community Bot 1 1 1 silver badge. SamStephens SamStephens 5, 6 6 gold badges 33 33 silver badges 44 44 bronze badges. Obviously the language designers of Java agree "Language designers of Java" are humans and prone to mistakes too.
Personally, I feel that other side in the discussion you refer has much stronger arguments. Gabe: Because it's not harmless - it's almost certainly a mistake. Either you've put your code in the wrong place, or misunderstood that you've written your statement in such a way that some of it can't be reached. Making this an error prevents incorrect code from being written, and if you want something in an unreachable place in your code for other developers to read the only audience for unreachable code , use a comment instead.
SamStephens: Seeing as how uncalled methods and unused variables are essentially all forms of unreachable code. Why allow some forms and not others? In particular, why disallow such a useful debugging mechanism? Aren't comments also unreachable? In my mind, unreachable code is actually a form of comments this is what I was trying to do earlier, etc.
But considering "real" comments aren't "reachable" either The problem is that they java language designers are inconsistent with this. If there is actual flow analysis, it's trivial to realize that both return; System. So, when I'm debugging, and I use this method to "comment out" code temporarily, that's how I do it. The problem with commenting out code is that you have to find the appropriate place to stop your comment, which may take longer than throwing in a return; real quick.
Show 20 more comments. For your specific need, this is the usual trick: if true return; It looks nonsensical, anyone who reads the code will guess that it must have been done deliberately, not a careless mistake of leaving the rest of statements unreachable. Still don't get why you'd bother with the trick you show.
Unreachable code is meaningless to the compiler. So the only audience for it is developers. Use a comment. Although I guess if you're adding a return in temporarily, using your workaround might be fractionally quicker than just putting in a return, and commenting out the following code. SamStephens But everytime you wanted to switch you would have to remember all the places where you have to comment out code and where you have to do the opposite.
You would have to read through the whole file, changing the source code, probably making some subtle mistakes every now and then instead of just replacing "true" with "false" at one place. I think it's better to code the switching logic once and don't touch it unless necessary. This is exactly the problem in my opinion, people shouldn't be guessing, they should understand. Remembering code is communication with people, not just instructions for computers.
If it's anything other than extremely temporary, you should be using configuration in my opinion. This trick is really useful for debugging. I often add an if true return to eliminate "the rest" of a method trying to find out where it's failing. There are other ways, but this is clean and quick--but it's not something you should ever check in. Brady Moritz Brady Moritz 8, 8 8 gold badges 61 61 silver badges 97 97 bronze badges. Save Article.
Improve Article. Like Article. Next Arrays in Java. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert.
0コメント