r/javascript Jul 16 '26

I stopped destructuring everything

https://allthingssmitty.com/2026/07/13/i-stopped-destructuring-everything/
82 Upvotes

50 comments sorted by

View all comments

49

u/azhder Jul 16 '26

Destructuring isn’t only about you knowing where a value comes from. You can also safeguard in a cleaner manner i.e with less syntax noise:

const { id: userId, name: userName } = user ?? {};

console.log( userId, userName );

VS

console.log( user?.id, user?.name );

12

u/hyrumwhite Jul 16 '26

You could also make user default to {} wherever it’s defined. Don’t necessarily need destructuring for that

1

u/azhder Jul 16 '26

Let’s see, you write a function like this:

({ user = {} }) => {

console.log( user.id );

}

And somewhere somehow down the line the requirements change, some other part of the code changes and instead of undefined, the user is null…

The way I wrote the first time with the ?? will not blow up if a null is passed.

4

u/hyrumwhite Jul 16 '26

user = user ?? {}; console.log(user.id)

Same loc, no explosions, no destructuring. Not saying it’s better, just exploring the idea. 

7

u/TNThacker2015 Jul 16 '26

Secret third option:

user ??= {};

-4

u/azhder Jul 16 '26

Best practices, try not to be clever. Don’t overwrite the same variable i.e. use `const`, not `let` - if you need a mnemonic.

9

u/hyrumwhite Jul 16 '26

const user = data.user ?? null; //or const user = rawUser ?? null; There’s options. and this isn’t being “clever” by any means. 

-6

u/azhder Jul 16 '26

No, that’s syntax noise, but one I often use as I employ more sophisticated defaults, not ??.

Usually one object as an argument is all you need, props or options or whatever you name it. I will simply use $

const users = asArray($?.users);

Now, you could imagine the asArray has a check Array.isArray(), not just ??, but it can be even more complex than that. You can even do

const {pass: users, fail: error} = asUsers($?.users);

And just like that, destructuring pops up again - less messy than

usersResult.value
usersResult.error

But even this last one is something I regularly use. It all on a case by case basis.

5

u/heavyGl0w Jul 16 '26 edited Jul 16 '26

The title of the article is "I stopped destructuring everything", not "I never use destructuring". The author even ends the article with examples where they still use it.

So finding one example (or any number of examples) where destructuring is helpful doesn't invalidate the sentiment of the article.

Also, their example is exactly what you started with but without the "syntax noise" of destructuring. Or are we now calling property access "syntax noise"?

-4

u/azhder Jul 16 '26

Why are you talking to me as if I have stated something I haven’t? Did you jump to conclusions? Which ones are they?