diff --git a/README.md b/README.md index b5ae5f0..8eff07d 100644 --- a/README.md +++ b/README.md @@ -186,22 +186,40 @@ class Car ### Use default arguments instead of short circuiting or conditionals -**Bad:** +**Not good:** + +This is not good because `$breweryName` can be `NULL`. + ```php -function createMicrobrewery($name = null) { - $breweryName = $name ?: 'Hipster Brew Co.'; +function createMicrobrewery($breweryName = 'Hipster Brew Co.') +{ +    // ... +} +``` + +**Not bad:** + +This opinion is more understandable than the previous version, but it better controls the value of the variable. + +```php +function createMicrobrewery($name = null) +{ +    $breweryName = $name ?: 'Hipster Brew Co.'; // ... } - ``` **Good**: -```php -function createMicrobrewery($breweryName = 'Hipster Brew Co.') { - // ... -} +If you support only PHP 7+, then you can use [type hinting](http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration) and be sure that the `$breweryName` will not be `NULL`. + +```php +function createMicrobrewery(string $breweryName = 'Hipster Brew Co.') +{ +    // ... +} ``` + **[⬆ back to top](#table-of-contents)** ## **Functions** ### Function arguments (2 or fewer ideally)