1. Meaningful naming (a neat way of code)

1. Meaningful naming (a neat way of code)


table of Contents

  1. Worthy of the name
  2. Avoid misleading
  3. Make meaningful distinctions
  4. Use the read name
  5. Use searchable names
  6. Avoid coding
  7. Avoid mind mapping
  8. Class name
  9. Method name
  10. Each concept corresponds to a word
  11. Don't use puns
  12. Use the solution domain name
  13. Use names derived from the problem area
  14. Add meaningful context
  15. Don’t add context

Note: The clean and tidy code PDF: https://pan.baidu.com/s/16PLDWPiusGjcUfW_jgOm5w Password: s708


1. worthy of the name

  1. The name of the variable, function, or class should already answer all the big questions. It should tell you why it exists, what it does, and how it should be used.
int d; //消逝的时间,以日计
  1. The name d doesn’t say anything, you should choose a name that indicates the measurement object and measurement unit
int elapsedTimeInDays;
int daysSinceCreation;
int daysSinceModification;
int fileAgeInDays;

2. Avoid misleading

  1. Beware of using names with less differences. For example, XYZControllerForEfficientHandlingOfStrings and XYZControllerForEfficientStorageOfStrings in another place.
  2. Spelling the same concept in the same way is information. Inconsistent spelling is misleading.
  3. Don't use accountList to refer to a group of accounts, unless it is really a List type. The term List has a special meaning for programmers. If the container containing the account is not a List, it will cause wrong judgment. Therefore, it would be better to use accountGroup or bunchOfAccounts, or even to use accounts directly.

3. Make meaningful distinctions

  1. Nonsense is a meaningless distinction. Assuming you have a Product class and a ProductInfo or ProductData class, although their names are different, they have no difference in meaning.
  2. The nonsense is redundant, and the word Variable should never appear in variable names. Table should never appear in the table name.
  3. To distinguish between names, it is necessary to distinguish the differences in a way.

4. Use the read name

5. Use searchable names

  1. There is a problem with single-letter and numeric constants, which is difficult to find in a large text.
  2. It is easy to find MAX_CLASSES_PER_STUDENT, but it is troublesome to find the number 7.
  3. Single-letter names are only used for local variables in short methods, and the length of the name should correspond to the size of its scope.

6. Avoid coding

7. Avoid mind mapping

  1. You shouldn’t let people who see you translate your name into a name they know well.
  2. One-letter variable names are a problem. When the scope is small and there is no conflict, the loop counter may naturally be named i or j or k. This is because traditionally single-letter loop counters are used.

8. Class name

  1. Class names and object names should be nouns or noun phrases, such as Customer, WikiPage, Account, and AddressParser.
  2. Avoid using class names like Manager, Processor, Data, and Info.
  3. Class names should not be verbs.

9. Method name

  1. The method name should be a verb or a verb phrase, such as postPayment, deletePage, and save.

10. Each concept corresponds to a word

  1. Choose a word for each abstract concept and stick to it consistently. For example, if you use fetch, retrieve, and get to name the same method in multiple classes, how do you remember that method in which class?
  2. The function name should be unique and consistent.

11. Don't use puns

12. Use the solution domain name

  1. Remember, only programmers can read your code, so just use computer science terms, algorithm names, model names, and mathematical terms.
  2. It is not advisable to base the name on the domain of the question, because the collaborator should not always ask the customer what each name means.
  3. For programmers familiar with the room problem model, the name AccountVisitor is meaningful.
  4. It is usually the most reliable way to give these things a technical name.

13. Use names derived from the problem area

14. Add meaningful context

  1. You need to use well-named tiredness, functions, or namespaces to place the name and provide context to the reader. If you don't do this, prefixing the name is the last resort.
  2. Suppose there are variables named firstName, lastName, street, houseNumber, city, state, and zipcode. When they are put together, they clearly constitute an address. But if you only see a solitary state variable in a certain method, it is difficult to have short legs, that is part of the address
  3. You can add a prefix to provide context. A better solution is to create a class called Address.

15. Don't add context that you don't have

  1. As long as the short name is clear enough, it is better than the long name. Don't add unnecessary context to the name.
  2. For entities of the Address class, accountAddress and customerAdderss are both good names, but they are not good for class names. Address is a good class name. If you need to distinguish it from the MAC address, port address, and Web address, you will consider using PostalAddress, MAC, and URI. Such names are more accurate, and accuracy is the point of naming.

Guess you like

Origin blog.csdn.net/weixin_41910694/article/details/110896225