Small Things Matter: Naming
Naming is hard. But we can improve and make a difference.
As developers, we write code for a living. Most of the time, we pay attention to solving problems, learning new technologies; kind of new cool stuff. We tend to ignore or look down on small things.
There are so many small things. Well, in reality, everything is small things. Big is just a sum of small things. Naming is one of them.
Dear developers, what is on your mind when you heard about “Naming”?
- Project name
- Class name
- Method name
- Interface name
- Property name
- Variable name
- Hardcoded value
They are all matter. Why? Because they help us__human being__can read the code. They help us understand the logic by just looking at the signatures.
In the technical jargon, we call readability.
Let’s take a look at “Hardcoded value” and “Variable name”. They are pretty much the same thing. I have seen many places that developers name a variable: b1, b2, … They have the same problem:
They give no meaning at all
Number Cannot Speak
Take a look at this code. I am sure all C# developer knows
public class BadNaming { public void CanYouUnderstandTheLogic() { Thread.Sleep(180000); } }
The code says that: Should sleep (block and wait the current thread) for 180000 milliseconds.
What are the issues with that code?
- As a reader, I have to convert 180000 to a meaningful concept that my brain can process, Ex: Second, Minute, Hour. As a human, we do not usually deal with milliseconds.
- What is the logic behind the sleep? Usually, developers will write inline comments. (Hint: You should do a quick search and will know that inline comment is not a good approach)
The number itself cannot carry meaning. It cannot speak what it means.
Give It a Meaning
Now take a look at this code. What do you think?
public void GiveMeMeaning() { var sleepThreeMinutes = TimeSpan.FromMinutes(3); Thread.Sleep(sleepThreeMinutes); }
The Thread.Sleep method can take a TimeSpan. Usually, developers forget that option.
With a little change, I can assign meaning to a number. The code can self-explain its meaning.
No matter what languages you are using, there are primitive types that convey, naturally, no meaning. When you use them, think about the other person (maybe you in a year) will read that code. They have to spend time investigate the meaning.
I just pick a small corner of the problem with the hope that you get the idea and pay more attention with your code. Someone will thank you and call you a professional.
Professionalism is nothing than caring for small stuff.