You might not need if statements: a better approach to branching logic
hackernoon.com
hackernoon.com
I suspect this article would read very much better if you deleted it and rewrote it from scratch, omitting the cutesy crap and narrative details about your mood while you were thinking about this problem, and saying only what you need to say.
[snip] {
title: 'Has the user searched yet?',
test: !!pageData.searchKeyword,
if: {
true: {
[snip]
},
false: {
[snip]
},
},
}, [snip]
vs. // Has the user searched yet?
[snip] if (decisionData.searchKeyword) {
[snip]
} else {
[snip]
}
I expected something like a state machine, instead of OOJSON. Imagine discovering this code — having never seen it before —
and trying to work out what happens for non-logged in users that haven’t searched yet.
I'd run.Overall, this seems to advocate for moving all your branching logic into one big file. I mean - I guess there could be a situation where I would want this - but it seems decoupled from real world examples and usage.
This is functionally, not much different from a function or method with a lot of branching logic embedded in itself. this just changes the format. So I don't see how this is new or better.
So I'll share one of my favorites related to reducing if statements that I use with some regularity:
before:
if user.signed_in?
if user.admin?
do_magic
end
end
After: return unless user.signed_in? && user.admin?
do_magic