20 code naming tips and programmer tips for each programmer

Source: Internet
Author: User

20 code naming tips and programmer tips for each programmer

Naming is required everywhere in the code. As a programmer, We have to name classes, variables, functions, parameters, namespaces, and so on. Below are 20 tips to help you improve your naming skills.

1. Use a name that can express the intention

The name can tell us what it wants to do, why it exists, and how it works. Selecting the name that can express the intent will help us better understand the code.

<span style="font-size:14px;">int d; // elapsed time in daysint elapsedTimeInDays;int daysSinceCreation;int daysSinceModification;int fileAgeInDays;</span>

In the above snippet, we can only know from the comment what the variable d is. So the person who reads the Code had to look for its instances to get clues in order to understand its meaning. Therefore, if we can name this variable well, people who read the code will be able to instantly understand the meaning of this variable.

2. Don't be afraid to spend time on selecting a name

You should try several different names until they are sufficient to describe their meanings. Do not be afraid to spend time on them. Those who read your code (including yourself) will benefit from this. In addition, a descriptive name can even help you clarify the module design in your mind. A good name does take time, but in the long run, the advantage is greater than the disadvantage.

3. Rename

If you think of a better name in the subsequent development process, do not hesitate to change it immediately. The current IDE makes name refactoring very easy.

4. Avoid interfering words in the name

For example, the synonyms of Manager, Processor, Data, Info, and "I don't know what this is" are all interference words. If you need to use these interfering words, it means that your name may be too cumbersome.

5. Be careful about the difficult-to-name classes/functions

A class or function that is difficult to name may be a code smell. Note:

  • Code is too much.
  • The code is not enough.
  • You have not fully understood this problem. You need to obtain more information first.
6. Class Name

Class should have the name of a noun or noun phrase, such as Customer, WikiPage, Account, and AddressParser. The inherited parent class should be a short and influential name. The name of a subclass should be long. adjectives are used to describe its parent class, for example, SavingsAccount is derived from Account.

7. variable name

The variable name should also be a noun. Most of them are derived from the classes it points. Boolean variables should be written as predicates, such as isEmpty and isTerminated, so that they can be easily understood in the if statement.

8. Method Name

The method name should be a verb or verb phrase, such as postPayment (), deletePage (), and save (). The accessors and runners should have the prefix get and set respectively. The method that returns a Boolean value should be prefixed with 'is', such as isPostable (), so that it is easy to understand in the if statement.

9. range size and variable name length

The length of the variable name must match its range size. If the variable range is short, the variable name length should be short. Otherwise, the variable name should be longer and more descriptive.

10. Size of the range and length of the method/Class Name

The length of the method and class name should be inversely proportional to its range. For public methods, shorter names are better because they are called multiple times. Private methods are called only within the scope of the class. A longer name can be used as a document. The exception to this rule is the name of the derived class. The more class is derived, the more adjectives are added before the base class, and the longer the name.

11. One concept and one word

Select a word for an abstract concept, and then do not change. For example, get (), fetch (), and retrieve () are obfuscated as equivalent methods in different classes. Consistent vocabulary is an important tool for programmers to control code.

12. do not apply the same word to two different concepts.

If you follow the 11th-a concept-a word principle, you can avoid many classes with the same method name. As long as the parameter list and the return values of various methods are semantically equivalent, no problem occurs. A problem occurs only when you apply the same word to two different concepts.

For example, we can use the add () method in multiple classes to create a new value by adding or connecting two existing values. If we need to introduce an add method in the class to add parameters to the set later, this will cause problems due to different semantics. This new method should be changed to insert ().

13. Name of the application solution Field

The code we write may be read by other programmers in the future. Therefore, using some technical terms for code naming will bring great benefits. For example, if appropriate algorithm names, design pattern names, and mathematical terms are used, these naming methods may make it easier for other programmers to understand programs and resonate.

14. Use the name of the problem domain

If you cannot find a technical term that is easy to understand, you can also find the appropriate code name from the problem area. When programmers who read your code in the future are not sure about the meaning of the Code, this will provide them with some clues to the problem.

15. Add meaningful context

Most names are meaningless and need to be placed in the context (Class/function/namespace) so that the people who read the code can understand what they refer. In some cases, prefix names may be required to supplement the context. For example, suppose we have some variables used to represent the address: firstName, lastName, street, houseNumber, city, state, and zip. If we only look at the state variable, it is difficult to infer what it means. A better solution is to encapsulate these variables into the Address class.

16. Do not add context without reason

A shorter name is usually better than a longer name. Some unnecessary information that can be inferred from the class/package/namespace should not be added before the name.

17. Avoid Coding

Given the strength of IDE, we do not need to encode the type and range information to the variable name and class name. This includes not having to add I to the interface, because users who use Code do not need to know that their classes are being passed to the interface. So if you must use encoding, it is best to encode the implementation rather than the interface.

18. Avoid incorrect information

Do not give some error information, because it will mislead people who read the code. If you name a variable that actually supports arrays as accountList, it is easy to draw a wrong conclusion.

19. Use the unreadable name

Programming is a social activity. The use of unreadable names only hinders our discussion.

20. Use a name that is easy to search

Using Short and general names will prevent us from searching for things in the code library. This has a significant impact on code manipulation and refactoring.

Finally, your code can be completed perfectly. Of course, there are other important steps, that is, adding a shell to the code, that is, encryption protection! Do not let the code development programs that you have worked hard to develop be used by others!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.