ryanmcdermott / clean-code-javascript

:bathtub: Clean Code concepts adapted for JavaScript
MIT License
91.72k stars 12.3k forks source link

Use default arguments instead of short circuiting or conditionals #230

Open mdv27 opened 7 years ago

mdv27 commented 7 years ago

It's mentioned that Other "falsy" values than undefined such as '', "", false, null, 0, and NaN, will not be replaced by a default value. I am doubtful as below line of code works with default value; let a = ""; let b = a || "my default value"; console.log(b) // prints my default value This even works for other falsy values.

MattShaile commented 6 years ago

I think that's the point. Consider this example:

function setEnable(enable) {
    enable = enable || true

    console.log(enable);
}

setEnable(); // true
setEnable(true); // true
setEnable(false); // true

Setting the param as false is not the same as leaving it undefined

kurtextrem commented 6 years ago

https://mrale.ph/blog/2018/02/03/maybe-you-dont-need-rust-to-speed-up-your-js.html#optimizing-sorting---argument-adaptation