Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In C++ when I wanted to mark that if is empty by design I added NULL; as statement:

if (cond1) {
    ...
} else if (cond2) {
    NULL;
} else {
    ...
}

However in Java it seems to be impossible to have null; as statement. What is the correct way of marking empty by design if in Java (is it just comment)?

share|improve this question
1  
If I really want to mark it I use: //do nothing –  RoflcoptrException Nov 19 '11 at 16:53
2  
What's wrong with using comments? –  Mike Baranczak Nov 19 '11 at 16:53
6  
Why are you making empty blocks? You could re-write that as } else if (!cond2) { and follow that with the contents of the else block. –  unholysampler Nov 19 '11 at 16:54
    
unholysampler: In this case yes (I was more interested here to give short sample then anything else) - it was just that the cond2 is longer and there are several ifs after that. I'd need to prepend the condition to each of them. –  Maciej Piechotka Nov 19 '11 at 16:57
    
Mike Baranczak: Nothing - but I was wondering if there is other idiom to do this. –  Maciej Piechotka Nov 19 '11 at 16:58

2 Answers 2

up vote 5 down vote accepted

Firstly, how is this:

if (cond1) {
    ...
} else if (cond2) {
    NULL;
} else {
    ...
}

Better than:

if (cond1) {
    ...
} else if (cond2) {

} else {
    ...
}

The NULL;-statement doesn't tell anything, or does it?

Secondly, as others pointed out, you don't have to have that empty block, unless you need to say something about the condition or possibly leave space to handle it.

So this makes perfect sense:

if (cond1) {
    ...
} else if (cond2) {
    //cond2 is not handled, because 2 is the smallest prime number
} else {
    ...
}

Or this:

if (cond1) {
    ...
} else if (cond2) {
    //TODO: investigate whether/how this condition needs to be handled
} else {
    ...
}

However an undocumented empty consequence serves no purpose, whether you write NULL; in there or not.

Lastly, if you feel like you are having too many conditions, then you should investigate how behavioral patterns and polymorphism in general could help you clean up your code.

share|improve this answer
    
I presume the point of the NULL is to make clear that the programmer intended an empty block, rather than a line having been deleted by mistake or the like. However, in that case a comment would work as well. –  David Schwartz Nov 20 '11 at 4:56

That's a bad practice altogether and you shouldn't have this block in the first place. If you don't react on a condition then don't code it. It's a code smell and contains hardly any useful information. Using it to design a program flow is even worse, as this can always be expressed more elegantly.

The correct way of handling this is to remove the unecessary else if or to rewrite your conditions instead of having a marker for an empty block.

In your example that would be:

if (cond1) 
{
    ...
}
else if(!cond2)
{
    ...
}

If the conditions are tedious to write or too difficult to read, then encapsulate them in a method or evaluate them once and save them to a variable.

If you really really want to communicate why you did not handle a certain condition, write a comment above the method or the statement.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.