Artem Zakharchenko d75d867ea5 Update README.md
2017-06-30 11:53:09 +02:00
2017-06-30 11:53:09 +02:00

Naming cheatsheet

Naming things is hard. Is it?

Guidelines

  • Pick one naming convention and follow it. Whether it is likeThis, or like_this, or anyhow else, it does not matter. What matters is consistency in your work.
  • Name, whether of a variable, method, or something else, should be short, descriptive and intuitive:
    • Short. Variable should not take long to type, and therefore to remember,
    • Descriptive. Name of the variable should reflect what this variable possesses/does in the most efficient way,
    • Intuitive. Name of the variable should read naturally, as close to common speach as possible
/* Bad */
const a = 5; // "a" could mean anything
const isPaginatable = (a > 10); // "Paginatable" sounds extremely unnatural

/* Good */
const postsCount = 5;
const shouldDisplayPagination = (postsCount > 10);
  • Name should not duplicate the context when the latter is known, and when removing the context from the name does not decrease its readability:
class MenuItem {
  /* Method name duplicates the context it is in "...MenuItem..." */
  handleMenuItemClick = (event) => { ... }
  
  /* This way it reads as MenuItem.handleClick() */
  handleClick = (event) => { ... }
}
  • Name should reflect expected result:
/* Bad */
const isEnabled = this.props.enabled;
return (<Button disabled={!isEnabled} />);

/* Good */
const isDisabled = this.props.disabled;
return (<Button disabled={isDisabled} />);

Pattern

prefix? + action (A) + high context (HC) + low context (LC)

Example

Name Prefix Action High context Low context
getPost get Post
getPostData get Post Data
handleClickOutside handle Click Outside
shouldDisplayMessage should Display Message

Naming methods

Action

get

Access data immediately (i.e. shorthand getter of internal data).

function getFruitsCount() {
  return this.fruits;
}

fetch

Request for data, which takes time (i.e. async request).

function fetchPosts(postCount) {
  return fetch('https://api.dev/posts', { ... });
}

set

Declaratively set variableA with valueA to valueB.

function Component() {
  this.state = { fruits: 0 };

  function setFruits(nextFruits) {
    this.state.fruits = nextFruits;
  }
}

reset

Set something back to its initial value.

const initialFruits = 5;
const fruits = initialFruits;
setFruits(10); // fruits = 10

function resetFruits() {
  fruits = initialFruits;
}

resetFruits(); // fruits = 5

compose

Create new data from the existing one. Probably, applicable mostly to strings.

function composePageUrl(pageName, pageId) {
  return `${pageName.toLowerCase()}-${pageId}`;
}

handle

Handler for a dedicated action. Usually, used as a callback method.

function handleLinkClick(event) {
  event.preventDefault();
  console.log('Clicked a link!');
}

link.addEventListener('click', handleLinkClick);

Prefixes

is

Describes certain characteristic of the context.

const color = 'blue';
const isBlue = (color === 'blue');

if (isBlue) {
  console.log('The color is blue!');
}

min/max

Represent minimum or maximum value. Usually describe allowed limits.

function PostsList() {
  this.minPosts = 3;
  this.maxPosts = 10;
}

should

Reflects conditional statement (returns Boolean value).

const currentUrl = 'https://dev.com';

function shouldUpdateUrl(url) {
  return (url !== currentUrl);
}
Description
Comprehensive language-agnostic guidelines on variables naming. Home of the A/HC/LC pattern.
Readme 182 KiB