GithubHelp home page GithubHelp logo

art-of-naming-variables's Introduction

The art of naming variables (My variation)

Arrays

// bad
const fruit = ['apple', 'banana', 'cucumber']

// bad
const fruitArr = ['apple', 'banana', 'cucumber']

// bad
const fruits = ['apple', 'banana', 'cucumber']

// great - "names" implies strings
const fruitNames = ['apple', 'banana', 'cucumber']

// great
const fruits = [{
    name: 'apple',
    genus: 'malus'
}, {
    name: 'banana',
    genus: 'musa'
}, {
    name: 'cucumber',
    genus: 'cucumis'
}]

Booleans

Booleans can hold only 2 values, true or false. Given this, using prefixes like “is”, “has”, and “can” will help the reader infer the type of the variable. But it also specify context of the variable like a noun that the boolean variable is attached to.

// bad
const open = true
const write = true
const fruit = true

// bad
const isOpen = true
const canWrite = true
const hasFruit = true
const wasEgg = true
const eatAble = true
const haveCustomFilters = true
const hasCustomFilter = true
const areCustomFiltersApplied = true
const isCustomFilterApplied = true

// good
const itIsOpen = true
const itCanWrite = true
const itHasFruit = true
const itWasEgg = true
const itIsEatAble = true
const itHasCustomFilters = true
const itHasCustomFilter = true
const thereAreCustomFiltersApplied = true
const itIsCustomFilterApplied = true

it can be replaced for the context with concrete noun.

And then you can use with if statement, and it would look natural in English:

if (doorIsOpen) {
  // close it
}

Functions

What about predicate functions (functions that return booleans)?

const checkWhetherItHasFruit = (user, fruitName) => (
    user.fruits.includes(fruitName)
);
const itHasFruit = checkWhetherItHasFruit(user, 'apple')

Functions should be named using a verb if they return void, and a noun if they return something. When functions perform some type of action on a resource, its name should reflect that. A good format to follow is actionResource. For example, getUser.

// bad
getUser(db)
getUser(xml)

// good
const userRetrievedFromDB = userFromDB()
const userParsedFromXML = userFromXML()

art-of-naming-variables's People

Contributors

guseyn avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.