1
0
mirror of https://github.com/jupeter/clean-code-php.git synced 2025-09-26 14:08:59 +02:00

Merge pull request #24 from peter-gribanov/showList

Optimize showList() function
This commit is contained in:
Tomáš Votruba
2017-08-31 00:12:47 +02:00
committed by GitHub

View File

@@ -337,7 +337,8 @@ function parseBetterJSAlternative($code) {
} }
``` ```
**Good**: **Good:**
```php ```php
function tokenize($code) { function tokenize($code) {
$regexes = [ $regexes = [
@@ -397,9 +398,11 @@ a good abstraction, do it! Don't repeat yourself, otherwise you'll find yourself
updating multiple places anytime you want to change one thing. updating multiple places anytime you want to change one thing.
**Bad:** **Bad:**
```php ```php
function showDeveloperList($developers) { function showDeveloperList($developers)
foreach($developers as $developer) { {
   foreach ($developers as $developer) {
$expectedSalary = $developer->calculateExpectedSalary(); $expectedSalary = $developer->calculateExpectedSalary();
$experience = $developer->getExperience(); $experience = $developer->getExperience();
$githubLink = $developer->getGithubLink(); $githubLink = $developer->getGithubLink();
@@ -413,8 +416,9 @@ function showDeveloperList($developers) {
} }
} }
function showManagerList($managers) { function showManagerList($managers)
foreach($managers as $manager) { {
   foreach ($managers as $manager) {
$expectedSalary = $manager->calculateExpectedSalary(); $expectedSalary = $manager->calculateExpectedSalary();
$experience = $manager->getExperience(); $experience = $manager->getExperience();
$githubLink = $manager->getGithubLink(); $githubLink = $manager->getGithubLink();
@@ -429,13 +433,15 @@ function showManagerList($managers) {
} }
``` ```
**Good**: **Good:**
```php ```php
function showList($employees) { function showList($employees)
foreach($employees as $employe) { {
$expectedSalary = $employe->calculateExpectedSalary();    foreach ($employees as $employe) {
$experience = $employe->getExperience();        $expectedSalary = $employe->calculateExpectedSalary();
$githubLink = $employe->getGithubLink();        $experience = $employe->getExperience();
       $githubLink = $employe->getGithubLink();
$data = [ $data = [
$expectedSalary, $expectedSalary,
$experience, $experience,
@@ -446,6 +452,24 @@ function showList($employees) {
} }
} }
``` ```
**Very good:**
It is better to use a compact version of the code.
```php
function showList($employees)
{
   foreach ($employees as $employe) {
render([
$employe->calculateExpectedSalary(),
$employe->getExperience(),
$employe->getGithubLink()
]);
}
}
```
**[⬆ back to top](#table-of-contents)** **[⬆ back to top](#table-of-contents)**
### Don't use flags as function parameters ### Don't use flags as function parameters