Github標(biāo)星7.9K!程序員專屬的命名寶典來(lái)了
開(kāi)源最前線(ID:OpenSourceTop) 猿妹編譯
地址:https://github.com/kettanaito/naming-cheatsheet


/* Bad */
const primerNombre = 'Gustavo'
const amigos = ['Kate', 'John']
/* Good */
const firstName = 'Gustavo'
const friends = ['Kate', 'John']
/* Bad */
const page_count = 5
const shouldUpdate = true
/* Good */
const pageCount = 5
const shouldUpdate = true
/* Good as well */
const page_count = 5
const should_update = true
短:輸入一個(gè)名稱一定不要花太長(zhǎng)時(shí)間,因此一定要簡(jiǎn)短
直觀:名稱讀起來(lái)一定要直觀,盡可能貼近日常用語(yǔ)
描述性:名稱必須可以用最有效的方式反映它的作用
/* Bad */
const a = 5 // "a" could mean anything
const isPaginatable = a > 10 // "Paginatable" sounds extremely unnatural
const shouldPaginatize = a > 10 // Made up verbs are so much fun!
/* Good */
const postCount = 5
const hasPagination = postCount > 10
const shouldPaginate = postCount > 10 // alternatively
/* Bad */
const onItmClk = () => {}
/* Good */
const onItemClick = () => {}
class MenuItem {
/* Method name duplicates the context (which is "MenuItem") */
handleMenuItemClick = (event) => { ... }
/* Reads nicely as `MenuItem.handleClick()` */
handleClick = (event) => { ... }
}
/* Bad */
const isEnabled = itemCount > 3
return <Button disabled={!isEnabled} />
/* Good */
const isDisabled = itemCount <= 3
return <Button disabled={isDisabled} />
評(píng)論
圖片
表情
