r/javascript • • 5d 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.

0 Upvotes

13 comments sorted by

9

u/JouleV 5d 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)

6

u/caboosetp 5d ago

Yeah, this is a good discussion to have especially with new programmers, but the "Hot take" in front of it is just comment bait.

3

u/AustinYQM 5d ago

I will only accept Fast Inverse Square Root if it includes the

/// evil floating point bit level hacking
/// what the fuck?

comments.

→ More replies (1)

5

u/SquarePixel 5d 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

u/popovitsj 5d ago

Yeah I stopped reading after that first example. That's not an improvement.

2

u/dronmore 4d 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";
→ More replies (2)

3

u/Risc12 5d ago

Hell nah, I’ll do you even better: 3 complex statements!! 3 times the good!!

3

u/azhder 5d ago

Why do you think it is a “hot take”?

1

u/Discordbothost 5d 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.

•

u/HipHopHuman 23h 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.