Variable Naming & Conventions
Good naming is one of the most important aspects of readable code. Variables, functions, and classes should have names that clearly communicate their purpose without requiring additional comments.
Naming Conventions
+---------------------------------------------------------+
| COMMON NAMING CONVENTIONS |
+---------------------------------------------------------+
| |
| camelCase |
| - Start lowercase, capitalize subsequent words |
| - Used for: variables, methods in Java/C#/JS |
| - Example: firstName, calculateTotal |
| |
| PascalCase |
| - Capitalize first letter of each word |
| - Used for: classes, interfaces, methods in C# |
| - Example: BankAccount, CalculateTotal |
| |
| snake_case |
| - Lowercase with underscores between words |
| - Used for: variables, functions in Python/C |
| - Example: first_name, calculate_total |
| |
| UPPER_SNAKE_CASE |
| - All caps with underscores |
| - Used for: constants |
| - Example: MAX_CONNECTIONS, PI |
| |
+---------------------------------------------------------+
Good vs Bad Names
// BAD: Names that obscure meaning
int d; // elapsed time in days?
String x; // what is x?
void proc(); // what does it process?
// GOOD: Names that reveal intent
int elapsedTimeInDays;
String customerName;
void processPayment();
Naming Rules of Thumb
- Use pronounceable names โ you'll need to talk about them
- Avoid abbreviations โ write
buttonnotbtn - Use consistent terminology โ don't mix
fetch,get, andretrieve - Booleans should answer a question โ
isActivenotactive - Don't encode type in the name โ
stringNameis redundant
Key Takeaways
- Good names are the cheapest form of documentation
- Follow your language's conventions consistently
- Names should reveal intent, not implementation
- Maintainability starts with readability