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:
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.
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.
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:
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!
Read books about best practices. I can recommend Clean Code by Robert C Martin.
Ask other developers to review your code and offer suggestions for improvement.
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.