views:

1261

answers:

18

This may be a religious argument, but it has been debated ad-nauseum here at my work whether all IF statements should include an ELSE clause - even if the ELSE clause only contains a comment stating that it was 'intentionally left blank'.

I have heard arguments for both sides: The 'For' camp - ensures that the codes has actually addressed whether the condition requires an ELSE clause The 'Against' camp - code is harder to read, adds too much noise

I am interested in any other points of view as I have to resolve this debate with an answer that would satisfy both parties.

Thank you for your help.

BTW: I did search StackOverflow for an answer to this and was unable to find one. If there is one, just include a link to it and close. Thanks.

+82  A: 

Seems like useless typing to me... and a possible cause for confusion. If you don't need it, don't put it!

jldupont
That's my sentiment as well. If it's not needed, no point in putting a blank else clause.
Matt Davis
... and in Erlang, I seldom have to resort to an if statement... and much less to an `else` :-)
jldupont
Agreed. Code you create is code you need to maintain. Leave it out.
Ryan Taylor
I had a boss that DEMANDED empty else's be placed. "if you have an 'if', you need an 'else' even if it's empty". What possesses people to think this?
splatto
+9  A: 

As you say, this may be a question of style, but I would not dream of putting in empty else-blocks in my code just because "every if-block should have one". In my opinion, it adds nothing else than some more characters in the code and one more point (of very little value) to spend time on during code reviews.

Fredrik Mörk
+32  A: 

No. If you don't need to run any code on the else side, you don't need an else clause.

Robert Harvey
+2  A: 

No. Guard conditions are a great example. You could next the rest of the method logic in else clauses but it could get ugly really quickly.

peacedog
+4  A: 

"ensures that the codes has actually addressed whether the condition requires an ELSE clause"

This is no more true than requiring a catch clause in every method ensures that all possible exceptions has been properly handled.

Michael Petrotta
+9  A: 

Are you talking about Algol-derived languages?

In Lisp, I'd say yes: every IF should have an else-clause. Otherwise, you should be using WHEN.

Ken
+8  A: 

The golden rule: put it in if it makes your code clearer and easier to understand, and leave it out otherwise. An experienced programmer will be able to make these judgements on a case-by-case basis.

Tim
My general rule of thumb is 3+ else if gets a potentially empty else block to show others my logic.
Woot4Moo
@Woot4Moo: How does a potentially empty else block show others your logic?
Asaph
+1  A: 

If your code is complex enough that this becomes an issue, you should probably be rethinking your approach to the problem. Break what you're doing down into smaller simpler functions where it should be unbelievably obvious what the if statements are doing.

If you can't break it down into smaller functions, or you find yourself nesting more than one if statement inside another, Using if statements for your conditional logic are probably not a good fit. Consider switches, lookup tables (if the only difference between your conditions is the value of some constant), or decision tables instead.

If you've already done all this, then you are quibbling over something so unbelievably trivial it's hardly worth your time to argue it.

Breton
Trivial, yes. However when management is determined to force developers to standards such as these, even the trivial (and stunningly stupid) will become critical decisions.
Jack
The usual problem defending against pointy-haired bosses on technical issues like this is that engineers use logic and reason. Unfortunately, when one is battling superstition and authority, those will never succeed. Instead, use their point-of-view against them: ask, "What is the business case?" "How does going against established efficient industry-standard practice make our product cost less or be more valuable?" Having more code is more places for bugs, and slower engineer comprehension.
wallyk
+6  A: 

Requiring an else stinks. Use it when needed. All programmers understand the construct and the implication of a missing else. It's like a pointless comment that echoes the code. It's plain daft IMO.

spender
A: 

One of the few possible situations where this might be a good idea is where you have several nested if statements, but fewer else clauses. The language will specify which if the else matches, but this may not always be clear to the reader. Of course, where you put content in curly brackets, nesting will be obvious, so there's no ambiguity. This make this a bit of an artificial case, but still possibly worth mentioning. Also, if your code is this complicated, there's probably a clearer way of writing it.

Tim
I agree with the last sentence here. Any method where this is needed is too complex and should be refactored.
Rasmus Kaj
that was essentially the point I was trying to make.
Breton
+15  A: 

It is clear by the responses here that no one feels an unused else is needed. I have never heard nor read of such a thing. The more important issue before you is dealing with co-workers/developers who somehow believe adamantly that this is how If Then is to be used.

It sounds like you are not the senior person in this scenario so you cannot simply decree it to be so. I suggest asking the "empty else" parties to show where such a process is suggested in a book (blogs don't count) about development. Better yet, in 3 books about development. I have read my share of programming books across programming languages since 1982 and I have never seen such a suggestion.

This way you are not telling them that they are wrong which they could take personally. Instead you are willing to accept such a position but would like to see some documentation. The onus is on them to find the proof. Either they find it or they are left to argue that every programming book ever written is wrong while only they are right.

Good luck.

dredful
+2  A: 

there are a lot of "words" telling you the way to programming such as DRY

in this case i'd use YAGNI.. you aint gonna need it..

so following this you shouldn't write the else.

anyhow in my opinion it makes it harder to read and understand the code.. the more you write the harder to understand the code is

edit: here are the links you requested: http://en.wikipedia.org/wiki/YAGNI http://en.wikipedia.org/wiki/Don%27t_repeat_yourself

Dimitri
Please link or explain your terms
Alex Brown
+1  A: 

SQL Server 2000, 2005 at least.

IF 1 = 1
BEGIN
    PRINT 'doing something productive'
END
ELSE
BEGIN
    --Just sitting here
END


Msg 102, Level 15, State 1, Line 8
Incorrect syntax near 'END'.

You have to have a meaningful statement, which means a dummy assign or return data to client. I suppose I could use WAITFOR DELAY...

gbn
+1  A: 
if (thereIsSomeThingToDoInTheElse)
{
    putAnElseClause();
}
else
{
    // intentionally left blank
}
JoelFan
*shudder* that's going to be fun to debug, when the next line after the comment is executed as an "else" when that clearly wasn't intended
Chad
Yeah... I added braces to fix it... thanks
JoelFan
This edit was probably one of the top reasons against empty else clauses
Rado
+4  A: 

I tend to use "early if" statements as means of reducing the level of nested braces (or indentation in Python) like so:

if (inParam == null) {
  return;
}

if (inParam.Value < 0) {
  throw new ArgumentException(...,...);
}
// Else ... from here on my if statements are simpler since I got here.

Of course, .Net 4.0 now has code contracts, which is great! But, most languages do not have that just yet, so "early ifs" (for the lack of better term) are great precisely because they eliminate a number of else clauses and nested ifs. I do not think it is beneficial to have an else clause in high-level languages ... heck, even in assembly! The idea is: if you checked and did not jump, then the condition was false and we can carry on. Makes logical sense to me ...

Hamish Grubijan
+1: I like this technique too.
jldupont
+2  A: 

Haskell's if is always ternary. So else is mandatory.

Hai
A: 

There are situations where using an optional syntax element when not required can improve readability or prevent future errors. A typical case are the brackets around one-sentence conditionals:

Doing

if(foo){
    bar
}

instead of

if(foo)
    bar

could eventually prevent

if(foo)
    bar
    dot

I can't really think of any language I know where omitting an unnecessary else can lead to potential errors :-?

Álvaro G. Vicario
A: 

Having an "else" with just an empty line of a code comment can usually mean the developer thought-through the condition and has a better idea of what execution path is actually taken at a given time. All recent compilers will remove the "else" and the comment, so you are not slowing software execution.

Unless I am reading the other responses incorrectly, it looks like most folks are objecting to the time it takes to declare their thoughts in the code and would rather just do it.

ScSub