PHP gives the variable a short and unambiguous name

Source: Internet
Author: User

The code audit contains at least two aspects to first determine the purpose or function of the code, and also to check the readability of the code, that is, more conducive to understanding and maintenance.
Maybe you've audited a lot of code, too long variable names are really maddening. Not only does it affect the clarity of the code, but too long variables make your code difficult to scan, forcing more lines to wrap.
How to give a variable or method a good name
First you need to know what the name wants to say, and you should know that the name is irrelevant. In addition, all the strings in all the names are superfluous.
(1) The parameter type of the omitted variable
If you are a statically typed language, you will know the type of the variable or the method return value type, and then add the type to the name is superfluous

Bad:
String namestring;
Dockablemodelesswindow Dockablemodelesswindow;

Better:
String name;
Dockablemodelesswindow window;

Use complex numbers instead of certain types
Bad:
List<datetime> holidaydatelist;
Map<employee, role> Employeerolehashmap;

Better:
List<datetime> holidays;
Map<employee, role> employeeroles;

The method name should not describe the parameter or its type
Bad:
Mergetablecells (list<tablecell> cells)
Sorteventsusingcomparator (list<event> events,
Comparator<event> Comparator)

Better:
Merge (list<tablecell> cells)
Sort (list<event> events, comparator<event> Comparator)

(2) Omitting the ambiguous description in the naming
When you see this variable name recentlyupdatedannualsalesbid you're wondering
Is there an annual sales offer that is not recent?
Are there any updates to the recent annual sales report?
Is there a recently updated sales quotation that is not of the year?
...

Please remove these irrelevant descriptions, not to mention that you can add a comment.
Bad:
Finalbattlemostdangerousbossmonster;
Weaklingfirstencountermonster;

Better:
Boss
Firstmonster;

(3) Remove the content that is known to the context
For example, when class describes something, the method name does not need to be repeated.
Bad:
Class Annualholidaysale {
int _annualsalerebate;
void Promoteholidaysale () {...}
}

Better:
Class Annualholidaysale {
int _rebate;
void promote () {...}
}

(4) Omitting meaningless information
Many people like to name many business terms based on the product, such as data, state, amount, value, Manager,engine, object, entity, and instance. The existence of these words is meaningless.
Look at one of the links below.
Class Deliciousbelgianwaffleobject {
void Garnishdeliciousbelgianwaffle (
List<strawberry> strawberries) {...}
}

Is it cool to simplify it?
Class Waffle {
void garnish (list<strawberry> strawberries) {...}
}

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.