Why are good names so important?
Every name you write in your code is only for humans. The compiler, interpreter or whatever program runs your code, will throw your names away and use internal identifiers. So the purpose of your names literally is for you and other developers to understand the code. Furthermore, code is far more often read than written.
Indeed, the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code.Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship
Consequently, writing understandable code by using meaningful names is much more rewarding than just doing it “quick and dirty”. It may take a lot longer than doing it quickly, but in the end you and your colleagues will save more than it will cost.
Giving bad names is quick
Even if it doesn’t seem like it at first glance, sometimes it can be really difficult to give meaningful names. It is often very challenging, especially to make it understandable for outsiders.
Everybody knows the situation, where you have a topic or a goal, and just start coding right away. You are incredibly productive in a short time and the outcome is great. But most of the code we write in this attitude is anything but clean and readable. Consider this small example where the names don’t say anything about what the code does.
It is very hard to understand what the code actually does. The writer, with the code’s thoughts and concepts in mind, will understand the code because he wrote it. But after a while, when he hasn’t dealt with the code, he wouldn’t understand it either. When the code needs to be adjusted because the requirements changed or there is a bug in it, there’s a slight problem. Getting familiar with poorly written code can be very frustrating and may take a long time. To fully understand the code, the developer possibly needs to debug it line by line, which is really time consuming and exhausting.
How to give meaningful names
So, how can you come up with meaningful names? There are a few tricks that will help you to improve the names in your code!
- Don’t be afraid to use long names
Even if it seems confusing at the fist moment, long names are mostly better than short ones. Especially when the shorter name would hide some relevant information, the better choice is always better to include it in the name. The code will grow, but most information about purpose of the code is in the names. By the way, explaining what the code does with lots of comments isn’t a good idea, as you can see in our post to Comments – What you should keep in mind.
- Keep your functions doing only one thing
By applying this principle, you keep your functions small and reusable. Every function has only one purpose, and therefore it is easy to give them meaningful names. Additionally, you will have more functions and more opportunities to give names. As a result, your code contains more names and tells what it’s doing.
- Let others review your code
Only if someone else understands your code you should be satisfied with the code and your names. There are lots of other advantages of code reviews, so it’s recommended to do it anyway. Nonetheless, after your code passed the review, it’s not “finished”. Writing code is an iterative process that never ends, so you should keep improving it!
With these tricks and some creativity, we can refactor the previous code.
After this revision, the code grew a lot but it looks much more inviting. Other developers looking at this code will more likely be motivated to read, understand and extend it.
There is nothing that excites me more than solving problems and understanding complex concepts. I want to share this enthusiasm by making complex things as simple as possible, so everyone can understand them. Nothing is more frustrating than trying to understand a topic with incomprehensible explanations. That’s why I want to inspire you to become a better developer by simplifying concepts and explaining issues clearly.
Book Recommendations on Clean Code (Affiliate)