views:

46

answers:

4

When experimenting I often use if (true) {..} or if (false) {..} to section off chunks of code I'm playing with.

The problem is that compilers these days sometimes issue a warning about unreachable code. I then have to promote my code to something like if ((10 % 2) == 0), but then some smarter compilers catch this as unreachable too.. (And so a small arms race begins..)

I'd like to catalogue the standard expressions (across many languages) which will always return true or false, and which the compiler is unable to statically warn me about. Can you tell me any?

+1  A: 

Why not just use some kind of "environment variable" to control this kind of thing? Make it a reall conditional then you're not fighting the compiler.

if ( environment.MODE_EXPERIMENT) {
     experimental code here
}

Personally I try pretty hard to avoid doing this kind of thing, we have SCM systems that allow us to keep old versions of code, really shouldn't need to do this too much.

djna
+1  A: 

This should do:

if (atoi("1")) {
}

This can also be useful to generate any kind of constant.

Thomas
+1  A: 

Try moving it out of the code, using pre-processor flags:

#if false 
    .... code which won't get run ....
#endif

Some IDEs will even collapse such un-runnable blocks for you to keep them out of the way.

Douglas
A: 

Almost every single language has a version of if(false) { } that a compiler/IDE won't complain about. The most common (afaik) look like the following:

/*
    int i = 0;
    String s = "I will never be run!";
*/

Yes, comments. Multi-line comments, to be exact. Stopping blocks of code from being run are pretty much their second main purpose (after documentation). if(true), of course, is then handled by not using them.

You're trying to re-invent the wheel here, and I have no idea why.

Stephen