loading...

Use applesauce to code faster

newyorkanthonyng profile image Anthony Ng ・2 min read

You are coding. You start getting into a flow. Then you get derailed trying to think of a variable name.

Most variable names are simple enough. Is this variable keeping track of for-loop counters? Name it i (or j or k).

const array = ['Hello', 'World'];

for (let i = 0; i < array.length; i++) {
  for (let j = 0; j < array.length; j++) {
    for (let k = 0; k < array.length; k++) {
    }
  }
}

Is this variable an array that holds user objects? Name it userArray.

const usersArray = [
  { name: 'Alice' },
  { name: 'Bob' }
]

But now you have a variable that holds the first 10 vegan users that live in NorthEastern United States? What do you call this?

const ??? = [
  { name: 'Alice', dietaryRestrictions: 'vegan' },
  { name: 'Bob', dietaryRestrictions: 'vegan' },
  // ...
]

You scan through your code to see what naming conventions you used. If you're paranoid, you start thinking about all your future, unwritten code. What will all that code look like?

You've broken out of your flow. After 2 minutes of meditation, you found a variable name.

// rolls right off the tongue
const topVeganUsersInUnitedStates = [
  { name: 'Alice', dietaryRestrictions: 'vegan' },
  { name: 'Bob', dietaryRestrictions: 'vegan' },
  // ...
]

Great!

Now you're most likely married to the variable name. You spent so much time naming it in the first place, so why change it?

And this is even if you remember to change the variable name. You have an approaching deadline as you create your pull request. The last thing you'll want to do is look at your code, line-by-line, and update variable names.

This assumes that your variable even makes it to your pull request. You may have refactored your code during development and deleted the variable. What a waste!

function getTargetUsers() {
  // 💀 topVeganUsersInUnitedStates
  return [
    { name: 'Alice', dietaryRestrictions: 'vegan' },
    { name: 'Bob', dietaryRestrictions: 'vegan' },
    // ...
  ];
}

In general, you want to delay decisions until you have the most information possible. We should do the same with naming variables.


I attended a refactoring workshop. The instructor used the name applesauce when he ran into a difficult variable. Why?

Having a default variable name speeds up development. Imagine if you had to think up new variable names whenever you created for-loops. That would take up a lot of time. Similarly to i for for-loops, we want to use applesauce for ambiguous variable names. This speeds up development.

If applesauce is already taken, feel free to experiment with other sauces.

Also, it's so outlandish of a variable name that it stands out among the rest of your code. If the variable name survives all refactoring and makes it to the pull request, you will need to rename it.

During pull requests is the time when you have the most information to decide on the best variable name.


Do you have any variable naming tips & trips? Let me know about it.

Discussion

pic
Editor guide