【进阶必看】代码整洁之道

文章很长,建议收藏有时间慢慢看
也可以关注微信公众号:php全栈技术
更新更频繁,不定时提供优质文章哦(●’◡’●)

使用有意义且可发音的变量名

Bad:

$ymdstr = $moment->format('y-m-d');

Good:

$currentDate = $moment->format('y-m-d');

对同一类型的变量使用相同的词汇表

Bad:

getUserInfo();
getUserData();
getUserRecord();
getUserProfile();

Good:

getUser();

使用可搜索的名称(第一部分)

Bad:

$result = $serializer->serialize($data, 448);

Good:

$json = $serializer->serialize($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);

使用可搜索的名称(第二部分)

Bad:

class User
{
   
    // What the heck is 7 for?
    public $access = 7;
}

// What the heck is 4 for?
if ($user->access & 4) {
   
    // ...
}

// What's going on here?
$user->access ^= 2;

Good:

class User
{
   
    public const ACCESS_READ = 1;

    public const ACCESS_CREATE = 2;

    public const ACCESS_UPDATE = 4;

    public const ACCESS_DELETE = 8;

    // User as default can read, create and update something
    public $access = self::ACCESS_READ | self::ACCESS_CREATE | self::ACCESS_UPDATE;
}

if ($user->access & User::ACCESS_UPDATE) {
   
    // do edit ...
}

// Deny access rights to create something
$user->access ^= User::ACCESS_CREATE;

避免嵌套太深,尽早返回

Bad:

function isShopOpen($day): bool
{
   
    if ($day) {
   
        if (is_string($day)) {
   
            $day = strtolower($day);
            if ($day === 'friday') {
   
                return true;
            } elseif ($day === 'saturday') {
   
                return true;
            } elseif ($day === 'sunday') {
   
                return true;
            }
            return false;
        }
        return false;
    }
    return false;
}

Good:

function isShopOpen(string $day): bool
{
   
    if (empty($day)) {
   
        return false;
    }

    $openingDays = ['friday', 'saturday', 'sunday'];

    return in_array(strtolower($day), $openingDays, true);
}

不要添加不必要的上下文

Bad:

class Car
{
   
    public $carMake;

    public $carModel;

    public $carColor;

    //...
}

Good:

class Car
{
   
    public $make;

    public $model;

    public $color;

    //...
}

函数参数(理想情况下为2个或更少)

限制函数参数的数量是非常重要的,因为它使测试函数更容易。有三个以上的参数会导致组合爆炸,你必须用每个单独的参数测试大量不同的情况。
零参数是理想的情况。一个或两个参数是可以的,应该避免三个参数。除此之外的任何东西都应该得到巩固。通常,如果您有两个以上的参数,那么您的函数尝试做的太多了。在没有的情况下,大多数情况下,更高级别的对象作为参数就足够了。

Bad:

class Questionnaire
{
   
    public function __construct(
        string $firstname,
        string $lastname,
        string $patronymic,
        string $region,
        string $district,
        string $city,
        string $phone,
        string $email
    ) {
   
        // ...
    }
}

Good:

class Name
{
   
    private $firstname;

    private $lastname;

    private $patronymic;

    public function __construct(string $firstname, string $lastname, string $patronymic)
    {
   
        $this->firstname = $firstname;
        $this->lastname = $lastname;
        $this->patronymic = $patronymic;
    }

    // getters ...
}

class City
{
   
    private $region;

    private $district;

    private $city;

    public function __construct(string $region, string $district, string $city)
    {
   
        $this->region = $region;
        $this->district = $district;
        $this->city = $city;
    }

    // getters ...
}

class Contact
{
   
    private $phone;

    private $email;

    public function __construct(string $phone, string $email)
    {
   
        $this->phone = $phone;
        $this->email = $email;
    }

    // getters ...
}

class Questionnaire
{
   
    public function __construct(Name $name, City $city, Contact $contact)
    {
   
        // ...
    }
}

函数名应该说明它们的作用

Bad:

class Email
{
   
    //...

    public function handle(): void
    {
   
        mail($this->to, $this->subject, $this->body);
    }
}

$message = new Email(...);
// What is this? A handle for the message? Are we writing to a file now?
$message->handle();

Good:

class Email
{
   
    //...

    public function send(): void
    {
   
        mail($this->to, $this->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值