2 ms·
When formatted like this: result = (cond_A ? 1 : cond_B ? 2 : cond_C ? 3 : 5) they look quite a bit
by ced 7y ago
When formatted like this:
result = (cond_A ? 1 :
cond_B ? 2 :
cond_C ? 3 :
5)
they look quite a bit nicer than the if/else equivalent IMO.
- mikekchar 7y agoThis is actually nice because in some languages a case statement is a statement and not an expression -- so you can't assign the value easily. +1, would use this in production code :-)
- AstralStorm 7y agoAnd when conditions are not direct don't forget to add Lisp level of parentheses. I bet one time you will forget and everything will explode.
- klingonopera 7y agoIs everyone preferring to state these on multiple lines? With the advent of the widescreen monitor, I find we have all the more reason today to use all that extra space. Using the example from the article, I'd refactor: const result = (!conditionA) ? ("Not A") : (conditionB ? "A & B" : "A"); or, since code should be simple to read: const result = (conditionA) ? (conditionB ? "A & B" : "A") : ("Not A"); All the parentheses' are redundant for the compiler or computer, but remind myself as a human that "const result" is not a direct statement, but a result of a conditional expression. EDIT: 2nd set of parentheses' in 2nd expression might not be redundant.