Skip to content

Commit

Permalink
Merge pull request piotrplenik#170 from peter-gribanov/delete_functio…
Browse files Browse the repository at this point in the history
…ns_one_thing

Delete "Functions should do one thing" section
  • Loading branch information
TomasVotruba authored Sep 18, 2019
2 parents 89ab6f1 + 5e2d423 commit 8693a08
Showing 1 changed file with 0 additions and 46 deletions.
46 changes: 0 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* [Use identical comparison](#use-identical-comparison)
4. [Functions](#functions)
* [Function arguments (2 or fewer ideally)](#function-arguments-2-or-fewer-ideally)
* [Functions should do one thing](#functions-should-do-one-thing)
* [Function names should say what they do](#function-names-should-say-what-they-do)
* [Functions should only be one level of abstraction](#functions-should-only-be-one-level-of-abstraction)
* [Don't use flags as function parameters](#dont-use-flags-as-function-parameters)
Expand Down Expand Up @@ -535,51 +534,6 @@ class Questionnaire

**[⬆ back to top](#table-of-contents)**

### Functions should do one thing

This is by far the most important rule in software engineering. When functions do more
than one thing, they are harder to compose, test, and reason about. When you can isolate
a function to just one action, they can be refactored easily and your code will read much
cleaner. If you take nothing else away from this guide other than this, you'll be ahead
of many developers.

**Bad:**
```php
function emailClients(array $clients): void
{
foreach ($clients as $client) {
$clientRecord = $db->find($client);
if ($clientRecord->isActive()) {
email($client);
}
}
}
```

**Good:**

```php
function emailClients(array $clients): void
{
$activeClients = activeClients($clients);
array_walk($activeClients, 'email');
}

function activeClients(array $clients): array
{
return array_filter($clients, 'isClientActive');
}

function isClientActive(int $client): bool
{
$clientRecord = $db->find($client);

return $clientRecord->isActive();
}
```

**[⬆ back to top](#table-of-contents)**

### Function names should say what they do

**Bad:**
Expand Down

0 comments on commit 8693a08

Please sign in to comment.