Is it somehow possible to make the compiler not to care about unreachable statements? Wouldn't a warning be enough?
Best regards
Marcus
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Is it somehow possible to make the compiler not to care about unreachable statements? Wouldn't a warning be enough?
Best regards
Marcus
in my opinion , i dont think so... because the compiler will execute and read sequentially each and every line of your code.. even the smallest space(i think so)...
so if the compiler encounters anything that IT CANT REACH... it will complain... because it cant reach it..
consider this as an example
lets assume that you make a method with a switch structure..
public int myMethod() { switch (value) { case <value1>: break; return 0;
of course the compiler will complain.. AND it wont be able to execute the program..
because how can it return something if the program already breaks?....
it wont be able to reach the return value...
Last edited by chronoz13; December 8th, 2009 at 10:00 AM.
Whats the point in keeping unreachable code anyways?
// Json
During the creation of a program I can see plenty of good reasons for temporarily wanting to have unreachable code. You may for example want to switch off large parts of a function with a simple return statement instead of jerking around with commenting stuff out. It's kind of a useful debug-thing.
Last edited by Marcus; December 8th, 2009 at 12:17 PM.
There's no way around this in Java unless you choose to write your own Java compiler. There are ways to "trick" the compiler into thinking that a section of code is reachable when it is really unreachable, but I would strongly recommend AGAINST doing this because there really isn't any point in doing so and will greatly confuse anyone who is trying to read your code.
The best way to test segments of code is to build them up separately, test them against expected (and maybe unexpected) inputs to see if they work, then stuff them into your main code. Once you know that a section of code works alone, you can be guaranteed that that section of code will function exactly as it should, and if any problems occur it is either from incorrect use of that code by the main code, or is simply somewhere else.
Well, actually I've written a programming language that creates windows applications. I'm now working on a version of the compiler that creates Java applets instead. I know it's probably not of any one's interest. But the language can be found at NaaLaa (link). I've compiled one of the examples to a Java applet that can be found at http://www.naalaa.com/breakout_java/test.html (link). It's a breakout-clone that takes very long to load (the images are all png and copied directly from the windows app), but it works very well (although slow) so far :-)
However, my language doesn't mind unreachable code. Therefor it can also generates Java code with unreachable statements. It'd probably take me forever trying to make the compiler detect such code segments ...
Last edited by Marcus; December 10th, 2009 at 12:55 PM.
Why not give assertions a try then?
// Json