r/javascript • u/AllThingsSmitty • 10d ago
Hot take: Sometimes three boring statements beats one clever expression
https://allthingssmitty.com/2026/11/16/sometimes-three-boring-statements-beat-one-clever-expression/Sometimes the simplest code is the best code. It's tempting to squeeze everything into one clever expression, but three straightforward statements can be easier to understand and maintain. Here's a look at why.
5
u/SquarePixel 10d ago
I agree with the overall sentiment, and with your example to avoid nesting ternary expressions (to avoid needing to remember order of operations or do paren tracking).
But I’d argue nullish coalescing (??) with optional chaining is trivial to read and understand from left to right.
2
2
u/dronmore 9d ago
Chaining ternary operators is just as straightforward as chaining null coalescing ones. You can learn to read it. I don't encourage you to write it, but at least learn to read it. It's simple.
const a = false ? "no" : false ? "no" : false ? "no" : false ? "no" : false ? "no" : true ? "yes" : false ? "no" : false ? "no" : "no";1
u/SquarePixel 9d ago
You are right, with good spacing it’s easier to read.
Though, `a && b ? x : c && d ? y : z` still trips me a bit because I want to make it sure I’m not screwing up how && binds.
1
u/dronmore 8d ago
Yeah, that line is ugly, but it's not magic. Logical operators have higher precedence than the ternary, so they collapse to a single value first. You can wrap them in parentheses to emphasize the precedence if that helps.
const ugh = (a && b) ? x : (c && d) ? y : zAnd then add more parens to emphasize the right-to-left associativity of the ternary operators.
const ugh = (a && b) ? x : ((c && d) ? y : z)And one more pair to show that the assignment is also right-to-left associative.
const ugh = ((a && b) ? x : ((c && d) ? y : z))
1
u/Discordbothost 9d ago
The getFallbackTheme() call is what I'd pin down in a test. I'd check that it's skipped with a saved preference, and called once when both settings are missing. That catches an eager refactor even if the final theme still looks right.
1
u/HipHopHuman 5d ago edited 2d ago
I'm a bit late to the discussion here; but couldn't you use ??= to collapse all those if statements?
let theme = config?.theme;
theme ??= user?.preferences?.theme;
theme ??= getFallbackTheme();
theme ??= defaults.theme;It's only slightly more verbose than your original example, but in 4 lines instead of 5, and you still get to keep using debugger breakpoints.
EDIT: You could also start with null in the original code and invert it so the ?? is on the left. I find this somewhat easier to parse
const theme = null
?? config?.theme
?? user?.preferences?.theme
?? getFallbackTheme()
?? defaults.theme;1
u/spatel_21 3d ago
One edge: ??= treats an empty string as set; add a check if empty theme strings should fall through.
1
u/HipHopHuman 2d ago
One edge: ??= treats an empty string as set; add a check if empty theme strings should fall through.
??=does not treat an empty string as set. According to your claim, the following code should output "false", but it outputs "true":let foo = null; foo ??= ''; foo ??= 'test'; console.log('' === foo); // true
9
u/JouleV 10d ago
In other news water is wet?
If you are sending me Fast Inverse Square Root in a PR I'm rejecting it. Unless ultra hardcore optimisation is needed (e.g. trading, running on limited resources)