Skip to content

Instantly share code, notes, and snippets.

@chris2cant
Last active August 9, 2019 12:42
Show Gist options
  • Save chris2cant/c5a29a51e40ee01d4e5a98ce44a7a999 to your computer and use it in GitHub Desktop.
Save chris2cant/c5a29a51e40ee01d4e5a98ce44a7a999 to your computer and use it in GitHub Desktop.

Javascript/Typescript Style Guide

This style guide is inspired from the john papa style guide.

Small Functions

Pro :

  • Easy to :
    • Test
    • Read
    • Reuse
    • Maintain

Naming

Variables

Boolean

// IS
isLoading, isActive, isLogged, isSet, isVisible, isFinished, isFound, isOpen
// HAS
hasError, hasLicense, hasCar
// CAN
canEvaluate, canEdit, canRead

Number Of

numberOfUsers, numberOfCars, numberOfItems, numberOfMessages, numberOfTickets

Function

Computed

cars.computeTotalPrice();

Complement name

get/set, add/remove, create/destroy, start/stop, insert/delete, increment/decrement, old/new, begin/end, first/last, up/down, min/max, next/previous, old/new, open/close, show/hide, suspend/resume, etc.

Named Functions instead Anonymous Function

Pro Named Function :

  • Easy to :
    • Test
    • Reuse

Examples

// Avoid
myArray.filter((value) => {
   return item > 0 ? true : false;
});
// Recommanded
// + Test separately isPositive function
// + Reuse isPositive
// + Easier to read myFunc(isPositive);
const isPositive = (value) => {
   return item > 0 ? true : false;
};

myArray.filter(isPositive);

Sources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment