When naming a variable, you can ask yourself the following questions to ensure the name is meaningful and clear:
-
🤔 What does the variable represent? Choose a name that accurately reflects the purpose or meaning of the variable.
- Bad example:
x
(doesn’t provide any information about what the variable represents) - Good example:
customerName
(clearly indicates that the variable stores the name of a customer)
- Bad example:
-
💡 What type of data does the variable hold? Include the data type or use a descriptive term if it helps to clarify the purpose of the variable.
- Bad example:
value
(ambiguous and doesn’t specify the type of value) - Good example:
orderTotalAmount
(indicates that the variable stores the total amount of an order)
- Bad example:
-
🎯 Is there a more precise or specific term available? Use specific terms when they accurately describe the purpose or role of the variable.
- Bad example:
variable1
(vague and doesn’t convey any meaning) - Good example:
maxTemperature
(clearly indicates that the variable represents the maximum temperature)
- Bad example:
-
🌐 Does the name provide enough context? Ensure that the name provides sufficient context within the scope of its usage.
- Bad example:
state
(ambiguous when used alone without additional context) - Good example:
addressState
(specifies that the variable represents the state within an address)
- Bad example:
-
📝 Is the name concise and readable? Choose a name that is not excessively long but still conveys the intended meaning.
- Bad example:
thisVariableStoresTheCurrentYear
(too long and cumbersome) - Good example:
currentYear
(clear and concise)
- Bad example: