mirror of
https://github.com/jupeter/clean-code-php.git
synced 2025-09-27 06:29:01 +02:00
Add origin bad example
This commit is contained in:
20
README.md
20
README.md
@@ -171,26 +171,40 @@ function paintCar(&$car) {
|
|||||||
|
|
||||||
### Use default arguments instead of short circuiting or conditionals
|
### Use default arguments instead of short circuiting or conditionals
|
||||||
|
|
||||||
|
**Not good:**
|
||||||
|
|
||||||
|
This is not good because `$breweryName` can be `NULL`.
|
||||||
|
|
||||||
|
```php
|
||||||
|
function createMicrobrewery($breweryName = 'Hipster Brew Co.')
|
||||||
|
{
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
**Not bad:**
|
**Not bad:**
|
||||||
|
|
||||||
|
This opinion is understandable than the previous version, but it better controls the value of the variable.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
function createMicrobrewery($name = null)
|
function createMicrobrewery($name = null)
|
||||||
{
|
{
|
||||||
$breweryName = $name ?: 'Hipster Brew Co.';
|
$breweryName = $name ?: 'Hipster Brew Co.';
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**Good for PHP 7+**:
|
**Good**:
|
||||||
|
|
||||||
|
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
|
```php
|
||||||
function createMicrobrewery(string $breweryName = 'Hipster Brew Co.')
|
function createMicrobrewery(string $breweryName = 'Hipster Brew Co.')
|
||||||
{
|
{
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**[⬆ back to top](#table-of-contents)**
|
**[⬆ back to top](#table-of-contents)**
|
||||||
## **Functions**
|
## **Functions**
|
||||||
### Function arguments (2 or fewer ideally)
|
### Function arguments (2 or fewer ideally)
|
||||||
|
Reference in New Issue
Block a user