{ The Rithm Blog. }

Good Ideas for Better Variable Names May 04, 2021

A large part of professional programming is communicating clearly with other developers: almost no serious software projects are written by a single author, and it's common for dozens of programmers to be able to understand your code.

​However, even if you're coding for yourself, it's still a good idea to pay attention to your strategies for naming variables: good variable names clearly help you understand how you plan to use a variable, and will make it much easier for you to come back and read your code after you've written it.

​Naming things well is a *skill*; like other things in programming, it may be difficult to do quickly at first, but over time, you'll find yourself using common patterns and ideas for names, and you'll find it faster to write code,and that you'll be able to read and understand your code more quickly.

 

## Plural / Singular

One of the easiest things to check is that a variable that holds a single value should **always** have a singular name, and that a variable that holds multiple things should **always** have a plural name:

let dogName = "Fido";
​
let dogNames = ["Fido", "Woofles", "Whiskey"];

Always double-check this: if your name suggests the wrong choice to the reader,it will be very confusing when they try to treat a single string as an array,and loop over it (or vice-versa).

## Purpose Over Type

One of the best ideas is to name variables after *what they're about* rather than *what type of thing they are*.

​For example, if you have an array of temperature readings, consider these two names:

```js
// not very helpful:
let arrayOfNums = [98.6, 212];
​
// better!
let temperatures = [98.6, 212];
​
// even better: are these Fahrenheit or Celsius?
let temperaturesF = [98.6, 212];
```
​
Even if it's not clear *exactly* what your variable will be used for, try to
find something more specific than `string` or `arr`:
​
```js
// not very helpful
let str;
​
// now we know it will be a word, rather than a sentence or name or such:
let word;
```

 

## Object Names

In JS, objects are used all the time for mapping a *key* (often called a *property*) to a value. For example, if you were building a Scrabble game, you could use an object to map a letter tile to the score that letter has in the game. Consider this code:

```js
const scores = {E: 1, S: 1, Q: 10, /* and more ... */};
```

That's *okay*, but if you just saw that variable name, you might think it was a list of scores for the different players, or other things. Good object names can often by made by combining the *key* idea and the *value* idea, like this:

```js
const lettersToScores = {E: 1, S: 1, Q: 10, /* and more ... */ };
```

Be consistent here in putting the key name before the value name --- a variable called `scoresToLetters` would suggest a very different object, one that mapped a score to all the different letters that have that score.

 

## What Exactly Is This?

Sometimes you have two (or more) slightly-different things to keep track of. Consider this code:

```js
let user = "jenna";
​
// somewhere else...
let user = "[email protected]";
​
// somewhere else...
let user = {name: "Jenna", email: "[email protected]", age: 39};
```

Any of those three different ideas could be a "user" and in different parts of your program, you might need their username, email, or lots of data about them. Try to find clearer names:

```js
let username = "jenna";
​
let userEmail = "[email protected]";
​
let userInfo = {name: "Jenna", email: "[email protected]", age: 39};
```

And, having chosen these names, try to use these same names, even across functions, to mean the same kind of thing: always call the user's email `userEmail` in some places, and call it `user` in others.

 

## How Short Should I Go?

Complex ideas often need longer names to explain them. It's usually better to make a clear name, even if its longer, than trying to find the shortest possible thing for a complex idea.

​In general:​

  • - Variables that are "global" (defined outside of any functions and used in different places in the code) should generally have longer and more explicit names. After all, these variables can be used *anywhere* in the program,so it can hard to figure out exactly what they're for.
  • ​- Variables that are defined inside of a function are only available (*in scope*)of that function. In general, functions should be relatively short and have a single purpose, so the variables declared in them often don't need to be as long or explicit, since where they're used and how they're used is often much easier to understand.

For example, consider this code:

```js
// a puzzling global variable: what, exactly didn't we find?
let found = false;
​
function searchForUser(searchTerm) {
 // probably a reasonable name: it's probably very clear what "found" means
 let found = false;
  // more code here...
}
```

 

## Global Constants

​One particular type of variable is a "global constant": a variable that is *constant* (doesn't ever change) and defined in the global scope (outside of any functions). Often these kind of variables are created to make code clearer:rather than hardcoding this value throughout the program, it's nicer to put them at the top of your program, and use that name throughout the code.

​For example, imagine code for a game played on a 10-by-5 grid gameboard.

Rather than code like this:

```js
// search for a piece horizontally
for (let x = 0; x < 10; x++) { /* code here */ }
```

This is clearer:

```js
const BOARD_WIDTH = 10;
​
for (let x = 0; x < BOARD_WIDTH; x++) { /* code here */ }
```

It's very helpful to communicate to people reading your code that this variable will never change while the program is running, so it's declared as `const` and given a name in `ALL_CAPS_WITH_UNDERSCORES`. Now readers of the code won't need to keep trying to figure out if the board changes width and can just mentally substitute 10 in for `BOARD_WIDTH`.

## Any Ideas?

​There are lots of things you might be thinking about when naming variables in your code, and experimenting and reading other people's code can give you additional strategies.

​If you have additional good ideas, I'd love to hear them! You can reach me at [email protected]

Written by Joel

Back to all posts