Master Variable Naming: Clean Code Practices for JavaScript
This article explains essential clean‑code techniques for naming variables in JavaScript/TypeScript, covering self‑descriptive, pronounceable identifiers, avoiding type prefixes, using consistent terminology, eliminating unnecessary context, and replacing magic numbers and strings with meaningful constants.
Introduction
This article is part of a "Clean Code" series that explores how to write tidy JavaScript/TypeScript code, focusing specifically on the most fundamental element – variables.
Variable names should be self‑explanatory
A variable’s name must convey its purpose; generic names like x or y are acceptable only in mathematical contexts. In most code, such names obscure intent and make the code hard to understand. Instead of adding comments to vague names, rename them to something meaningful, using IDE refactoring tools if needed.
const x; // What it is?!
const x; // User information
const user;Variable names should be pronounceable
When a name has meaning, it should be readable aloud. Avoid clever acronyms or abbreviations that are difficult to pronounce, especially in a multilingual team. Clear, pronounceable names improve readability and reduce mental effort.
class DtaRcrd102 {
private Date genymdhms;
private Date modymdhms;
}
class Customer {
private Date generationTimestamp;
private Date modificationTimestamp;
}Don’t use type prefixes in names
Prefixing variable names with their data type (e.g., aCountries, sName, dAmount) adds noise and ties the name to a specific implementation. When the type changes, the name must be updated, creating unnecessary churn.
const aCountries = [];
const sName = '';
const dAmount = 3.2;
const countries = [];
const name = '';
const amount = 3.2;Use consistent vocabulary for the same type
Apply the same term for a given concept across the codebase. Do not alternate between user , customer , and client for the same entity, and avoid adding extra suffixes.
getUserInfo();
getClientData();
getCustomerRecord();
getUser();Avoid unnecessary context in names
Including class or package context in a variable name (e.g., carMake, carModel) is redundant; the surrounding code already provides that context. Removing such noise yields clearer code.
const Car = {
make: 'Honda',
model: 'Accord',
color: 'Blue',
};
function paint(car) {
car.color = 'Red';
}Don’t use magic numbers and strings
Hard‑coded literals like 86400000 or the string Administrator should be replaced with well‑named constants that explain their purpose, reducing the risk of errors and improving maintainability.
// What the heck is 86400000 for?
setTimeout(blastOff, 86400000);
user.rol = 'Administrator';
const MILLISECONDS_IN_A_DAY = 86400000;
const ADMINISTRATOR_ROLE = 'Administrator';
setTimeout(blastOff, MILLISECONDS_IN_A_DAY);
user.rol = ADMINISTRATOR_ROLE;Conclusion
When you start as a developer, variable names may seem trivial, but over a long‑term project they become crucial for maintainable, high‑quality code. Remember the key points: self‑descriptive names, pronounceability, no type prefixes, consistent terminology, no unnecessary context, and no magic literals.
Variable names should be self‑explanatory
Variable names should be pronounceable
Avoid type prefixes
Use consistent vocabulary
Don’t add unnecessary context
Avoid magic numbers and strings
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
