Skip to content
Permalink
Browse files

Merge pull request #170 from peter-gribanov/delete_functions_one_thing

Delete "Functions should do one thing" section
  • Loading branch information
TomasVotruba committed Sep 18, 2019
2 parents 89ab6f1 + 5e2d423 commit 8693a080737ff2b5b33ebf6930ebf082c0fa18c2
Showing with 0 additions and 46 deletions.
  1. +0 −46 README.md
@@ -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)
@@ -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:**

0 comments on commit 8693a08

Please sign in to comment.
You can’t perform that action at this time.