JavaScript quandary
August 11, 2012 6:18 AM   Subscribe

In JavaScript, I can't think of any situation in which nested if statements can't be replaced by more concise code that employs the logical operators &&, || and !. Can you think of any?
posted by markcmyers to Technology (10 answers total) 2 users marked this as a favorite
 
You're correct. You can do all of Boolean algebra with just those three operators. The implication operator is a convenience, a shorthand.
posted by bricoleur at 6:27 AM on August 11, 2012 [2 favorites]


Best answer: bricoleur gave you the correct answer, which is true of any programming language that support if statements and boolean operators. Now, to answer the question that was not asked: while it makes no difference to a computer interpreting your code, it might make a huge difference to a human trying to understand your code, as nested if statements might be easier to follow and understand, in some circumstances.
posted by aroberge at 6:55 AM on August 11, 2012 [4 favorites]


Well mathematically you just need not (!) and one of the others but it gets incredibly confusing very quickly.

A && B is equivalent to ! ( ! A || ! B)
posted by sammyo at 7:00 AM on August 11, 2012


Best answer: I may be misunderstanding, but if you need to do anything after evaluating the first if statement but before evaluating the nested statement, that seems like not nesting the statements would force you to duplicate code.
posted by lucidium at 7:04 AM on August 11, 2012


I don't know about more concise. I don't know JavaScript, but based on my general programming knowledge: Say you have two variables, X and Y, and want to set them conditionally based on logical statements A and B. If A, X = 5, then interpret: (if B, Y = 7, else Y = 4). If not A, X = 3, Y = 2. Not sure how you'd do that more concisely without nesting the "if B" statement.
posted by supercres at 7:05 AM on August 11, 2012


Orrrr... what lucidium said.

Pony request: code excerpting.
posted by supercres at 7:06 AM on August 11, 2012


Well mathematically you just need not (!) and one of the others but it gets incredibly confusing very quickly.

All you really need is nand, aka the Sheffer stroke, though that isn't available as a single operator in any non-obfuscated language of which I'm aware.

This is only true if you live in a pure, side-effects-free world, though.
posted by kenko at 7:19 AM on August 11, 2012


Best answer: If by nested, you mean immediately nested, then no, they're equivalent.

If you have statements between the first if and the second, maybe, if you can't re-organize.

This would include both
if A ...
  something
  if B ...
     more
  }
}
and
If A ...
  If B ...
    something
  }
  something else.
}
Sometimes this is your only option, especially if some of the conditional things you want to do are costly.
posted by Mad_Carew at 7:29 AM on August 11, 2012


Yes, the question should probably be clarified whether it's about theoretical possibilities or generally good programming practices. There are rare instances where very concise statements are beneficial but unless there is a compelling reason, clarity of code should be the primary approach.
posted by sammyo at 7:42 AM on August 11, 2012


Also regarding clarity: If you are using functions with side effects in the conditional, you need to call them all up front and store the results before using those results in a syntactically compact but possibly redundant evaluation. And calling those functions might be expensive or maybe you only want some side effects to occur under condition of the results of the other functions, etc. It's much nicer to just express what you mean then create a complex logic problem for yourself.
posted by thefool at 9:06 AM on August 11, 2012


« Older What's the best place for a picnic on the Stanford...   |   Sometimes, I overshare. How do I get over it? Newer »
This thread is closed to new comments.