Before you unlock this “secret” you must be aware of a single, most important thing.
And that is: code is mostly being read!
Your code will be written only a fraction of the time. Most of the time, it will be read over and over again.
And mostly by other people as well.
What’s worse – you yourself will almost be like a different person in 2-3 months. Because you will lose nearly all context of why and how you did something.
And all that will be left is the code that you wrote.
So if you were either a bit lazy and your variable name isn’t described very well, or you tried being too funny with your function naming – now you have a problem.
Now you wonder how and why that was funny in the first place. Desperately trying to remember the joke.
Maybe you remember, maybe you don’t. Either way, you are wasting time and energy unnecessarily.
The First Rule Of Variable Naming…
is to name things precisely.
Let’s say you are making an app that is basically a news aggregator. In that case, you wouldn’t name your function “getPosts()”.
Why?
Because you’re fetching your posts from multiple locations and you have different APIs or implementations for each of the locations:
- getPostsFromTwitter()
- getPostsFromMedium()
Ok but what about the main function that calls all these other functions?
Something like this:
getPosts() {
getPostsFromTwitter();
getPostsFromMedium();
};
Surely, it can be named “getPosts()”, right?
No.
Technically it can be called “x()” but remember this code is going to be read over and over again.
And it’s not like the “getPosts()” is wrong. If it was, then we wouldn’t be having this conversation.
It would be just obvious or the code wouldn’t run.
But naming is that gray zone where a bunch of bad things can go wrong. And without even realizing it – the entire project is slowed down.
The point is to make it as easy as possible for the next person to read (including you sometime later).
Extra 5 seconds you’ll invest in better naming will save a whole minute for someone else down the line.
This is your first step in becoming the “mythical” 10X developer.
But, how would I name it?
It’s easy to say what is wrong. But it’s much more difficult to make it better. Personally, I would go with this:
getPostsFromAllNewsSources()
This makes it clear that multiple APIs are going to be called.
Don’t Be Afraid Of Long Variable Names
This brings me to the common objection I get – that this variable name is too long.
And I understand why you’d think that.I also went to school and in almost every example, there was a variable named “x”.
While this is ok for school assignments where you have zero external context and the entire solution has maybe 50 lines of code…
Try doing that on a project with 100.000+ lines of code and 10+ people working on it.
I suspect that would not be a fun experience to go through.
Remember: you’re not in Kansas school anymore 🙂
Singular vs. Plural
Also remember, if you’re coding in a dynamically typed language, to return proper values back.
Let your function name (correctly) indicate the type of return value.
If properly named, I can always tell what the type of the return value is:
- getPosts() – list
- getPost() – object
- getPostTitle() – string
- getPostLikesCount() – number
- getIsPostDeleted() – boolean
Conclusion
Follow these simple rules and you’ll make your life much easier. Not to mention the lives of your colleagues.
An additional benefit is they will likely recognize you as a much better/experienced developer.
So it really pays off to invest 5 extra seconds into better naming.
P.S. -> read this article if you want to double your salary