Good code style build-up

Source: Internet
Author: User

1. Avoid using magic number

if ($age <18) {

}

This 18 doesn't quite understand why it's going to be this way.

28 can be defined in a variable, the variable name indicates the meaning of this value

$adult _age = 18;//The age of the adult demarcation point

if ($age < $adult _age) {

}

2. Return result of function: Do not use a variable to store the returned result

Once you know the return result, you should return immediately. The advantage of doing this is that you can reduce errors.

3, function with a lot of parameters. No more than three

If there are many parameters, try to aggregate into a model to pass in. For example, an array, an instance can be.

Why are too many parameters affecting the stability of the method?

Changes, for example, can become cumbersome.

I understand now. This method needs to be passed in with a new parameter, so the code that originally called this method has to be changed.

When you do the interface, you often encounter similar problems.

function forgot ($userName, $email, $email _url, $format = ' json ')

The above is a three parameter.

Originally someone called this function is,

Forgot ($userName, $email, $email _url, $format = ' json ');

Now that the demand is changing, a new parameter needs to be added. What to do?

The original code will have to be modified accordingly. method is not stable. Or just re-open a way to adapt to the new requirements. or modify the caller's calling code.

But there's a kind of thing to avoid.

The original passed parameters are made into an array form, as follows:

Forgot ($params =array (), $format = ' json ');

Aggregated into an array. This allows you to add any number of arguments.

How to understand: PHP engine built-in functions, with multiple parameters of the case?

Design flaws?

4. The parameters of the method contain Boolean parameters.

This means that this method is not accomplishing a single goal. violated a single duty. Added complexity.

Thinking: How to understand the problems in our code now?

Good code style build-up

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.