Re:Using the switch(true) pattern in JavaScript
by tpoacher on Monday April 12, 2021 @03:41PM
(#26784162)
I agree, but also I think this is misrepresenting the 'early return' pattern. It's more likely to be succinct and look like this instead:
In _this_ scenario, it is true that this is much more readable than:
[ Parent
| on HN ]
Re:Using the switch(true) pattern in JavaScript
by tsimionescu on Monday April 12, 2021 @03:51PM
(#26784272)
Never ever use if without curly braces. That's one of the worst ideas of C's syntax.
Your first example would still look clean like this:
function check(x) {
if (!test1 (x)) { return false; }
if (!test2 (x)) { return false; }
if (!test3 (x)) { return false; }
if (!test4 (x)) { return false; }
return true;
}
And it would be much safer from stupid copy/paste errors.
Re:Using the switch(true) pattern in JavaScript
by jhgb on Monday April 12, 2021 @05:53PM
(#26785659)
Instead of
function check(x) {
if (!test1 (x)) return false;
if (!test2 (x)) return false;
if (!test3 (x)) return false;
if (!test4 (x)) return false;
return true;
}
why wouldn't you do something along the lines of (every (lambda (f) (funcall f x)) (list #'test1 #'test2 #'test3 #'test4a))
or whatever is the equivalent in your preferred language?
Re:Using the switch(true) pattern in JavaScript
by wvenable on Monday April 12, 2021 @06:03PM
(#26785743)
The tests are rarely that simple or regular.
Re:Using the switch(true) pattern in JavaScript
by jhgb on Monday April 12, 2021 @06:25PM
(#26785943)
Isn't that exactly the reason to put them somewhere else?
Re:Using the switch(true) pattern in JavaScript
by wvenable on Monday April 12, 2021 @06:30PM
(#26785995)
No, I just mean the actual tests might be something like:
if (user == null) return false;
if (IsCompleted(user)) return true;
if (action == null) return false;
if (value < 0 || value > 100) return false;
You're not usually just passing a single value to a bunch of "test" functions.
Re:Using the switch(true) pattern in JavaScript
by jhgb on Monday April 12, 2021 @08:18PM
(#26786740)
Well, the way it was written, I was assuming that conditions to be satisfied had separate named predicates (although combinators could alleviate even some of these things).
Re:Using the switch(true) pattern in JavaScript
by brundolf on Monday April 12, 2021 @03:44PM
(#26784191)
Worth noting that elses don't require curly braces, which are where most of the visual noise is coming from in your example:
function check(x) {
if (!test1 (x)) return false;
else if (!test2 (x)) return false;
else if (!test3 (x)) return false;
else if (!test4 (x)) return false;
else return true;
}
I think this is more clear than either of the above
Re:Using the switch(true) pattern in JavaScript
by tpoacher on Monday April 12, 2021 @03:49PM
(#26784249)
> Worth noting that elses don't require curly braces
True. I still think the first is clearer though. Either all tests pass, or they don't.
Re:Using the switch(true) pattern in JavaScript
by tpoacher on Monday April 12, 2021 @03:50PM
(#26784257)
(also I'd be very skeptical of such an if..else ladder. Prime target for bugs.)
Re:Using the switch(true) pattern in JavaScript
by alerighi on Monday April 12, 2021 @07:10PM
(#26786293)
That code is ugly anyway, something I would have wrote in middle school... why not:
function check(x) {
return test1(x) && test2(x) && test3(x) && test4(x);
}
Or, since I assume is JavaScript, just: const check = x => test1(x) && test2(x) && test3(x) && test4(x);