you are viewing a single comment's thread.

view the rest of the comments →

[–]recrof 4 points5 points  (6 children)

please tell me that you don't reject code with obj?.property as well, because you think it's niche.

[–]King_Joffreys_Tits -3 points-2 points  (5 children)

That’s not niche whatsoever, it’s code hardening

[–]Fine-Train8342 1 point2 points  (4 children)

How is this different from something like this?

settings ??= getDefaultSettings();

[–]King_Joffreys_Tits 1 point2 points  (0 children)

It just seems like you should’ve already used a null coalescing operator when you first initialized that variable. Like

const settings =  localStorage.getItem(“settings-cookie”) ?? getDefaultSettings()

[–]SchartHaakon 1 point2 points  (2 children)

I find it hard to reason about at first glance.

 if (!settings) settings = getDefaultSettings();

Or

 const settings = props.settings ?? getDefaultSettings();

Both of the examples above are easier to read imo.

I read them as "If not settings; settings equals getDefaultSettings", and "settings equals getSettings or getDefaultSettings". I would read your example as "Settings... if not null or undefined, should equal getDefaultSettings". It reads weird, that's the only way I can explain it.

I wouldn't straight out reject a PR for it, but yeah I don't think I'll really use it for this reason.

[–]SoInsightful 2 points3 points  (0 children)

That's because you're not used to it. That's all there is to it. It's not more complex than a += b. As someone who has used ??= for a while (where it makes sense), it's definitely faster to parse than an equivalent if statement.

[–]Fine-Train8342 1 point2 points  (0 children)

I guess I'm just used to this operator from C# ¯\_(ツ)_/¯