In this series, we share quick tips for developers to make their code more accurate, efficient and scalable.
Today’s Developer Tip Of The Day: Variable Names
What do you mean “meaningful name”… Variables can be named anything, can’t they?
You are right, they can be named anything. Most of the times when we learn programming, through tutorials or in class, the focus is usually teaching the concepts of the language. As a result, we often don’t pay attention to the names of the variable since:
- “After all, it’s just me” Syndrome: Its only you doing the programming and there are no inerdependencies with other developers/ testers
- “It’s over” Syndrome: Most cases, once you learn a concept, that code you used to learn is thrown away or relegated to the dark dusty corners of your hard drive 🙂 It is not really, actively in use by you or others
Why is this tip useful?
Once you start writing code for any non-trivial application, having a variable name which at a glance will tell you the purpose for which you created it, it will immensely help you as well as any other progammers both maintain or extend the code easily. It will also help you easily spot bugs which may have been missed otherwise.
For most languages, your code must read like pseudo code, you should be able to give the program to someone who does not know the syntax and yet be able to understand the purpose of the code.
I don’t follow this tip. What’s the problem?
If you dont use this tip, well… you will possibly spend a lot more time wondering what exactly were you thinking when you created this variable or that. As your codebase expands or you start working on the same codebase over a long period of time, say a few months, as would happen in your careers, the problem only gets worse. The recency effect wears off and you forget. Very much like naming files and folders on your hard drive.
It could also lead to case where you spent hours and hours debugging code, only to find out, you were supposed to use a variable name ‘xx’ instead of ‘x’.
Are there any exceptions?
Nope. Never. Nada. With pretty much any modern language, you have the ability to have as lengthy variable names as you want. In the choice between those 3 extra seconds thinking and typing out a meaningful variable name and 3 hours of recollecting and debugging, we choose 3 seconds anyday 🙂
From the Team @ WukiLabs