Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty if-statements [duplicate]

By "empty if-statement", I mean something like this (note the semicolon):

if (condition);

I'm having trouble thinking of an application for this. With a while loop you can do this:

while (callUntilReturnsFalse());

But there's no such application for an if-statement. What's more, the Java compiler doesn't issue an error or a warning when confronted with such a statement. This can lead to large and silent problems, especially with a long and convoluted statement:

if ((functionA() && functionB(getFoo()) ||
    checkForComplexCondition(arg1, arg2, getBar(getFoo())));
{
    doStuff();
}

My question is: why is this allowed in Java? And, more importantly, can I enable an option to cause a warning when this happens?

(This question was asked before with regards to C#, which does issue a warning, but I was hoping to find a way to cause a warning with Java.)

like image 644
nullptr Avatar asked May 07 '13 21:05

nullptr


People also ask

Is empty in if condition?

An empty statement does nothing. Which means, if the condition is true, do nothing.

What do you put in an empty if statement?

Empty Statement in if … else Statement In the above code snippet, the if the condition has the empty body (or statement) as we have put a semicolon immediately after the if statement. Similar to loops, we can re-write the above code snippet as: int k = -1; if(k < 0)

Can you leave an IF statement empty Python?

A Python program cannot have an empty if statement. If for any reason, you must provide an empty if statement then use the pass keyword.


3 Answers

why is this allowed in Java?

See Java Language Specification (14.6. The Empty Statement):

An empty statement does nothing.

It's simply allowed and it's equivalent to (and will be translated to):

if (condition) {  }

Which means, if the condition is true, do nothing.

If you're using eclipse, you can look here, you might find something useful (I'm not sure there exists such an option for semicolon terminator):

WindowPreferencesJavaCompilerError/Warnings

enter image description here


EDIT

As @nullptr pointed out in his answer, there exist an IDE warning for this, you need to set warning on Empty statement.

like image 54
Maroun Avatar answered Oct 26 '22 15:10

Maroun


I don't think this is truly relevant to the intent of the question but I think it should be stated as it is relevant to the essence of the question.

There is an effect of an:

if(variable);

if the variable is volatile. It''s effect is to cause a memory barrier to be honoured between the current thread and any other threads accessing the variable.

public volatile variable;
....
if(variable);

See here for a more detailed discussion.

I cannot imagine any real value to putting this kind of statement in your code but I felt it important to note that there is a real effect to this statement in this very specific situation.

like image 14
OldCurmudgeon Avatar answered Oct 26 '22 15:10

OldCurmudgeon


There's one construct that I use fairly frequently which the "null statement" makes clearer and easier to understand. Here's an example:

for (int i=0;  i < argc;  i++)
{
   if (argv[i]=="left")
      hpos++;
   else if (argv[i]=="right")
      hpos--;
   else if (argv[i]=="up")
      ;
   else if (arv[i]=="down")
      ;
   else fprintf(stderr, "Unknown option \"%s\\n".", argv[i]);
}

In this case, I still want to check for the existence of certain options, while only executing code for some of them. In this case, using the null statement, as above, makes the function and structure of the code more readable and comprehensible to the next guy who has to come along and maintain it.

There are certainly ways to restructure this code to not require the null statement. But I don't believe that its intention will be as clear as in the code snippet.

like image 7
Curt Avatar answered Oct 26 '22 15:10

Curt