Guide section 5.1 on object destructuring may lead to anti-pattern
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 45/100
- Issue 类型
- 文档
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- javascript
调研方向
从 README.md 中的第 5.1 节“Use object destructuring when accessing and using multiple properties of an object”开始,并将其中的指导与此 issue 中的示例进行比较。检查提议的反模式案例,并补充说明何时直接访问属性可能更合适的指导,同时提供一个清晰的预期规则示例。
由索引模型根据 Issue 内容生成。
描述
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:
- Accessed object properties have meaningful names and are each only referenced once, thus not requiring local variables.
- There are more than a handful of accessed properties
- The accessed properties are mostly used to create a new object, with or without key/value transformations
Here's an example to illustrate:
// bad
function toNewUserType(user) {
const {
id: userId,
status: userStatus,
email,
phone,
encryptedPassword,
firstName,
middleName,
lastName,
jobTitle,
birthDate,
createdAt,
updatedAt,
address,
} = user;
return {
userId,
userStatus,
email,
phone,
encryptedPassword,
jobTitle,
birthDate,
createdAt: toDate(createdAt),
updatedAt: toDate(updatedAt),
address: toNewAddressType(address),
userHash: idToHash(userId),
fullName: getFullName(firstName, middleName, lastName),
};
}
// still bad
function toNewUserType({
id: userId,
status: userStatus,
email,
phone,
encryptedPassword,
firstName,
middleName,
lastName,
jobTitle,
birthDate,
createdAt,
updatedAt,
address,
}) {
return {
userId,
userStatus,
email,
phone,
encryptedPassword,
jobTitle,
birthDate,
createdAt: toDate(createdAt),
updatedAt: toDate(updatedAt),
address: toNewAddressType(address),
userHash: idToHash(userId),
fullName: getFullName(firstName, middleName, lastName),
};
}
// good
function toNewUserType(user) {
return {
userId: user.id,
userStatus: user.status,
email: user.email,
phone: user.phone,
encryptedPassword: user.encryptedPassword,
jobTitle: user.jobTitle,
birthDate: user.birthDate,
createdAt: toDate(user.createdAt),
updatedAt: toDate(user.updatedAt),
address: toNewAddressType(user.address),
userHash: idToHash(user.id),
fullName: getFullName(user.firstName, user.middleName, user.lastName),
};
}
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.
- 主要语言
- JavaScript
- 星标
- 148k
- 派生
- 26.6k
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
airbnb/javascript 的其他 Issue
-
Severity: Unhandled promise rejection in `whitespace-async.js` when ESLint async path is used可能已有人在做 @bodapatisaikrishna 于 24 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
airbnb/javascript#3237 · 6 条评论 ·
-
Inconsistent semicolon usage in examples (Arrays vs Functions)可能已有人在做 @Developer-shivamMishra 于 15 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
airbnb/javascript#3152 · 4 条评论 ·
-
No error handling around execSync + JSON.parse in whitespace.js (ESLint 9 path)可能已有人在做 @dataCenter430 于 219 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 48/100
airbnb/javascript#3238 · 8 条评论 ·
-
Upgrading eslint-plugin-react-hooks可能已有人在做 @weihongyu12 于 365 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 38/100
airbnb/javascript#3186 · 3 条评论 · 2 个 reaction ·
-
难度 3/5 1-2 天 新手友好度 45/100
airbnb/javascript#3173 · 9 条评论 · 1 个 reaction ·
查看 airbnb/javascript 的全部 Issue
相似的 Issue
-
Progress difficulty filter lists Hard before Medium可能已有人在做 @Pandamachi 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 86/100
sysprog21/codetrial#281 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 71/100
yjh051108/dsh-routing-suite#227 ·
-
难度 2/5 1-3 小时 新手友好度 68/100
-
needs-triage release-watch
难度 1/5 1 小时以内 新手友好度 76/100
-
难度 2/5 1-3 小时 新手友好度 66/100
remoteintech/remote-jobs#2271 ·
维护者通常 1 天内回复