DEV Community

Favor Charles Owuor
Favor Charles Owuor

Posted on

Proper Variable Naming

variable naming

When I started programming, naming variables was the last thing on my mind. I would use the variables given in YouTube tutorials, that was enough for me.

I would use names like x, temp, data, num, etc. The code ran, so I didn't see the problem.

I would come back to the same project later and spend several minutes trying to remember what those variables actually stored.

That's when I realized variable names are not for the computer. They're for the people reading the code.

Why Variable Naming Matters

A variable is a named storage location in a computer's memory that holds data or values which can change during program execution. To a computer there is no difference when you name a variable x or total_price. To the compiler, both are just labels pointing to data.

The opposite is true for Humans. Imagine opening a project you worked on six months ago and finding something like this:

if balance > amount {
    withdraw = true
}

Enter fullscreen mode Exit fullscreen mode

What are x, y, z, and a? Without context, it's difficult to tell.

Now compare that to:

if account_balance > withdrawal_amount {
    is_withdrawable = true
}

Enter fullscreen mode Exit fullscreen mode

The second example tells a story. You immediately understand what the code is trying to do.

Good variable names improve readability, reduces confusion, and makes maintenance much easier. They also help when debugging.

When an error appears, it's much easier to trace a problem through variables named user_id, total_price, or transaction_date than through variables named id, price, and date.

This becomes even more important when working with other developers. A clear variable name can save several minutes of explanation every time someone reads your code.

Common Naming Mistakes

One mistake many beginners make is choosing names that are too short.

For example:

tp := 1500

Enter fullscreen mode Exit fullscreen mode

What does tp mean?

It could mean total price, transaction processed, temporary product, or something else entirely.

A more descriptive name removes the guesswork:

total_price := 1500

Enter fullscreen mode Exit fullscreen mode

Another common mistake is leaving out important context.
Imagine you have a timeout value:

timeout := 30

Enter fullscreen mode Exit fullscreen mode

Thirty what? Seconds? Minutes? Hours?
Adding the unit makes the meaning clear:

timeout_seconds := 30

Enter fullscreen mode Exit fullscreen mode

Small details like this prevent mistakes that can be surprisingly difficult to track down.

Naming Boolean Variables

Boolean values deserve special attention.

Since they can only be true or false, it's usually best to name them like questions.

For example:

is_logged_in := true
has_permission := false

Enter fullscreen mode Exit fullscreen mode

When these variables are used in conditions, the code becomes easy to read:

if is_logged_in {
    // continue
}

Enter fullscreen mode Exit fullscreen mode

The statement almost reads like plain English.
Compare that to:

if status {
    // continue
}

Enter fullscreen mode Exit fullscreen mode

What does status mean? You have to inspect more code before you understand it.

Consistency Matters

One thing that's often overlooked is consistency.

Most programming languages and teams have naming conventions.

Some use camelCase:

userAge
totalPrice

Enter fullscreen mode Exit fullscreen mode

Others prefer snake_case:

user_age
total_price

Enter fullscreen mode Exit fullscreen mode

The choice matters less than being consistent.
Mixing different styles throughout a project makes the code look messy and harder to follow.

A Simple Rule

When naming a variable, ask yourself: "If someone else reads this six months from now, will they immediately understand what it contains?"

If the answer is no, choose a better name. You don't need extremely long variable names. You simply need names that clearly communicate the purpose.

Conclusion

Proper variable naming seems like a small detail when you're focused on building features. The longer I spent working on projects, the more I realized that poorly named variables create confusion, slow down debugging, and make maintenance harder than it needs to be.

Good names make your code easier to read, easier to understand, and easier to work with. The next time you're creating a variable, spend a few extra seconds choosing a meaningful name.

Top comments (0)