wiki

View on GitHub

Đặt tên biến dễ hiểu

//bad
<?php

 $ymdstr = $moment->format('y-m-d');
//good
<?php

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

tránh lồng quá nhiều và nên return sớm

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; } else { return false; } } else { return false; } } else { return false; } } //good <?php function isShopOpen(string $day): bool { if (empty($day)) { return false; }

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

return in_array(strtolower($day), $openingDays, true); } ```` ## Đừng thêm những nội dung không cần thiết - Nếu tên của class/object đã rõ ràng, không nên lặp lại chúng trong tên biến.
//bad
<?php
class Car
{
    public $carMake;
    public $carModel;
    public $carColor;

    //...
}
//good
<?php
class Car
{
    public $make;
    public $model;
    public $color;

    //...
}