|
|
|
|
|
by lhorie
2074 days ago
|
|
> not using if-statements gets you closer to the code-as-data concept, which opens the door for unique capabilities like modifying the code as it is being executed This is actually one of the things that makes code _less_ readable, since the order of statements in source code no longer mirror the actual behavior at runtime. IMHO, the value of the exercise is to decrease the surface area of a karnaugh map[1], since generally speaking lookup tables tend to be simpler than large multi-conditional expressions, and thus, less prone to bugs. But once you can express your conditions in terms of keys to a lookup table, then a switch statement is arguably more readable than a map since it's no longer possible to mutate the realm of possible branches at distance: function foo(bar) {
switch (bar) {
case 'a': return baz();
case 'b': return qux();
}
}
// vs
const options = {a: baz, b: qux};
function foo(bar) {
return options[bar]();
}
// somewhere else, three months later, by someone else
if (hack) options.lol = lol;
[1] https://en.wikipedia.org/wiki/Karnaugh_map |
|