What are your personal guidelines for good variable names?

This shares variable naming guidelines and tips like using descriptive names and following conventions to write readable and maintainable code.
On this page

What are your personal guidelines for good variable names?

Excerpt

Well-chosen variable names make your code easier to understand and modify. This article shares guidelines like using descriptive names, consistent conventions, avoiding keywords, and refactoring.


Variable names are one of the most important aspects of writing clean, readable code. The names we give to variables can have a huge impact on the maintainability and understandability of our code. In this post, I’ll share my personal guidelines and tips for assigning good variable names.

Introduction

Well-chosen variable names are crucial for writing code that is easy to comprehend and modify. Meaningful and consistent names make it simple to understand what a variable represents without having to look up its declaration. On the other hand, poor names like x, temp, or var1 force developers to decode what these variables are meant for every time they encounter them.

Good variable naming takes time but pays off tremendously in the long run. Studies have found that developers spend over half their time reading code as compared to writing it. This makes readable code an invaluable asset. Investing effort into naming variables thoughtfully can reduce the cognitive load while reading code and make the intent crystal clear.

Clear and Descriptive Names

My first rule of thumb is that variable names should be descriptive and unambiguous. I always try to use full, meaningful words that explain what the variable is used for.

For instance, userName is better than just name. totalPrice is preferable to price if it represents the total price rather than a unit price. I avoid abbreviations and acronyms since these can be confusing to someone unfamiliar with the codebase. Longer names are fine as long as they aid comprehension.

I also aim for names that specifically describe the variable’s purpose rather than generic names like data or info that could refer to anything. The more precise and focused the name, the better.

Consistent Naming Conventions

Naming conventions like camelCase for variables and functions or UPPER_SNAKE_CASE for constants help establish consistency across the codebase. I try to stick to the conventions already used in a project rather than introducing new ones that could be perplexing.

Online tools like String to Snake Case and String to CamelCase can quickly convert between formats to maintain uniformity.

Avoid Overly Long Names

However, I avoid excessively long or wordy names that feel unnatural. For instance, calculateTotalUserPurchasesValue may be technically accurate but calculateTotalPurchases is easier to grasp at a glance while still conveying meaning.

I try to find the right balance between brevity and description. Common words like user, data, info can often be omitted without losing clarity.

Stay Away from Keywords and Special Characters

Variable names should avoid collision with language keywords, built-in classes, and function names. For instance, string or hash would be bad choices in Ruby.

I also avoid special characters like dashes, dots, or spaces within names. Underscores can separate words but should be used sparingly. Sticking to alphanumeric characters makes variable names least prone to causing issues.

Avoid Misleading or Ambiguous Names

Names that are vague or could imply multiple meanings should also be avoided. For example, data could refer to raw input data or processed output data. line could mean a geometric line or line of text.

I ensure names clearly reflect the specific purpose or content of the variable. Disambiguous names remove assumptions and prevent confusion down the line.

Use Camel Case and Underscores Consistently

In languages like JavaScript, I tend to use camelCase for variable and function names consistently. For example, getUserPosts reads better than get_user_posts.

However, in Python, I follow the PEP8 style guide and use snake_case for variables and functions. get_user_posts would be the standard convention.

Whatever naming style is chosen, I try to use it consistently across the codebase for consistency and readability. The String to CamelCase and String to Snake Case tools help quickly convert between the two formats.

Regularly Review and Refactor Names

I reevaluate the quality of variable names whenever I am reading through a codebase and find myself confused. If a name requires deciphering, it is a red flag for refactoring.

As requirements change, some names may become misleading or redundant. Renaming variables to better reveal intent helps reduce cognitive strain.

Tools like String to Upper Snake Case automate renaming variables in bulk according to a standard convention. I leverage such tools and refactoring techniques to continuously improve variable names.

Conclusion

Variable naming is a foundational skill that directly impacts code comprehension and quality. With meaningful, unambiguous, and consistent naming, developers can write self-documenting code that clearly conveys intent.

I encourage all programmers to apply guidelines like the ones I outlined here. Invest time thinking about the best names during initial development and refactor mercilessly later on. Coding with carefully chosen variable names will make your programs far more readable and maintainable.