Take a look at this little piece of JavaScript:
const d = 86400000;
const t = Date.now() + d;
It works, but what exactly is d? And what does t represent?
You could spend a few seconds figuring it out, or you could write it like this:
const oneDayInMs = 86400000;
const expiresAt = Date.now() + oneDayInMs;
Same code, same result, but much easier to read.
Another common example is something like this:
const data = users.filter(user => user.active);
There's nothing technically wrong with calling it data. But once the file gets bigger, that name isn't particularly helpful.
const activeUsers = users.filter(user => user.active);
A small change, but a useful one.
That doesn't mean every variable needs a ridiculously long name. Using i in a simple loop is perfectly fine. Context matters.
It's also fine to start with a bad variable name when you're trying to get something working. You can always rename it later.
There's no need to spend ten minutes thinking of the perfect name for every variable.
Sometimes making code better is just changing a couple of words.
Top comments (0)