From the course: Agile Software Development: Clean Coding Practices

Variable names

From the course: Agile Software Development: Clean Coding Practices

Start my 1-month free trial

Variable names

- [Tutor] There are a few different rules to follow for crafting clean variable names. Singular nouns should be used to name variables that store primitive type values and object references. Using a singular noun, more clearly indicates that the variable only contains one value. Plural nouns should be used to name variables that store arrays or other collections. Using a plural noun, more clearly indicates that the variable contains multiple values. Verb forms should be avoided for variables that store primitive types. Variables that use verbs can easily be confused with methods. Instead of using verbs for variable names like this, convert them into nouns. In cases where a noun will not work, a past tense verb form works well. If you're working with a language that supports closures or lambda functions, be sure to follow the rules for method names when creating one of these variables. If the lambda variable isn't called like a function, and is instead passed into a method as a parameter to be called later, then it might make more sense to construct a noun using the verb as the base. Single letters should be avoided as variable names. Instead, the terms that the single letters are referring to should be spelled out. Confusing acronyms and abbreviations, should be avoided in variable names. Instead, acronyms should be separated and abbreviations should be spelled out. Avoid complicated prefixes, such as those that were popularized by Hungarian notation. The purpose of this notation is to duplicate the details of a variable's data type in its name. In modern languages, and with older languages using modern tools, this practice is no longer needed. Instead, just write the variable's name without any details of the data type it contains. A similar practice is to use the data type to craft a suffix for the variable name. These can be removed for the same reasons.

Contents