The rules that PHP should follow to write code

Source: Internet
Author: User

Quoted in: https://zhuanlan.zhihu.com/p/33451652, thank summer God

1, use meaningful and readable variable names (be sure to define the meaning of the variable name)
Eg: $currentDate

2, use a searchable name
Eg: Defining constants in a class

3, avoid nesting too deep and returning early
Error:

1 functionIsshopopen ($day):BOOL2 {3 if($day) {4 if(is_string($day)) {5 $day=Strtolower($day);6 if($day= = = ' Friday ') {7 return true;8}ElseIf($day= = = ' Saturday ') {9 return true;Ten}ElseIf($day= = = ' Sunday ') { One return true; A}Else { - return false; - } the}Else { - return false; - } -}Else { + return false; - } +}

Correct: (Error can throw an exception)

function isshopopen (string$day): bool{if (empty($day ) {returnfalse;} $openingDays = [' Friday ', ' Saturday ', ' Sunday ']; return In_array (strtolower($day$openingDaystrue);}


4, be sure to define the type of the function variable (not conditional judgment)
Eg:public function Send (string $email, String $userName) {}

5, conditional judgment use equals character = =

6, the function parameter should be less than two parameters, it is more than three, encapsulates an object
eg:public function Send (User $user) {}

7, the function should do only one thing

8, the name of the function to say clearly what it does

9, do not use a Boolean variable as a function parameter
Unfriendly:

1 functionCreateFile (string $name, BOOL$temp=false):void2 {3 if($temp) {4 Touch('./temp/').$name);5}Else {6 Touch($name);7 }8}


Friendly to:

1 functionCreateFile (string $name):void2 {3 Touch($name);4 }5 6 functionCreatetempfile (string $name):void7 {8 Touch('./temp/').$name);9}

Note: This also conforms to the function only to do one thing

10, try not to define global functions

11, Package condition statement
Unfriendly:

if ($article->state = = = = ' published ') {
// ...
}
Friendly to:

if ($article->ispublished ()) {
// ...
}

12, avoid the use of antisense conditions to judge
Eg:if (!isdomnodenotpresent ($node))

13. Remove Useless code

14, use protected and private to describe the variables in the class

15, single principle of responsibility
Eg: as stated in the Clean Code book, "Modifying a class should be for one reason only". It's always easy to "cram" a class in a bunch of ways, as if we were going to be able to carry all the stuff into the box when we had to take a suitcase on a plane. The consequence of this is that, logically, such a class is not high cohesion and leaves a lot of reason to modify it later.
It is important to minimize the number of times you need to modify a class, because when there are many methods in a class, it is difficult to know which modules depend on it in the entire code base.

More bad:

1 classusersettings2 {3 Private $user;4 5  Public function__construct (User$user)6 {7 $this->user =$user;8 }9 Ten  Public functionChangesettings (Array $settings):void One { A if($this-verifycredentials ()) { - // ... - } the } -  - Private functionVerifycredentials ():BOOL - { + // ... - } +}

Awesome:

1 classUserauth2 {3 Private $user;4 5  Public function__construct (User$user)6 {7 $this->user =$user;8 }9 Ten  Public functionVerifycredentials ():BOOL One { A // ... - } - } the  - classusersettings - { - Private $user; + Private $auth; -  +  Public function__construct (User$user)  A { at $this->user =$user; - $this->auth =NewUserauth ($user); - } -  -  Public functionChangesettings (Array $settings):void - { in if($this->auth->verifycredentials ()) { - // ... to } + } -}

16, avoid writing duplicate code

The rules that PHP should follow to write code

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.