## 别写重复代码 (DRY)
遵循[DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) 原则要尽量避免复制代码,这是一种非常糟糕的行为,通常意味着需要变更一些逻辑时,需要修改不止一处。
通常情况下复制代码是应该有两个或者多个略微不同的逻辑,它们大多数都是一样的,但是由于它们的区别致使你必须有两个或者多个隔离的但大部分相同的方法,移除重复的代码意味着用一个function/module/class创建一个能处理差异的抽象。
不合理的抽象比复制代码更糟糕,所有务必谨慎!如果能设计一个合理的抽象,实现它并不要重复,否则会发现任何时候当你想修改一个逻辑时你必须修改多个地方。
**差:**
```php
function showDeveloperList(array $developers): void
{
foreach ($developers as $developer) {
$expectedSalary = $developer->calculateExpectedSalary();
$experience = $developer->getExperience();
$githubLink = $developer->getGithubLink();
$data = [
$expectedSalary,
$experience,
$githubLink
];
render($data);
}
}
function showManagerList(array $managers): void
{
foreach ($managers as $manager) {
$expectedSalary = $manager->calculateExpectedSalary();
$experience = $manager->getExperience();
$githubLink = $manager->getGithubLink();
$data = [
$expectedSalary,
$experience,
$githubLink
];
render($data);
}
}
```
**优:**
```php
function showList(array $employees): void
{
foreach ($employees as $employee) {
$expectedSalary = $employee->calculateExpectedSalary();
$experience = $employee->getExperience();
$githubLink = $employee->getGithubLink();
$data = [
$expectedSalary,
$experience,
$githubLink
];
render($data);
}
}
```
**极好:**
最好让代码紧凑一点
```php
function showList(array $employees): void
{
foreach ($employees as $employee) {
render([
$employee->calculateExpectedSalary(),
$employee->getExperience(),
$employee->getGithubLink()
]);
}
}
```