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.

This construction is considered wrong

unless
  #..
else
  #..
end

This is for the general reason of double-negative confusions. It translates to:

if not 
  #..
if not not
  #..
end

We all agree this is confusing.


Is there any situation (include semantic) that the unless..else construction would be more appropriate?

share|improve this question
add comment

3 Answers

I've not worked with ruby, but Perl has a similar construct which has the same kind of baggage around it.

There is no structural difference between these 3 constructs:

if condition
    OneThing()
else
    AnotherThing()

unless condition
    AnotherThing()
else:
    OneThing()

if not condition
    AnotherThing()
else:
    OneThing()

However, semantically, I think the reason that people recommend it is that unless condition is really just syntactic sugar for if not condition. The difference is that not every language has unless so the latter is more instantly readable to a new programmer. When introducing unless it is usually in the context of the above definition.

Using unless then else just confuses things further, because while unless is a helpful shortcut, unless else adds confusion by having an unfamiliar construct used in an unneccessary way.

Any unless then else can trivially be rewritten as if then else, so I would suggest that there is no case where unless then else improves the readability or computability of the code.

share|improve this answer
1  
Right. You can always switch the branches around once you have two of them. unlesselse is confusing in English as well, using it in a program isn't better. (I haven't seen or heard it used in either speech or writing, there's probably a reason for that!) –  Jörg W Mittag Jul 11 '13 at 23:28
2  
Unless you go out of your way, its uncommon to use "Unless" in English. I do like it in programming as can help clarify singleton branches, especially with lots of possibilities; eg. unless 1 of 100 options: do a thing. But unless used properly, it can confuse readability, otherwise it is fine! –  Lego Stormtroopr Jul 11 '13 at 23:35
1  
Interesting usage there :-) Would it be better if it was unlessotherwise instead of unlesselse? –  Jörg W Mittag Jul 11 '13 at 23:38
    
@JörgWMittag Yeah, that was kind of on purpose. But I doubt changing unless else to unless otherwise would improve readability at all. I think just write if not then else and be done with it. –  Lego Stormtroopr Jul 12 '13 at 3:45
1  
I do use unless .. otherwise in some edge-cases of common speech. Maybe more as a warning: "Unless you [blah] such thing will happen, otherwise it will turn out OK" –  New Alexandria Jul 12 '13 at 16:49
show 1 more comment

No.

Any construct of that type can be rewritten in your second form, with better clarity.

In most languages, the if not not collapses into a simple if anyway, so the mind-bending consequences of unless else become especially pointless.

share|improve this answer
add comment

You don't want "unless" because it easily leads to confusion because an if condition with a built in negative would be just bad design. Also because it's easy to just use an "if".

You would get constructs like:

unless !(x=3) {

unless(item.isnotred){

which you have to read twice, where an easy:

if (x=3)

if (item.isred){

is very clear.

Unless operator leads to double negatives which should be avoided.

share|improve this answer
add comment

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.