Variable Naming Conventions
What you'll learn: How to choose clear, meaningful, and consistent names for your variables that make your code readable and maintainable.
Why Variable Names Matter
Now that you know how to declare and initialize variables, you need to learn how to name them well. Think of variable names like labels on kitchen containers — a jar labeled "sugar" is far more helpful than one labeled "s" or "white_powder_1".
Core Guidelines
Be Descriptive and Clear
Choose names that explain what the variable stores. age is better than a. studentCount is better than num or x.
Use Consistent Style
Pick one naming style and stick with it:
- camelCase:
firstName,totalScore(common in many languages) - snake_case:
first_name,total_score(common in Python) - PascalCase: usually reserved for type names, not variables
Follow Length Guidelines
- Too short (
x,tmp) hides meaning - Too long (
theCurrentUserFirstNameString) becomes tedious - Aim for balance:
userFirstName
Avoid Misleading Names
Don't call a variable count if it stores a person's name. The name should match the data type and purpose.
Use Pronounceable Names
genYmdhms (generate year-month-day-hour-minute-second) is harder to discuss than generationTimestamp.
Avoid Reserved Keywords
You can't name a variable int, boolean, true, or other language keywords.
Example Comparison
- ❌
int x = 25; - ✅
int userAge = 25;
Key Takeaway: Good variable names act as documentation — they make your code self-explanatory and easier for you and others to understand, debug, and maintain.