045 - Improving Naming Conventions

5 Dec, 2017

The more you write code, the more you realize the importance of choosing good names for your functions and variables. In this episode I discuss a few best practices I’ve picked up so far.

As a newbie to writing code, it’s just about getting the darn code to work. Lots of questions take priority: What is a function? What is this var I keep seeing everywhere? Did I miss a semicolon AGAIN!? Thinking about how to name functions, variables, or elements takes a back seat.

But the more you read code, and especially the more you read your own code from the past, it’s easy to see just how important it is to name things well. It’s probably the most important thing you can do to make your code readable.

Here are some tips I’ve picked up so far about naming conventions:

  1. Be specific with names so that the name describes what is actually happening. Example: name a function calculateAge() instead of just calculate(), which is too vague.

  2. Don’t use similar names. My terrible bad example was when I called a button “item-reward” and on the same page called a form “reward-item.” This is the epitome of confusing when you go back and read your code after a few weeks away.

  3. Don’t use initials, single letters, or numbers as names. It’s harder to understand and nearly impossible to search. Brevity does not always win over clarity!
    Basically when reading through your code, the reader should be told a story: a sequence of actions that makes sense to human understanding.

To improve your own style of naming things I can recommend:

  1. Read other people’s code, both good and bad. You’ll get best practices from the good, and can see what not to do from the bad!

  2. Read books about best practices. I can recommend Clean Code by Robert C Martin.

  3. Ask other developers to review your code and offer suggestions for improvement.

  4. Do the time test: read your own code a few weeks or months after you’ve put it down. After some time away you’ll see rather quickly where improvements could be made, as well as how clear or confusing it is.

All of these are just a start. If you have some tips to add, you can leave a comment below or tweet me @startovercoder.