Aikido

Why you should use descriptive variable names to write self documenting code

Readability

Rule
Use descriptive variable names.
Very short variable names make code unclear.

Supported languages: 45+

Introduction

Single-letter or cryptic variable names force readers to deduce meaning from context instead of understanding code at a glance. A variable named d could represent date, duration, distance, or data, requiring mental effort to track its purpose throughout the function. Descriptive names like userCreatedDate or requestDuration make intent immediately clear without cognitive overhead.

Why it matters

Code maintainability: Unclear variable names slow down comprehension. Developers spend time figuring out what x, tmp, or val represent instead of focusing on the actual logic. Poor naming also makes code harder to extend because it’s unclear where new logic should fit or how existing values relate to each other. On top of that, vague names make the codebase unsearchable, since searching for tmp or val gives no meaningful results. This compounds when returning to code months later or when new team members join.

Bug introduction: Ambiguous names increase the chance of using the wrong variable. When multiple variables have similar cryptic names (d1, d2, d3), developers might use the wrong one, introducing subtle bugs that pass code review because the names provide no semantic clues.

Code examples

❌ Non-compliant:

function calcAmt(u, qty) {
    const p = u.prc;
    const d = u.disc || 0;
    const t = p * qty;
    const a = t - (t * d);
    const tx = a * 0.08;
    return a + tx;
}

Why it's innacurate: Variable names like u, p, d, t, and a reveal nothing about their purpose. Readers must trace through the calculation logic to understand that p is price, d is discount, t is subtotal, and a is amount before tax.

✅ Compliant:

function calculateOrderAmount(product, quantity) {
    const price = product.price;
    const discount = product.discount || 0;
    const subtotal = price * quantity;
    const amountAfterDiscount = subtotal - (subtotal * discount);
    const tax = amountAfterDiscount * 0.08;
    return amountAfterDiscount + tax;
}

Why this matters: Each variable name describes what it contains. amountAfterDiscount clearly indicates the calculation state, tax is unambiguous, and product and quantity reveal the function's inputs without needing to read the body.

Conclusion

Use names that reveal intent without requiring context. Favor clarity over brevity. The extra characters in totalPrice versus tp cost nothing in execution but save significant time in comprehension.

FAQs

Got Questions?

Are single-letter variable names ever acceptable?

Yes, in limited contexts. Loop counters (i, j, k) are conventional and clear. Lambda function parameters in functional programming (x => x * 2) are fine when the operation is obvious. Mathematical operations can use conventional names (x, y for coordinates). However, these should be limited to small scopes (few lines).

How long should variable names be?

Long enough to be clear, short enough to be practical. userData is better than u and better than dataAboutTheCurrentUserBeingProcessed. Aim for 2-4 words. If you need more, the variable might represent a concept that deserves its own type or class.

What about abbreviations like btn, msg, or ctx?

Common abbreviations in your domain are acceptable. ctx for context, req for request, and res for response are widely understood in JavaScript. However, avoid project-specific abbreviations that aren't obvious to new team members. When in doubt, spell it out.

Should I rename variables when refactoring old code?

Yes, but strategically. If touching a function for other reasons, improve variable names as part of the change. Don't create PRs solely for renaming unless the names are causing active confusion or bugs. Use IDE refactoring tools to ensure all references update correctly.

How do I handle conflicts with language keywords?

Add specificity: userClass instead of class, itemType instead of type. Or use context: userCategory, productKind. Never append numbers (class1, class2) as this loses meaning. The constraint often reveals that you need a more specific name anyway.

Get secure for free

Secure your code, cloud, and runtime in one central system.
Find and fix vulnerabilities fast automatically.

No credit card required | Scan results in 32secs.