Skip to content

Commit 47d6f95

Browse files
Add origin bad example
1 parent 0fab26d commit 47d6f95

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,26 +171,40 @@ function paintCar(&$car) {
171171

172172
### Use default arguments instead of short circuiting or conditionals
173173

174+
**Not good:**
175+
176+
This is not good because `$breweryName` can be `NULL`.
177+
178+
```php
179+
function createMicrobrewery($breweryName = 'Hipster Brew Co.')
180+
{
181+
   // ...
182+
}
183+
```
184+
174185
**Not bad:**
175186

187+
This opinion is understandable than the previous version, but it better controls the value of the variable.
188+
176189
```php
177190
function createMicrobrewery($name = null)
178191
{
179192
   $breweryName = $name ?: 'Hipster Brew Co.';
180193
// ...
181194
}
182-
183195
```
184196

185-
**Good for PHP 7+**:
197+
**Good**:
198+
199+
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`.
186200

187201
```php
188202
function createMicrobrewery(string $breweryName = 'Hipster Brew Co.')
189203
{
190204
   // ...
191205
}
192-
193206
```
207+
194208
**[⬆ back to top](#table-of-contents)**
195209
## **Functions**
196210
### Function arguments (2 or fewer ideally)

0 commit comments

Comments
 (0)