CompTIA Tech+ (FC0-U71)Software Development ConceptsEasy
A developer is designing a program where several variables, such as 'username', 'password', and 'email', need to be declared. Which of the following is an example of good practice for naming these variables to improve code readability?
- AUsing names that start with numbers, like '1user', '2pass'.
- BUsing descriptive names like 'username', 'password', 'email'.
- CUsing short, cryptic names like 'u', 'p', 'e'.
- DUsing all uppercase letters for all variables, like 'USERNAME', 'PASSWORD'.
Show answer & explanationAnswer & explanation
Correct answer: B. Using descriptive names like 'username', 'password', 'email'.
Descriptive variable names significantly enhance code readability and maintainability. Names like 'username', 'password', and 'email' clearly indicate the purpose of each variable, making the code easier to understand for anyone reading it.
Why the other options are wrong
- A. Most programming languages do not allow variable names to start with numbers.
- C. Cryptic names reduce readability and make code harder to understand.
- D. All uppercase is typically reserved for constants, not general variables, and can hinder readability.
Variable Naming Conventions
Variable naming conventions are a set of rules or guidelines for choosing identifiers for variables, functions, and other entities in code. Good conventions improve code readability, maintainability, and consistency.
- Names should be descriptive and meaningful
- Avoid single-letter or cryptic names
- Follow language-specific conventions (e.g., camelCase, snake_case)
- Avoid using reserved keywords
Memory trick: Clear names make code easy to tame!