More compact "if...else if" when executing same code
I have a JavaScript calls structured this way:
if (($(this).scrollTop() == 0) && !controlsVisibility) {
triggerControls();
}
else if (currentScroll > (previousScroll + 100) && controlsVisibility) {
triggerControls();
};
While triggerControls() does just-in-case typecheck for undefined, and
uses controlsVisibility as default arg determine what exactly it is
supposed to do. I think:
Did I made a mistake of not passing controlsVisibility as a function arg
inside if clause. If value of that variable changes between I call
triggerControls() and function's execution (microsecond?) — should I:
account for the possible change by using the global state (as it is now)
or
interfere the change by passing stable args in advance?
I understand that this might be determined on case-by-case basis, but I
would really appreciate some tips.
If the current implementation (1) is OK
I could've written both scenario checks in one if just by using || as I am
executing the same function. Except for being messy and making the code
largely unreadable why shouldn't I do just that?
No comments:
Post a Comment