Naming in programming

Source: Internet
Author: User

Naming in programming]

In the design process, a good name is not necessarily, but it is more likely to bring about a good design. However, a bad name will not bring you a good design. During the design process, if you find it difficult to name a module or a method, you may encounter a problem that is not difficult to name, but whether the design is true or not, maybe you should spend more time re-designing your module.

 

1. Use nouns whenever possible.

Bad: Happy
Good: Happiness

2. Do not use a prefix similar to a namespace.

Bad: systemonlinemessage
Good: System: Online: Message

3. Do not use too many adjectives. Just describe them clearly.

Bad: iabstractfactorypatternbase
Good: ifacloud

3. If a class cannot be described by simple naming, you can consider using analogy.

Bad: incomingmessagequeue
Characterarray
Spatialorganizer
Good: mailbox
String
Map

4. If you use analogy, you should use them in the same way.

Bad: Mailbox, destinationid
Good: Mailbox, address

5. Concise

Bad: List. getnumberofitems ()
Good: List. Count ()

6. Do not be too concise
  Bad:           list.Verify()
  Good:          list.ContainsNull()

7. Avoid abbreviations
  Bad:           list.Srt()
  Good:          list.Sort()

8. Use a verb for a function that completes a task
  Bad:           obj.RefCount();
  Good:          list.Clear();
                 list.Sort();
                 obj.AddReference();

9. For a function that returns a boolean type, use a similar method to ask a question.
  Bad:           list.Empty();
  Good:          list.IsEmpty();
                 list.Contains(item);

10. Nouns are used for functions that only return attributes without changing the state.
  Bad:           list.GetCount();
  Good:          list.Count();

12. Do not repeat the parameter name in the function name.
  Bad:           list.AddItem(item);
                 handler.ReceiveMessage(msg);
  Good:          list.Add(item);
                 handler.Receive(msg);

13. Do not repeat the class name of this method in the Method Name
  Bad:           list.AddToList(item);
  Good:          list.Add(item);

14. Do not add the return type to the function name unless the function name must be different from the return type.
  Bad:           list.GetCountInt();
  Good:          list.GetCount();
                 message.GetIntValue();
                 message.GetFloatValue();

15. Do not use and or
If you use a connector to connect the function name, this function must have done too many things, A better way is to divide it into smaller functions for processing (similar to the single responsibility principle in Object-Oriented Design Principles ).
If you want to ensure that this is an atomic operation, you should use a name to describe this operation or a class to encapsulate it.
   Bad:           mail.VerifyAddressAndSendStatus();
   Good:          mail.VerifyAddress();
                  mail.SendStatus();

 

Naming in programming

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.