DEV Community

Andreas Riedmüller
Andreas Riedmüller

Posted on

Destructuring can worsen code readability

Destructuring objects is handy and often code is easier to read when it looks simpler.

But destructured variables lack context and this can worsen code readability.

Here is an example:

// A.)
const {title} = game;
// A FEW LINES LATER…
const text = title; // Title what?
Enter fullscreen mode Exit fullscreen mode
// B.) 
const text = game.title; // It's the title of the game!
Enter fullscreen mode Exit fullscreen mode

Use destructuring when the context is obvious and your code gets hard to read without.

This post is mostly a reminder to myself not to always deconstruct everything. Because I just had to trace a number of variables to understand where they came from.

Happy coding!

Oldest comments (0)