Clean Code part 2: variables names
Choosing the right name
We have the power to assign a name to a variable and it's a great power! We must assign a name to a variable that it must reveal its intents just when I read the name of the variable. If we can assign the right name and understand what the variable will do and its role on the software and on the algorithm, it would be perfect!
Variables names must reveal their intent
It will be practical and when we will type the name of a class on your IDE, it will show you the list of the files more easily.
- Choose a highly descriptive name
- Classes and variables are nouns
- Function names and methods are vebrbs
- Avoid comments
- Avoid too generic words like account, manager, processor, data or info. They don't mean anything. Choose a more specific name.
- Boolean starts with "is". This will help us to remember the role of this particular kind of variables.
THE SCOPE RULES
A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable cannot be accessed.
- Big scope: assign short names. Classes with big scope have short names
- Small scope: assign long names. Private Methods can have long names
- Choose your names thoughtfully: thinking about a good name will help us to reveal the intents of the variable. If we think the variable must have a better name, we must rename it.
- Communicate your intent
- Avoid disinformation
- Use pronounceable names
- Avoid encodings and comments about the name itself!
- Choose parse of speech well