Code cleanliness (2)

Source: Internet
Author: User
Meaningful names and functions

Name:

  1. It takes a long time to select a good name, but it saves more time. If a better name is found, replace the old one.
  2. Avoid misleading --- for example, using accountlist to reference a group of accounts
  3. Use the read name --- for example, genymdhms (generation date, year, month, day, hour, minute, second). How can this be read ??
  4. Class Name --- class name and object name should be a noun or noun phrase, such as customer, wikipage, account, avoid using class names such as manager, processor, date.
  5. Method Name --- the method name should be a verb or verb phrase.

Function:

  1. Short-the first rule is short, and the second rule is short.
  2. Only do one thing-the function should do one thing; do it well; only do this thing;
  3. Each function has an abstract level-read code from top to bottom: downward rule
  4. Swith statement --- it is difficult to write short switch statements. If you cannot avoid using a switch, place it in a lower abstract level and only appear once.
  5. Use descriptive name ---
    1. Don't be afraid of long names. Long and descriptive names are better than short and confusing names.
    2. Long and descriptive names are better than descriptive long comments.
    3. Using a naming convention makes it easy to read multiple words in a function name;
    4. Don't be afraid to get a name for a long time. This will help you sort out the design ideas about the module;
    5. The naming method must be consistent, such as includesetupandteardownpages, includesetuppages, and includesuitesetuppage.
  6. Function parameters ---
    1. The ideal parameter is zero, followed by one, followed by two, and avoided as much as possible. Unless you have special reasons, do not use more than three.
    2. Do not use the identification parameter to input a Boolean value to the function, that is, the function does more than one thing. If it is true, false will certainly not do this. For example, render (Boolean issuite) has a little help, But you should split it into two. renderforsuite () and renderforsingletest ()
  7. No side effects ---
    1. The side effect is that you are lying. The function promises to do only one thing, but it will still do other things that are hidden.
  8. Do not repeat yourself

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.