Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Use default arguments instead of short circuiting or conditionals #14

Closed
peter-gribanov opened this issue Aug 29, 2017 · 2 comments

Comments

@peter-gribanov
Copy link
Contributor

You broke your code.
https://github.com/jupeter/clean-code-php#use-default-arguments-instead-of-short-circuiting-or-conditionals

Original is good

function createMicrobrewery($name = null) {
    $breweryName = $name ?: 'Hipster Brew Co.';
    var_dump($breweryName);
}
createMicrobrewery();
createMicrobrewery('');
createMicrobrewery(null);
createMicrobrewery('foo');

Result

string(16) "Hipster Brew Co."
string(16) "Hipster Brew Co."
string(16) "Hipster Brew Co."
string(3) "foo"

Your "good"

function createMicrobrewery($breweryName = 'Hipster Brew Co.') {
    var_dump($breweryName);
}
createMicrobrewery();
createMicrobrewery('');
createMicrobrewery(null);
createMicrobrewery('foo');

Result

string(16) "Hipster Brew Co."
string(0) ""
NULL
string(3) "foo"

Result is different. You broke your code.

@piotrplenik
Copy link
Owner

Good point.
Do you have propose of better example?

@peter-gribanov
Copy link
Contributor Author

Problem is fixed in #26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants