Generally speaking, I would say everybody loves the destructuring assignment, and for good reasons. Next, I list some of the amazing things one can achieve with destructuring in JavaScript.
»Object destructuring
»Provide default values
»Renaming variables on creation
»Nested object destructuring ⚠️
»Array destructuring
»Works with any iterable on the right-side (e.g Set
)
»Swapping Variables
»Swapping array positions
»Skipping items ⚠️
»Nested array destructuring ⚠️
»Function parameter destructuring ⚠️
»What’s not so good here
The most significant dilemma of destructuring it’s related with the fact that relies on properties of nested structures that can only be evaluated at runtime, let me give you an example.
On the other hand, if you write this without destructuring, you won’t make assumptions on the user, you can double check whether the given user is valid.
»null - a catastrophe waiting to happen
Just a note, for you to be conscious that as in default assignment default parameters the null
value it’s not considered false, thus you can’t fall back to some default value in destructuring a property whose value is null
.
Every feature above mentioned exists for you to use, but we should find a balance (as in everything else in life). Below I list a few guidelines/rules that will help you find the balance when using destructuring.
source: https://imgflip.com/tag/fat+cat+balance
»The 3 Golden Rules
You might have noticed some previous warning sings (⚠️) in some of the listed usages of destructuring. I left those signs there because I consider those use cases potentially harmful.
- Don’t use nested destructuring on data that comes from any external data sources (such as REST APIs, GraphQL endpoints or files). You never know when these APIs change their data contracts. Hopefully all of this it’s synced between teams, but sometimes we get lost on the tiny details such as that one small property in some nested object that got renamed and instead of using
snake_case
it uses nowcamelCase
just because it made more sense from an aesthetic point of view. Now your marvelous single page application is burning and falling into pieces because you were unsafely accessing this property when you could have protected yourself against it. - Don’t use nested destructuring on function arguments that have long or complicated signatures. It makes it super hard to understand what the actually API of the function is, and you might get breaking behavior because someone decided to use your function, but in some edge case they do not provide you the valid input for you to destruct upon.
- Don’t use destructuring to retrieve a value if you rely on order. I listed the possibility of skipping elements with destructuring, it might sound tempting, but you might end up again breaking your website because your simple routine relies on the access of the Nth element of some inputted array. Also, you can still retrieve data out of the array, but it might be that in that position you don’t get what you were expecting. If you choose this path, at this point, it should also be arguable that there’s something very wrong either with your code or with the data model from where you’re your reading data, use this one with care.