-
Notifications
You must be signed in to change notification settings - Fork 26.7k
Guide section 5.1 on object destructuring may lead to anti-pattern #2619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I’m not sure this is an antipattern, given the language features available to us. Just because there’s repetition doesn’t automatically mean it’s bad - it would be nice to reduce it, but in your last example, user is repeated many many times. The repetition of de and re structuring is imo much better than the repetition of the object. |
I agree with "Just because there’s repetition doesn’t automatically mean it’s bad". This issue is really to raise this discussion of whether or not this is an antipattern (should be marked with "question" label?). So thanks for your input! I disagree on your assessment on the two types of repetition though. Mostly because the |
Thanks, @pedropedruzzi, for raising this interesting point and providing clear examples. It highlights a scenario where the strict application of guideline 5.1 might lead to code that some developers find less readable or more verbose than an alternative. You're focusing on a specific pattern: transforming/mapping one object structure to another, involving many properties, where each input property is typically used only once in the output. Let's break down the trade-offs in this context: Destructuring Approach (as per 5.1 / your "bad" examples):
Direct Access Approach (your "good" example):
Is it an Anti-Pattern? Labeling it a strict "anti-pattern" might be too strong, as @ljharb pointed out, readability and preference for types of repetition can be subjective. The repetition of However, your point is valid: in the specific case of mapping many properties directly into a new object literal with minimal logic beyond simple renaming or transformation, the boilerplate of destructuring + re-structuring can outweigh its benefits in terms of clarity and conciseness compared to direct Suggestion for the Guide: Perhaps Section 5.1 could benefit from a brief note acknowledging this trade-off, without fundamentally changing the core guideline. Something like:
This acknowledges the nuance you've highlighted without creating a complex exception, emphasizing developer judgment. This seems like a valuable discussion point for the maintainers regarding whether such a clarification would enhance the guide's practical applicability. |
This issue is realted to guide section 5.1 Use object destructuring when accessing and using multiple properties of an object
Although the section rationale and examples are very clear and logical, IMO when followed unrestrictedly (or naively) it may lead to an anti-pattern with the bad features this very section is trying to avoid in the first place (repetitive code, opportunities for mistakes and unnecessary extra temporary references).
Apparently the anti-pattern arises more clearly when there is a combination of these conditions:
Here's an example to illustrate:
If the package owners agree with the problem, I believe we should be able to add some content to warn about this anti-pattern and how to avoid it.
Thanks in advance.
The text was updated successfully, but these errors were encountered: