DEV Community

Cover image for Some variable naming tips and tricks to make your code better 👍
Sayantan Nandi
Sayantan Nandi

Posted on

Some variable naming tips and tricks to make your code better 👍

Hello 👋 guys, I am going to show you all some good variable naming traditions and tricks. If you struggle in writing good and clean code, this article might be able to ascend you in your journey. So lets not waste any time and jump right into the tip and tricks.

1. Use Short Names

Which of these variable names sound more meaningful to you : dataOfUsers or usersData? If you choosed the second option, then you are right. Short names are better because they are more readable and make more sense to the readers. Short names are not always meaningful. But you should first try short names for your variables. Now, let us see next what to do when we can't use short names.

2. Use Descriptive Names

We saw that short names are good practices of variable naming. Let us consider an example : Let us say that we are making a website and we are letting the user to get the current time. So we have to store the current time in some variable. What would you name this variable? Is it d or date or cd? If your answer is any one of these options, then you probably need to read this article. I would name this variable currentDateStamp. Why? This purely describes what the variable contains. There is a saying among great programmers that is "If you use comments to describe what your variable contains, then you are probably a rookie". Does this makes sense to you? It makes absolute sense to me. So you now know that variable names should describe what they contain. Let us move onto the next point.

3. Use Pronounceable Words

Do any of your variable name sounds like "getymwdh"(gets Year, Month, Week, Day, Hour)? If your answer is yes, you should continue reading this. Is "getymwdh" really easy to pronounce? I don't think so. Programming is a social skill. You have to deal with your project manager, junior developers, senior developers, etc. Suppose there is a bug in "getymwdh" and your project manager asks where is the bug in the code, how will you answer. You can't even pronounce the variable name. So it is highly advisable to use pronounceable names like "getTimePeriod".

4. Use Technical Terms

If the majority of people who read your code are programmers, then bam! This point is for you. Programmers understand a lot of terms which sound like fantasy names to the rest. So, you can use this as your weapon for writing good variable names. You can include names of algorithms, CS terms, math terms, pattern names and so forth as your variable name. This will also satisfy our 2nd and 3rd point which we talked about.

5. Use Searchable Names

Imagine that you have written a program which contains of 1 million lines and that you have to find the variable "n" which contains a random number. Just think how difficult it would for you to find that variable. It is nearly impossible. Let us make this problem a little simpler. Let us use all the points which we have learned till now. Let us try to make this variable short, describe, pronounceable and searchable also. How about "RANDOM_NUM"? This nearly satisfy all the points. Now, when we try to search for "RANDOM_NUM" we will pretty easily get the variable even inside 1 million lines of code. So it seems that the variable naming points are making our job easy.

Final Thoughts

So, if you feel overwhelmed with all this information, don't be because variable naming is not an important topic in programming but it truly helps us alot. I hope that you have got an idea about how to name your variables. If you wanna know from where I have this knowledge, then the source is Clean Code by Robert Cecil Martin. I have expressed some of the key concepts of variable naming from this book in my words. If you want this book, you can download it for free from this link : I hope that you have enjoyed and learned something from this article. If so, please follow me on and wait for my next post. Thank You for reading this.

Top comments (7)

pmgysel profile image
Philipp Gysel

Great Article!😊🙌 Thanks!
I think the tricky part is to make names both short but descriptive and searcheable. All is covered here👍🏼

maulik profile image

I like the point Use Pronounceable Words

dendihandian profile image
Dendi Handian

When I found someone else codes and it has obvious bad variables naming, I change them immediately without asking, BUT of course, you have to be aware of the scopes and blocks it uses.

meshamakes profile image

This is a really helpful article to those starting off, It was written so that anyone can understand. Keep it up man I look forward to more articles like this :)

itzprogrammer profile image
Sayantan Nandi


muhyilmaz profile image

Good article, the point is not just write the code also write readable.

juwdohr profile image

Link is no longer good.