Is it somehow possible to make the compiler not to care about unreachable statements? Wouldn't a warning be enough?
Best regards
Marcus
Printable View
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..
Code :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...
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.
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 ...
Why not give assertions a try then? :)
// Json