Variable Names, Some tips
Muhammad Asher Toqeer
Senior Java Developer | Backend | Spring | thebackendguy.com
Variables can be your best friend, or your worst enemy and that depends on You. If you are not careful and just creating variables without putting enough thought into it, your program soon would be a nightmare. In small programs, int x, y; is OK but unfortunately real life programs are not "small". So, you need to be careful while declaring variables.
In this article we would see how to meaningfully name our variables. I would focus "how to meaningfully name variables" rather than "rules or conventions for declaring variables". Rules or conventions might be vary in languages but meaningful names should be used regardless of language. Here are some points you should think before name you variable.
Avoid Action Words
Avoid words (words which shows action) should never be use as variable names. A variable is a data container, not an actor. Variables either contains data to perform an action or data which is result of an action. For example:
int add; // it makes no sense.
int sum; // ?thats a good name.
int additionResult; // a good elaburative name??
Avoid 'Temp'
Keep in mind there is nothing called "temporary variables", a variable named "temp" shows carelessly implemented logic. A variable, once declared, will always be there, so how is it temporary? Or on the other hand if some one want to say that the data in that variable is temporary then that is the main function of a variable, to hold temporary data, so what's new then?
Its simple you name it 'temp' because you don't know what name it should be given. Remember If there is a purpose, there is a name, much batter then 'temp'.
Determine Category
On highest level a variable represent a single data unit or collection of data unit. I am talking about single variables and arrays. Put an extra 's' in the end of a variable representing a collection, for example:
int number; // a single number
int[] numbers; // a list of numbers?
Note that The collection doesn't necessarily mean array, it could be a stack, a set. a queue or any other relevant data structure.
Scope Matters
A variable representing a field in a class should have much more meaningful name than a variable in a small little private utility function. A field in a class is most probably going to have getters and setters and would be referenced in future while a little variable in a small function is not that much important.
Similarly a variable representing a parameter of a function should be much more meaningful than a variable declared inside of that function. A parameter name is what seen by callers of that method and they might not be interested in what is inside. Keep that in mind while writing your methods.
Conclusion
Variables are very important and should be declared carefully. Variable names should not be action words and should have a meaningful name rather than temp. Keep in mind number of object it represents and scope should be taken care of while naming variables.
That was our brief discussion about variables names, You may also want to check out Function Names, do it right and Meaningful class names. If you have any query or suggestion please mention it in comment section, I would love to hear from you.
Thanks for reading, Happy coding !
Senior Software Engineer bij Kverneland Group Mechatronics BV
8 年Having a good naming convention is essential for extensibility of software.
PSE @ NXB | PHP | JS | React | Laravel
8 年hahaha