A series of examples with cats
Let's say we have a cat interface.
interface Cat {
...
}
Use nouns.
// good
const cat = new Cat()
// horrible
const dog = new Cat()
Use nouns.
Scenario: we want to use the id of the a cat
// good if id is used as a primary key in Cat class
const id = cat.id
// good if catId is used as a foreign key in another class
const catId = cat.id
// horrible
const cat = cat.id
Use is
and should
when you can.
Scenario: we want to add a boolean on our Cat class to keep track of if a cat is supposed to be displaying on our cat website
// bad
interface Cat {
...
display: boolean
}
// good
interface Cat {
...
isDisplaying: boolean
}
// good
interface Cat {
...
shouldBeDisplayed: boolean
}
Use the plural form of whatever the array contains.
// good
const cats = [smellyCat, grumpyCat]
Start with a verb. Descript what it does
// good
function getCuteCats(): Array<Cat> {
...
}
// good
function getFluffyCats(): Array<Cat> {
...
}
// bad
function cuteCats(): Array<Cat> {
...
}
When invoking one of the above functions
// good
const cuteCats = getCuteCats()
Start with an on
// good
function onCatImageClick() {
...
}
All caps
// good
const PIE = 3.1415926535897932
Add adjectives
// good
const cats = [smellyCat, grumpyCat]
const oldCats = cats.filter(cat => cat.age > 20)
// bad
const cats = [smellyCat, grumpyCat]
const updatedCats = cats.filter(cat => cat.age > 20)
// horrible
let cats = [smellyCat, grumpyCat]
cats = cats.filter(cat => cat.age > 20)
Use verb with different tense. Or words like next
, before
, etc.
// good
componentWillReceiveProps(nextProps) {
const isRenderingCat = this.props.isRenderingCat
const willBeRenderingCat = nextProps.isRenderingCat
...
}
MIT