Samantha Ming LogoSamantha Ming

  • Home
  • About
  • Code Tidbits
  • Blog
  • Podcast
  • Recommendations
  • Contact

Bad Variable Names to Avoid πŸ™…β€β™€οΈ

In an earlier post, I mentioned how you can give your boolean variables a better name by prefixing it with is/has/can. Now I want to give advice on bad variable names you should avoid πŸ’₯

Reading code is tough already, so don’t make it even more complicated by using names that they have to guess. Your code should convey understanding. So it’s better to use words that everyone can understand. This will make it a lot easier for others and even yourself to work with your code. Yay! Win Win 😊

// Avoid Single Letter Names
let n = 'use name instead'

// Avoid Acronyms
let cra = 'no clue what this is'

// Avoid Abbreviations
let cat = 'cat or category??'

// Avoid Meaningless Names
let foo = 'what is foo??'

Community Feedback

  • @avi.codes: Using camelCase while naming variables and UPPERCASE while naming constants.

  • @jonashavers: That's always better than feeling the need to add a comment to describe what you mean and what the variable is used for. The goal is: "don't make me think"

Navigate your Code Base with Better Variable Names

Having a good descriptive name also make searching and finding a lot easier in a large code base πŸ’―

  • @agm1984: I subscribe to the idea that, just because you can make your names short doesn't mean you should. You might open a 200 line file to line 175, see this.toggle() and be like, toggle what? Nothing beats a good keyword search that finds only 2-5 matches in 2 files instead of like 29 matches in 18 files.
// Bad
const toggle = () => {}

// Good
const toggleCountrySelector = () => {}

Thanks: @agm1984

Resources

  • Be Expressive: How to Give Your Variables Better Names
  • The art of naming variables
  • The Importance Of Naming In Programming
  • More on JavaScript Variable Naming Conventions
  • JavaScript naming convention

Like ❀️

Like this on Twitter

Like this on Instagram

Bad Variable Names To Avoid

What I use

If you're interested in what I use to create this code tidbits, I list them here.

More Code Tidbits
  • Home
  • About
  • Code Tidbits
  • Blog
  • Podcast
  • Recommendations
  • Contact
  • What I use
  • Courses I Love
  • Podcasts I Love
  • Tech Stack
  • Speaking

Β© Copyright 2019. Samantha Ming