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