From ee26d57559b30ba8136fa99303607bf1514d1d48 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 29 Aug 2017 19:23:58 +0300 Subject: [PATCH 1/2] Optimize showList function --- README.md | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index eb072e6..99c2010 100644 --- a/README.md +++ b/README.md @@ -418,16 +418,11 @@ function showManagerList($managers) { ```php function showList($employees) { foreach($employees as $employe) { - $expectedSalary = $employe->calculateExpectedSalary(); - $experience = $employe->getExperience(); - $githubLink = $employe->getGithubLink(); - $data = [ - $expectedSalary, - $experience, - $githubLink - ]; - - render($data); + render([ + $employe->calculateExpectedSalary(), + $employe->getExperience(), + $employe->getGithubLink() + ]); } } ``` From eccde56b6ba6aaefeeb639da21087b5445b7c880 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Aug 2017 21:54:23 +0300 Subject: [PATCH 2/2] use compact version of code --- README.md | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 99c2010..4527b7a 100644 --- a/README.md +++ b/README.md @@ -322,7 +322,8 @@ function parseBetterJSAlternative($code) { } ``` -**Good**: +**Good:** + ```php function tokenize($code) { $regexes = [ @@ -382,9 +383,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. **Bad:** + ```php -function showDeveloperList($developers) { - foreach($developers as $developer) { +function showDeveloperList($developers) +{ +    foreach ($developers as $developer) { $expectedSalary = $developer->calculateExpectedSalary(); $experience = $developer->getExperience(); $githubLink = $developer->getGithubLink(); @@ -398,8 +401,9 @@ function showDeveloperList($developers) { } } -function showManagerList($managers) { - foreach($managers as $manager) { +function showManagerList($managers) +{ +    foreach ($managers as $manager) { $expectedSalary = $manager->calculateExpectedSalary(); $experience = $manager->getExperience(); $githubLink = $manager->getGithubLink(); @@ -414,10 +418,34 @@ function showManagerList($managers) { } ``` -**Good**: +**Good:** + ```php -function showList($employees) { - foreach($employees as $employe) { +function showList($employees) +{ +    foreach ($employees as $employe) { +        $expectedSalary = $employe->calculateExpectedSalary(); +        $experience = $employe->getExperience(); +        $githubLink = $employe->getGithubLink(); + $data = [ + $expectedSalary, + $experience, + $githubLink + ]; + + render($data); + } +} +``` + +**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(), @@ -426,6 +454,7 @@ function showList($employees) { } } ``` + **[⬆ back to top](#table-of-contents)** ### Don't use flags as function parameters