-
Notifications
You must be signed in to change notification settings - Fork 5
Rules that cannot be coded.
A verb gives clear indication of an intent to make an action. A function name that consists of a verb and a noun makes reading code a lot more like reading nature language.
const precision = (value) => {
return (String(value).split('.')[1] || '').length;
};
console.log(precision('1.123'));
Just by looking at the precision
value, I cannot tell whether precision is a value or a function used to derive the value.
const getPrecision = (value) => {
return (String(value).split('.')[1] || '').length;
};
console.log(getPrecision('1.123'));
It is always clear that getPrecision
is a function used to get a value.
When variable name includes meta information (e.g. type of the variable, constructor name) order parts of the variable in a descending order of their scope, e.g.
# Good
const fixtureOrderEvents = {};
# Bad
const orderEventsFixture = {};
fixture
is the type of a variable. orderEvents
is the contents of the value. Therefore the variable must be fixtureOrderEvents
.
You can recognize this pattern in NPM project naming, e.g.:
- react
- react-redux
- react-redux-router
# Good
type TypeResultSearch = Object;
# Bad
type ResultSearch = Object;
type TypeSearchResult = Object;
type SearchResult = Object;
It is tempting to leave out Type
out of the name of the variable entirely. At the time of the variable type declaration, it seems superfluous. However, in the context of the code, variable ResultSearch
does not provide enough information about the purpose of the variable (it could be a constructor).
There are many ways a word can be abbreviated. Abbreviations save few characters at the cost of obscuring the scannability of the code.
Using words in their full form brings closer to reading a natural language.
(err) => {};
(error) => {};
Do not use JSDoc to document parameters.
// Runs PhantomJS process to convert/download PDF invoices.
// query must contain:
// cookies - array of cookie objects
// urls - array of pairs { url, file }
export default (query) => {
/**
* Runs PhantomJS process to convert/download PDF invoices. Query must contain:
* @param {Object[]} cookies
* @param {Object[]} urls
*/
export default (query) => {
type TypeUrl = {
url: string,
file: string
};
type TypeQuery = {
cookies: Array<Object>,
urls: Array<TypeUrl>
};
/**
* Runs PhantomJS process to convert/download PDF invoices.
*/
export default (query: TypeQuery) => {
When a type specific description is need, use @property
tags, e.g.
/**
* @property urls List of target sites.
*/
type TypeQuery = {
cookies: Array<Object>,
urls: Array<TypeUrl>
};
BDD is not a collection of "should" or "must" assertions: BDD is a story. A test case describes a specific event that makes a story. Therefore:
# Good
it('converts number to a string', () => {});
it('removes accented characters', () => {});
# Bad
it('should convert number to a string', () => {});
it('should remove accented characters', () => {});
it('must convert number to a string', () => {});
it('must remove accented characters', () => {});