Basic OO programming skills (2) -- naming
1. Use phrases as function names, nouns as class names, and descriptive words as variable names.
If business uses the domain model, the class name is usually the model name, which is the name directly mapped in our domain model:
Customer
Student
Account
Note: if there are names such as xxmanager, xxexecutor, and xxprocessor, you should consider modifying the design because it is likely that you have follow procedural programming thinking.
Method Name example
At the business layer, the method is usually the action of the model:
Postpayment ();
Submitorder ();
Saveorder ();
Variable name example
In general, the meanings of variables should be expressed as accurately as possible:
Intelapsedtimeindays; (instead of elapseddays or passeddays)
Intdayssincecreation; (not days)
Intdayssincemodification; (instead of changeddays)
Intfileageindays; (instead of fileage)
2. Do not use misleading names.
For example, student [] studentlist is not a good name, unless it is really a list <student> type, it is best to use students, here you can select studentarray or students is a good choice.
In addition, it is best to avoid using o and L. They look very similar to numbers 1 and 0.
3. Make the parameter name meaningful
For example
Public static void copychars (char A1 [], char A2 []) {
For (INT I = 0; I <a1.length; I ++ ){
A2 [I] = A1 [I];
}
}
A1 and A2 here, if you do not read the logic, you do not know what it represents. the better choice here is to use source and destination, so that the purpose of the parameter is clear at a glance.
4. Avoid abbreviations and abbreviations
For example, if chathistory is written as CH, you do not know what ch represents. Any name. If you need to map the name again, you need to consider changing the name.
5. Use unified words for the same concept
Don't forget, when you complete a story, when someone else reads code, it's like reading a story book and reading a class. It's like reading an article. To ensure the consistency of code naming, for example, if you use retrieve as the method name for the operation, do not use get. If you use controller as the class name, do not use manager.
6. Use the internal vocabulary of programmer as the suffix.
Don't forget that the code written is for programmer to read and expand. Therefore, we can use some internal words as suffixes to enhance code readability and smoothness. For example, lazyqueue can be used for algorithm-related purposes, and simfactory and andriodproxy are used for Mode-related purposes.
7. Use prefixes that express unified concepts
For example, fields related to address information: firstname, lastname, postalcode, unitno, and streetname. When they appear together, we may think that they both express the concept-address. But when they appear separately, we cannot guess that they are part of the address class. Therefore, we can use addrfirstname, addrlastname, and so on as the field name. This effectively limits the meaning of a field to express part of the address information.