|
18 | 18 | * [Use identical comparison](#use-identical-comparison) |
19 | 19 | 4. [Functions](#functions) |
20 | 20 | * [Function arguments (2 or fewer ideally)](#function-arguments-2-or-fewer-ideally) |
21 | | - * [Functions should do one thing](#functions-should-do-one-thing) |
22 | 21 | * [Function names should say what they do](#function-names-should-say-what-they-do) |
23 | 22 | * [Functions should only be one level of abstraction](#functions-should-only-be-one-level-of-abstraction) |
24 | 23 | * [Don't use flags as function parameters](#dont-use-flags-as-function-parameters) |
@@ -535,51 +534,6 @@ class Questionnaire |
535 | 534 |
|
536 | 535 | **[⬆ back to top](#table-of-contents)** |
537 | 536 |
|
538 | | -### Functions should do one thing |
539 | | - |
540 | | -This is by far the most important rule in software engineering. When functions do more |
541 | | -than one thing, they are harder to compose, test, and reason about. When you can isolate |
542 | | -a function to just one action, they can be refactored easily and your code will read much |
543 | | -cleaner. If you take nothing else away from this guide other than this, you'll be ahead |
544 | | -of many developers. |
545 | | - |
546 | | -**Bad:** |
547 | | -```php |
548 | | -function emailClients(array $clients): void |
549 | | -{ |
550 | | - foreach ($clients as $client) { |
551 | | - $clientRecord = $db->find($client); |
552 | | - if ($clientRecord->isActive()) { |
553 | | - email($client); |
554 | | - } |
555 | | - } |
556 | | -} |
557 | | -``` |
558 | | - |
559 | | -**Good:** |
560 | | - |
561 | | -```php |
562 | | -function emailClients(array $clients): void |
563 | | -{ |
564 | | - $activeClients = activeClients($clients); |
565 | | - array_walk($activeClients, 'email'); |
566 | | -} |
567 | | - |
568 | | -function activeClients(array $clients): array |
569 | | -{ |
570 | | - return array_filter($clients, 'isClientActive'); |
571 | | -} |
572 | | - |
573 | | -function isClientActive(int $client): bool |
574 | | -{ |
575 | | - $clientRecord = $db->find($client); |
576 | | - |
577 | | - return $clientRecord->isActive(); |
578 | | -} |
579 | | -``` |
580 | | - |
581 | | -**[⬆ back to top](#table-of-contents)** |
582 | | - |
583 | 537 | ### Function names should say what they do |
584 | 538 |
|
585 | 539 | **Bad:** |
|
0 commit comments