CompTIA Tech+ (FC0-U71)Software Development ConceptsMedium

A developer is designing a program where several variables, such as 'username', 'password', 'userAge', and 'isValidUser', need to be declared. Which of the following best describes a common best practice for naming these variables to ensure readability and maintainability?

  1. AUsing names that are as long as possible to be extremely verbose (e.g., 'the_name_of_the_person_who_is_logging_in').
  2. BUsing single-letter abbreviations (e.g., 'u', 'p', 'ua', 'ivu').
  3. CUsing descriptive names that clearly indicate their purpose (e.g., 'username', 'password', 'userAge', 'isValidUser').
  4. DUsing all uppercase letters for all variables (e.g., 'USERNAME', 'PASSWORD', 'USERAGE', 'ISVALIDUSER').
Show answer & explanation

Correct answer: C. Using descriptive names that clearly indicate their purpose (e.g., 'username', 'password', 'userAge', 'isValidUser').

Using descriptive names that clearly indicate the variable's purpose significantly improves code readability and maintainability. This allows other developers (or the original developer later on) to understand the code more easily without extensive comments.

Why the other options are wrong

  • A. Excessively long names can make code harder to read and type, though descriptive names are good.
  • B. Single-letter abbreviations make code difficult to understand and maintain.
  • D. All uppercase is typically reserved for constants, not regular variables.

Variable Naming Conventions

Standardized rules or best practices for naming variables in programming to promote code readability, consistency, and maintainability.

  • Names should be descriptive and reflect the variable's purpose.
  • Avoid single-letter names unless their context is extremely clear (e.g., loop counters like 'i').
  • Follow a consistent style (e.g., camelCase, snake_case) throughout the codebase.
  • Avoid keywords, special characters, and starting with numbers.

Memory trick: Clear names make code shine, like a well-lit sign.

More Software Development Concepts questions