Guide section 5.1 on object destructuring may lead to anti-pattern
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 45/100
- Tipo di issue
- Documentazione
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Stack tecnologico
- javascript
- Ambito
- documentation
Direzione di ricerca
Inizia dalla sezione 5.1, «Use object destructuring when accessing and using multiple properties of an object», in README.md e confronta le indicazioni con gli esempi di questa issue. Esamina i casi di anti-pattern proposti e aggiungi indicazioni che spieghino quando può essere preferibile l’accesso diretto alle proprietà, con un esempio chiaro della regola prevista.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- JavaScript
- Stelle
- 148k
- Fork
- 26.6k
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di airbnb/javascript
-
Severity: Unhandled promise rejection in `whitespace-async.js` when ESLint async path is usedForse già presa @bodapatisaikrishna l’ha presa 24 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
airbnb/javascript#3237 · 6 commenti ·
-
Inconsistent semicolon usage in examples (Arrays vs Functions)Forse già presa @Developer-shivamMishra l’ha presa 14 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
airbnb/javascript#3152 · 4 commenti ·
-
No error handling around execSync + JSON.parse in whitespace.js (ESLint 9 path)Forse già presa @dataCenter430 l’ha presa 219 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 48/100
airbnb/javascript#3238 · 8 commenti ·
-
Upgrading eslint-plugin-react-hooksForse già presa @weihongyu12 l’ha presa 365 giorni fa. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 38/100
airbnb/javascript#3186 · 3 commenti · 2 reazioni ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
airbnb/javascript#3173 · 9 commenti · 1 reazione ·
Tutte le issue di airbnb/javascript
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
dusk-network/exu#17 ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
jspreadsheet/ce#1809 ·
-
documentation
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 91/100
githubnext/gh-aw-workshop#4458 ·
I maintainer di solito rispondono entro 1 giorno
-
Add: CartoonitoApertacheck:failed feeds:add
Difficoltà 2/5 1-3 ore Idoneità per principianti 63/100
iptv-org/database#37390 · 1 commento ·
I maintainer di solito rispondono entro 9 giorni
-
bug: directory index route root priority is overwritten when wildcard is falseForse già presa @TalhaHunter101 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
fastify/fastify-static#617 ·