Profile:
Clean Code PHPIs based on Robert Martin’s classic programming book
“The Code Clean Way”This is not a style guide, but rather a guide to writing readable, reusable, and reconfigurable software in PHP. Of course, don’t follow the principles here mechanically.

Do not add unnecessary context

If your class name or object name has a specific meaning, do not repeat the variable name.

Poor:


        

class Car
{
    public $carMake;
    public $carModel;
    public $carColor;

    / /...
}
Copy the code

Good:


        

class Car
{
    public $make;
    public $model;
    public $color;

    / /...
}
Copy the code

Number of function arguments (ideally less than 2)

Limiting the number of arguments to a function is important because it makes the function easier to test. With more than three arguments, you have to test a lot of different cases with each individual argument.

No parameters is the ideal case. One or two parameters are ok, but three should be avoided. In general, if you have more than two arguments, your function is trying to do too much. If not, most of the time, higher-level objects will suffice.

Poor:


        

function createMenu($title. $body. $buttonText. $cancellable) {
    // ...
}
Copy the code

Good:


         class MenuConfig {
    public $title;
    public $body;
    public $buttonText;
    public $cancellable = false;
}

$config = new MenuConfig(a);
$config->title = 'Foo';
$config->body = 'Bar';
$config->buttonText = 'Baz';
$config->cancellable = true;

function createMenu(MenuConfig $config) {
    // ...
}
Copy the code

A function should only do one thing

This is the most important rule in software engineering. When functions do more than one thing, they are harder to write and test. When you can isolate functions into an action, you can easily refactor and your code will be more readable.

Poor:


        

function emailClients($clients) {
    foreach ($clients as $client) {
        $clientRecord = $db->find($client);
        if ($clientRecord->isActive()) {
            email($client);
        }
    }
}
Copy the code

Good:

function emailClients($clients) {
    $activeClients = activeClients($clients);
    array_walk($activeClients, 'email');
}

function activeClients($clients) {
    return array_filter($clients, 'isClientActive');
}

function isClientActive($client) {
    $clientRecord = $db->find($client);
    return $clientRecord->isActive();
}
Copy the code

Use the get and set methods

In PHP, you can set the public, protected, and private keywords for methods to control the visibility of properties on objects. This is part of the open/closed principle of object-oriented design.

Poor:

class BankAccount
{
    public $balance = 1000;
}

$bankAccount = new BankAccount();

// Buy shoes...
$bankAccount->balance -= 100;
Copy the code

Good:

class BankAccount
{
    private $balance;

    public function __construct($balance = 1000)
    {
      $this->balance = $balance;
    }

    public function withdrawBalance($amount)
    {
        if ($amount > $this->balance) {
            throw new \Exception('Amount greater than available balance.');
        }

        $this->balance -= $amount;
    }

    public function depositBalance($amount)
    {
        $this->balance += $amount;
    }

    public function getBalance()
    {
        return $this->balance;
    }
}

$bankAccount = new BankAccount();

// Buy shoes...
$bankAccount->withdrawBalance($shoesPrice);

// Get balance
$balance = $bankAccount->getBalance();
Copy the code

The original:
Clean Code Concepts Adapted for PHP

Read more:

  • You should learn some functional programming in 2017
  • How big the Python family is
  • jupeter/clean-code-php

Welcome to:

  • Zhihu’s aurora Daily provides Makers with three quality English articles a day.
  • NetEase Cloud radio “Aurora Daily”, for you to read the newspaper on your commute.
  • Wechat public account “Aurora Developers”, two technical dry goods a week.