The Art of Naming Variables: How to Write Code That Speaks Human

Tue Oct 21 20252mo ago5 min
The Art of Naming Variables: How to Write Code That Speaks Human
I still remember the first time I opened an old project of mine and thought,
“Who on earth wrote this?”
Then I realized… it was me.
The culprit? My variable names.
tmp, data1, info, stuff — they all made sense when I wrote them,
but six months later they were complete mysteries.
If you’ve ever been there, welcome to the club.
Naming things is one of the hardest parts of programming.
But it’s also one of the most important skills you can develop — because names are how we communicate our ideas through code.
Let’s talk about how to get it right.
1. Why naming matters
Good names make code self-explanatory.
Bad names make you reach for the comments section just to understand what’s going on.
When you read:
let total = calculateCart(items);
you instantly know what’s happening. But when you read:
let x = func(data);
you need to decode it like an alien message.
Good variable names save time, reduce bugs, and make collaboration smoother. And honestly — they make your future self a little happier.
2. The goals of a good variable name
A good name should be:
- Clear → You should know what it represents and why it exists.
- Consistent → It should match the style of the rest of your codebase.
- Predictable → Someone new should be able to guess what it means.
Example:
// ❌ Bad let d = new Date(); // ✅ Good let currentDate = new Date();
3. Common naming conventions
Different languages have different styles, but here are the main ones you’ll see:
| Convention | Example | Common In |
|---|---|---|
camelCase |
userName, isActive |
JavaScript, Java |
snake_case |
user_name, is_active |
Python |
PascalCase |
UserProfile, AppHeader |
Classes, React Components |
kebab-case |
main-container, user-profile |
CSS, filenames |
CONSTANT_CASE |
MAX_RETRIES, API_URL |
Constants |
Knowing these patterns makes your code feel familiar to others.
# Python example user_name = "Alaa" MAX_RETRIES = 3
4. Avoid vague or misleading names
Names like data, info, or temp tell you nothing.
They’re the programming equivalent of saying “the thingy.”
// ❌ Bad let data = fetchUserData(); // ✅ Good let userProfile = fetchUserData();
Every variable name should answer a question: “What exactly does this hold?”
5. Add context — but not too much
You don’t want your names too short to understand,
but you also don’t want superDetailedUserAccountDisplayNameString.
Aim for the middle ground:
// ❌ Too vague let name = "Alaa"; // ❌ Too long let userAccountDisplayNameString = "Alaa"; // ✅ Just right let displayName = "Alaa";
Think of it like a good tweet — short, but full of meaning.
6. Follow naming patterns
Certain naming patterns help your code read naturally:
- Booleans: start with
is,has, orcan→isVisible,hasError,canSave - Collections: use plural nouns
→
users,messages,items - Functions: use verbs
→
getUser,sendEmail,calculateTotal
Example:
function isUserLoggedIn() { return Boolean(localStorage.getItem("token")); } let messages = fetchMessages();
7. Context is everything
Sometimes the same name can mean different things depending on where it lives.
// In a shopping cart module let total = calculateTotal(items); // In a payment processor let total = amount + tax;
Both are fine — because the context tells the story. Good code gives its variables meaning through their surroundings.
8. Be consistent
This one’s huge.
If you use getUserById() in one file and fetchUserById() in another,
your brain will quietly scream every time you switch between them.
Pick one pattern and stick to it. Consistency makes your codebase predictable — and predictable code is easier to trust.
9. Naming in different paradigms
🧩 Object-Oriented Programming
In OOP, variable names often reflect the real-world entities they represent:
user.name order.total product.price
🧠 Functional Programming
In functional code, names often describe transformations or data flow:
transformData() filterUsers() calculateAverage()
Each paradigm has its rhythm. Learn to speak its language.
10. When it’s okay to break the rules
Sometimes short names make perfect sense:
for (let i = 0; i < users.length; i++) { ... }
i, j, x, y — these are classic in loops, math, or coordinates.
The key is context clarity: if everyone knows what it means, it’s fine.
Just don’t name your database connection x — that’s not context, that’s chaos.
11. Tools and references
You don’t have to rely on memory for good naming habits — tools can help.
-
Linters like ESLint or Pylint can catch inconsistent naming patterns.
-
Style guides:
-
Book recommendation: Clean Code by Robert C. Martin — a classic for thinking about readability.
12. Final thoughts
At the end of the day, naming variables is a form of storytelling. You’re telling the next person — and your future self — what’s going on inside your code.
“The way you name your variables says a lot about how you think.”
So name with care. Name with empathy. Write code you’ll be proud to read six months from now.
Thanks for reading — if you enjoyed this post, share it with someone who still uses data123 in their codebase. We’ve all been there.