Naming cheatsheet

# Naming cheatsheet Naming things is hard. This sheet attempts to make it easier. Although these suggestions can be applied to any programming language, I will use JavaScript to illustrate them in practice. ## Naming convention Pick **one** naming convention and follow it. It may be `cammelCase`, or `snake_case`, or anyhow else, it does not matter. What matters is for it to remain consistent. ```js /* Bad */ const pages_count = 5 const shouldUpdate = true /* Good */ const pagesCount = 5 const shouldUpdate = true /* Good as well */ const pages_count = 5 const should_update = true ``` ## S-I-D A name must be _short_, _intuitive_ and _descriptive_: - **Short**. A name must not take long to type and, therefore, remember; - **Intuitive**. A name must read naturally, as close to the common speech as possible; - **Descriptive**. A name must reflect what it does/possesses in the most efficient way. ```js /* Bad */ const a = 5 // "a" could mean anything const isPaginatable = postsCount > 10 // "Paginatable" sounds extremely unnatural const shouldPaginatize = postsCount > 10 // Made up verbs are so much fun! /* Good */ const postsCount = 5 const hasPagination = postsCount > 10 const shouldDisplayPagination = postsCount > 10 // alternatively ``` ## Avoid contractions Do **not** use contractions. They contribute to nothing but decreased readability of the code. Finding a short, descriptive name may be hard, but contraction is not an excuse for not doing so. ```js /* Bad */ const onItmClk = () => {} /* Good */ const onItemClick = () => {} ``` ## Avoid context duplication A name should not duplicate the context in which it is defined. Always remove the context from a name if that doesn't decrease its readability. ```js class MenuItem { /* Method name duplicates the context (which is "MenuItem") */ handleMenuItemClick = (event) => { ... } /* Reads nicely as `MenuItem.handleClick()` */ handleClick = (event) => { ... } } ``` ## Reflect expected result A name should reflect the expected result. ```jsx /* Bad */ const isEnabled = itemsCount > 3 return