变量
使用有意义且可读的变量名
坏的:
$ymdstr = $moment->format('y-m-d');
好的:
$currentDate = $moment->format('y-m-d');
对同一类型的变量使用相同的词汇
坏的:
getUserInfo();
getUserData();
getUserRecord();
getUserProfile();
好的:
getUser();
使用可搜索的名称 (第1部分)
我们将阅读比我们自身编写还要多的代码。 我们编写的代码必须是可读和可搜索的,这一点很重要。通过不命名对理解我们程序有意义的变量,我们伤害了我们的读者。让你的名称具有可搜索特性。
坏的:
// 448 到底是干什么用的?
$result = $serializer->serialize($data, 448);
好的:
$json = $serializer->serialize($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
使用可搜索的名称 (第2部分)
坏的:
class User
{
// 7 到底是干什么的?
public $access = 7;
}
// 4 到底是干什么的?
if ($user->access & 4) {
// ...
}
// 这是怎么回事?
$user->access ^= 2;
好的:
class User
{
public const ACCESS_READ = 1;
public const ACCESS_CREATE = 2;
public const ACCESS_UPDATE = 4;
public const ACCESS_DELETE = 8;
// 默认情况下,用户可以读取、创建和更新某些内容
public $access = self::ACCESS_READ | self::ACCESS_CREATE | self::ACCESS_UPDATE;
}
if ($user->access & User::ACCESS_UPDATE) {
// 编辑 ...
}
// 拒绝创建内容的访问权限
$user->access ^= User::ACCESS_CREATE;
使用解释变量
坏的:
$address = 'One Infinite Loop, Cupertino 95014';
$cityZipCodeRegex = '/^[^,]+,\s*(.+?)\s*(\d{5})$/';
preg_match($cityZipCodeRegex, $address, $matches);
saveCityZipCode($matches[1], $matches[2]);
不错:
这个更好,但是我们仍然严重依赖正则表达式。
$address = 'One Infinite Loop, Cupertino 95014';
$cityZipCodeRegex = '/^[^,]+,\s*(.+?)\s*(\d{5})$/';
preg_match($cityZipCodeRegex, $address, $matches);
[, $city, $zipCode] = $matches;
saveCityZipCode($city, $zipCode);
好的:
通过子模式命名减少对正则表达式的依赖。
$address = 'One Infinite Loop, Cupertino 95014';
$cityZipCodeRegex = '/^[^,]+,\s*(?<city>.+?)\s*(?<zipCode>\d{5})$/';
preg_match($cityZipCodeRegex, $address, $matches);
saveCityZipCode($matches['city'], $matches['zipCode']);
避免嵌套太深并尽早返回(第 1 部分)
过多的 if-else 语句会使您的代码难以理解。
显式优于隐式。
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);
}
避免嵌套太深并尽早返回(第 2 部分)
Bad:
function fibonacci(int $n)
{
if ($n < 50) {
if ($n !== 0) {
if ($n !== 1) {
return fibonacci($n - 1) + fibonacci($n - 2);
}
return 1;
}
return 0;
}
return 'Not supported';
}
Good:
function fibonacci(int $n): int
{
if ($n === 0 || $n === 1) {
return $n;
}
if ($n >= 50) {
throw new Exception('Not supported');
}
return fibonacci($n - 1) + fibonacci($n - 2);
}
避免心理映射
不要强迫代码的读者理解变量的含义。
显式优于隐式。
Bad:
$l = ['Austin', 'New York', 'San Francisco'];
for ($i = 0; $i < count($l); $i++) {
$li = $l[$i];
doStuff();
doSomeOtherStuff();
// ...
// ...
// ...
// Wait, what is `$li` for again?
dispatch($li);
}
Good:
$locations = ['Austin', 'New York', 'San Francisco'];
foreach ($locations as $location) {
doStuff();
doSomeOtherStuff();
// ...
// ...
// ...
dispatch($location);
}
不要添加不需要的上下文
如果你的 类或对象
已经有明确的含义,不要在变量中再次重复它。
Bad:
class Car
{
public $carMake;
public $carModel;
public $carColor;
//...
}
Good:
class Car
{
public $make;
public $model;
public $color;
//...
}
本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。