开发者

What does if(false){some code} mean in java

Java code

if(x == null){
 //some code
}
if(false){
 //some code
}

when is if(false){co开发者_开发知识库de} executed?


It is never executed. Sometimes people do it when they have some old code they want to remember, or some new code that should not yet be used. like

if(false){fancyNewFunction();}

(as far as i'm concerned, this is bad form and you should not do it, but that doesn't mean it doesn't happen ;) )


This could also be a common way to emulate macro preprocessor directives like #ifdefine. Some people use it to enable or disable logging.

For instance the following code:

public class Sample{

    private static final boolean LOG_ENABLED = true;

    public static void main(String args[]){
        if(LOG_ENABLED){
            System.out.println("Hello World");
        }
    }
}

Produces the following bytecodes:

public class Sample extends java.lang.Object{
public Sample();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   ldc #3; //String Hello World
   5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   8:   return

}

If you disable the flag, you get this bytecodes:

public class Sample extends java.lang.Object{
public Sample();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   return

}

As you can see, no bytecodes were generated for the second case in the main method. So, if you disable logging and recompile the code, you improve underlying bytecodes.


I use

if (false)
{
   doSomething();
}

on occasion to prevent execution of doSomething().

It may be better/clearer to do this:

final static private boolean ENABLE_WOLZOPPERY = false;

if (ENABLE_WOLZOPPERY)
{
   wolzopp1();
}
blah_blah_blah();
if (ENABLE_WOLZOPPERY)
{
   wolzopp2();
}

so that a single constant can enable/disable the behavior of more than one block in a named fashion.


The code in if condition executes when the condition becomes true now in if(false) the condition is always false so code in if will never execute.

if(false){

// This code will be dead code and will never execute.

}


The code in if(false) { code } will never be executed.

It is a common way to "comment out" code that should not be running (usually debugging code or test code). It's not really the best style, in my opinion, but it's a quite common idiom.


Code like this will never execute. Remove it. Do not comment it out. It is just clutter that will confuse someone who has to maintain it later on. If you need to get it back, go to your version control system.


code is never executed, and probably will be eliminated from the byte code. However the snippet will be syntax-checked at build time, so there are sometimes good reasons to do this.


the code inside the if will never be executed, the code might as well just be commented code out


It is not normal code, I have never used constructs liked this. Maybe a lousy way to comment out some code.


This can be convenient if you are stepping through code in the debugger.

If you have code that you only want executed while it is being 'supervised' i.e. you're using the debugger and actively setting the next executable step in the code to be inside the if (false) block. Normally, when the code is run it will be skipped.

There's no real reason for this to be in production code though.


if (false) means peace of code which is never executed. If some of your code is unused you should remove it.

The only reason for using the code is debug purpose but I think it isn't good idea. Nowadays when IDE exists we can select block of code and press some shortcut to make multilines comments (for Eclipse CTRL + /).

You should never commit this code because can cause many problems:

  • It's hard to notice that this peace of code never be executed
  • We still must maintenance this code.
  • We cannot make test coverage for this


I use if(false) when I use goto statement, to go there & execute some code not done otherwise and continue with rest of the code.


wrapping a code into if(false) block is just for author's referral.

Of course the code will never be executed but it holds instructions that could be executed otherwise. Saying today I'm writing a code and have an idea of something else that could be helpful in future days but still need clear ideas of how I could process. Instead of just writing comments or comment the code, I put it into that block.

It's not a good practice though. But doesn't harm.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜