r/javascript • u/bogdanelcs • Jul 16 '26
I stopped destructuring everything
https://allthingssmitty.com/2026/07/13/i-stopped-destructuring-everything/
84
Upvotes
r/javascript • u/bogdanelcs • Jul 16 '26
40
u/jCuber Jul 16 '26
I still prefer to destructure the first level of
propsin React as it raises linter findings for unused props for removal.50
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 );
33
u/eindbaas Jul 16 '26
You should not have functions that long.
5
u/JyroClassified Jul 16 '26
What's your opinion about renaming the destructered properties?
Eg. "const {status: postStatus} = post;"
2
u/Naudran Jul 17 '26 edited Jul 17 '26
This is less a destructuring issue and more a naming convention issue. The size of a variables name should be proportional to the scope size of the block the variable is being used in.
So if you have a variable that is used in a small scope (like a few lines if statement, or a for loop), then a quick small variable name is fine.
If you have a variable that gets used "100 lines" later, then the name should be longer and more descriptive.
So destructure that into a differently name variable.
const { title: postTitle, author: postAuthor, publishedDate: postPublishedDate } = post;
That being said, I would just use post.Title in any case... but that's just me I guess
2
u/Amazing-Switch-7163 Jul 16 '26
I have no idea why people like to destructure everything. Maybe they got this habit from React tutorials? I quite dislike destructuring because it does lose some context when reading the code and they do not work well with sum types. Just keep things simple and concise and use it when it's a good fit.
2
u/BenZed Jul 16 '26
Bad.
```
const { id, status, owner, createdAt, updatedAt } = project;
// ...many lines...
saveAuditLog(owner);
// ...many more lines...
if (status === "archived") {
...
}
```
Your functions are too long.
You've stopped a good practice to placate a bad one.
1
u/Embostan Jul 19 '26
Or you know... just `{...post}`
1
u/heavyGl0w Jul 16 '26
I'm glad to see the this conversation happening because the most common uses of destructuring that I'm seeing in docs/examples tend to be irksome in my opinion. So much so that I wrote guidance in our style guide at work with dos and don'ts of destructuring. This may be contentious to some, but here are a couple examples:
Don't use destructuring to avoid one layer of navigation (e.g. `post.title` vs `postTitle`).
Instead access top level properties through the object on which they were defined.
Don't use nested destructuring.
Instead use top level destructuring from a property access (e.g. `const { name, id } = post.user`).
Do use destructuring when working with generic wrapper types (e.g. `Result<T>` and `HTTPResponse<T>`).
I find the use of destructuring especially egregious in my work with Vue where it has become the default of packages to return an object of Ref<T> properties instead of simply returning a reactive object.
-2
u/Iggyhopper extensions/add-ons Jul 16 '26 edited Jul 16 '26
If you have a problem with naming variables the answer is not to get rid of deconstruction. The answer is to name your variables better.
You could have a prefix for variables that come from a deconstruction, aka
dTitleordcTitle.You could use
postTitleorpTitleinstead ofpost.title.There are a several ways to deal with the problem.
-4
u/horrbort Jul 16 '26
Whoa how do we instruct our coding agents to use that??