Labs ICT
โญ Pro Login

Variable Naming & Conventions

Best practices for naming variables, functions, and classes.

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 button not btn
  • Use consistent terminology โ€” don't mix fetch, get, and retrieve
  • Booleans should answer a question โ€” isActive not active
  • Don't encode type in the name โ€” stringName is 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

๐Ÿงช Quick Quiz

Which of the following is a good variable naming practice?