In Java, unreachable code is treated as compilation error. What do you think? Isn’t making it warning would have made developers’ life simpler?
Of course you can write –
if( true )
return;
What if you could simply write return. This helps in testing a function quickly. As a good programming practice we always remove all warnings from code, and such unconditional return statements can be rectified later.

2 Comments
I agree. Not only would this be more useful for coders wishing to test something, but what if you have a program that autogenerates Java code as its output? Now you have to create a special case for this sort of thing, re-engineering the code validator when the compiler could do the job just as well if this were a warning instead of an error.
Yes. C# for example treats unreachable code as just a warning. Sometimes this is simply volitional. I need this right now. The only choice I have is commenting out anything after the return. Unreachable code should definitly be only a warning.
Post a Comment