Why Naming Conventions Matter More Than You Think
Ask experienced developers what makes code hard to work with, and many will mention names before anything else. A function called process or a variable called data2 forces every reader to stop and decode what the author meant. Multiply that by thousands of lines, and the cost becomes enormous.
Naming conventions are the agreed rules for how things are named in a project. They sound like a minor matter of taste, yet they affect readability, searching, collaboration, and even the number of bugs. This guide explains what conventions cover and how to apply them sensibly.
Code Is Read More Than It Is Written
You write a line of code once. You and your teammates will read it many times: when fixing bugs, adding features, reviewing changes, and trying to remember why something exists. Good naming reduces the effort of every one of those readings.
Names are the main way code explains itself. A well-chosen name removes the need for a comment. Compare these two lines.
d = 86400
seconds_per_day = 86400
The second tells you what the number means without any extra effort. Over a large codebase, thousands of small clarifications like this add up to a project that feels easy to work in.
Two Layers: Style and Meaning
Naming conventions work on two levels, and both matter.
Style is about the shape of the name: capitalization, separators, and prefixes. Meaning is about whether the name describes the thing accurately. A project can follow a perfect style and still have confusing names, or the reverse. Aim for both.
Common Style Choices
Several case styles appear across languages.
- snake_case: words separated by underscores, such as
user_name. Common in Python and Ruby for variables and functions. - camelCase: the first word lowercase and later words capitalized, such as
userName. Common in JavaScript and Java for variables and methods. - PascalCase: every word capitalized, such as
UserName. Often used for class and type names. - UPPER_SNAKE_CASE: all capitals with underscores, such as
MAX_RETRIES. Frequently used for constants. - kebab-case: words separated by hyphens, such as
user-name. Common in file names, web addresses, and CSS class names.
Which one you use matters less than using it consistently. Each language community has its own defaults, and following them means your code looks familiar to anyone else who works in that language.
What Makes a Name Good
Style is the easy part. Choosing meaningful names takes judgment, but a few principles help.
- Say what it is, not how it is stored.
customersis better thancustomer_list_array. - Use verbs for actions and nouns for things. Functions do something, so
calculate_totalreads better thantotal. Variables hold something, sototalis a good variable name. - Be specific.
data,info,temp, andthingrarely say enough.invoice_totalorpending_orderstells the reader more. - Match the length to the scope. A loop counter used in three lines can be
i. A value used across a whole module needs a fuller name. - Avoid unclear abbreviations.
cust_addr_ln2saves a few characters and costs readers a few seconds each time. Common abbreviations such asidorurlare fine. - Use the project's vocabulary. If the business calls them "orders," do not call them "purchases" in one file and "transactions" in another.
For true or false values, a name that reads like a question works well: is_active, has_permission, can_edit. Avoid negatives such as is_not_valid, which become confusing when negated again.
Consistency Beats Perfection
Imagine a codebase where some functions are named getUser, others fetchOrder, and others retrieveInvoice, all doing the same kind of job. Readers wonder whether the different verbs signal a different behavior. Often they do not, and the variation only creates doubt.
Pick one word for each concept and use it everywhere. This also makes searching reliable. If every database lookup begins with get_, a search for that prefix finds them all. Predictable names let you guess correctly, so you spend less time hunting through files.
Where Conventions Come From
You rarely have to invent rules from scratch.
- The language's own style guide. Many languages publish recommended conventions, and the standard library follows them.
- The framework in use. Frameworks often expect certain names for files and functions, sometimes enforced by the tool itself.
- The existing codebase. When joining a project, match what is already there, even if you would choose differently. Consistency inside a project matters more than your personal preference.
- A team agreement. For anything not covered above, write down a short list of rules in the project documentation.
Automated tools help here. Linters and formatters can flag names that break the rules, so reviewers can spend their attention on the logic instead of style.
Renaming Safely
When you find a poor name, fix it. Modern editors can rename a symbol across a whole project at once, updating every reference. Make renames in their own small change so reviewers can see that behavior did not change. Run the tests afterward.
Do not rename public interfaces casually. If other people or other systems depend on a name, changing it can break them. In that case, introduce the new name, keep the old one for a while, and remove it once everyone has moved.
Frequently Asked Questions
Should I follow the language convention or my own preference?
Follow the language and project conventions. Code that looks like everyone else's is easier for others to read, and tools expect it. Personal preference matters far less than consistency.
Are long names bad?
Not by themselves. A long, clear name is better than a short, cryptic one. But overly long names can make lines hard to read, so aim for the shortest name that is still unambiguous in its scope.
How do I enforce naming rules on a team?
Document the rules briefly and add a linter or formatter that checks them automatically. Automated checks keep the discussion out of code reviews and apply the rules evenly to everyone.