3 ms·
there were 7. the formatting took multi-line options and made them one line so I just deleted them... while I was removing them the edit option went away. oh-we
by shawndumas 14y ago
there were 7. the formatting took multi-line options and made them one line so I just deleted them... while I was removing them the edit option went away. oh-well.
b was the 1st option but on 5 lines; d was the 2nd but on 4 lines.
e is now the 3rd option which is the ternary; and I am unsure of why you are saying that it implies anything about a return value.
in javascript the 4th will evaluate the left side of the and. if it is true it will evaluate the center. if the center is true it will evaluate the right side of the and which will invoke the function. since the function returns a 'truthy' value in javascript the other side of the or does not need to be evaluated because of 'short circuiting'.
if either of the first two parts of the and are false javascript will 'short circuit' the and and move on to the right side of the or and evaluate it which will invoke that function.
you are correct about the last option -- accidentally I left off the invoking parenthesis.
FYI, I like the third option but went with what was b:
if (data.IsActive && !data.IsLocked) {
lockBtn.show();
} else {
lockBtn.hide();
}
so all and all it was a complete failure...
- mooism2 14y agoOption 3/e: it's idiomatic in C-syntax languages to use if/then/else when you do not care about the return value. Use of ?: therefore implies that you do care about the value of the expression. When I see the result of ?: being thrown away, I have to ask myself whether it is supposed to be an if/then/else, or whether a previous programmer meant to do something with the result, but forgot. Option 4/f: if .show() always returns a truthy value, then yes, this is the same. Even if I know this is true, I must still mentally translate it into the idiomatic form. If I don't know this is true, I must waste time finding out. And if one day .show() doesn't return a truthy value --- perhaps because a different part of the codebase has been changed so that lockBtn is of a different class --- then your code will break.
- shawndumas 14y agoall true; that's why I went with b.