It should tell you why it exists, what it does, and how it is used. If a name requires a comment, then the name does not reveal its intent.
steps for good naming
- write the method
- rename the attributes to meaningful names
- be careful with names that are different only a little bit ex.: XYZabcdStorageHandling and XYZabcdLocationHandling
- no l or 0 letters in variable names since they look like digit 1 and 0
- Noise words are redundant. The word variable should never appear in a variable
name. The word table should never appear in a table name. How is NameString better than Name? Would a Name ever be a floating point number? If so, it breaks an earlier rule about disinformation. Imagine finding one class named Customer and another named CustomerObject. What should you understand as the distinction? Which one will represent the best path to a customer’s payment history?
- Class Names
Classes and objects should have noun or noun phrase names like Customer, WikiPage, Account, and AddressParser. Avoid words like Manager, Processor, Data, or Info in the name of a class. A class name should not be a verb.
- Method Names
Methods should have verb or verb phrase names like postPayment, deletePage, or save. Accessors, mutators, and predicates should be named for their value and prefixed with get,set, and is according to the javabean standard.
string name = employee.getName();
- Class Names
- However one might decide to use the word add for “consistency” when he or she is not in fact adding in the same sense. Let’s say we have many classes where add will create a new value by adding or concatenating two existing values. Now let’s say we are writing a new class that has a method that puts its single parameter into a collection. Should we call this method add? It might seem consistent because we have so many other add methods, but in this case, the semantics are different, so we should use a name like insert or append instead. To call the new method add would be a pun. Using the same term for two different ideas is essentially a pun.