clean-code-javascript icon indicating copy to clipboard operation
clean-code-javascript copied to clipboard

Maybe remove "Short-circuiting is cleaner than conditionals"?

Open adamshone opened this issue 9 years ago • 3 comments

Because:

a) You already have "Use default arguments instead of short circuiting", which is a better approach (if supported), and

b) I have seen bugs introduced by writing a short circuit when the function is supposed to accept falsy values:

function double(x) {
   var value = x || -1;
   return value * 2;
}

double(0);  // returns -2, should return 0

adamshone avatar Jan 07 '17 20:01 adamshone

Because 0 is considered as false all time, that's why you will keep getting -2

JefferyHus avatar Jan 08 '17 00:01 JefferyHus

I know, that's my point :)

I've seen more than one bug introduced when people get into the habit of using short circuits to set default values, and don't consider that some functions should accept zero or the empty string as valid inputs.

That's why I think it might be time to drop the short circuit advice and just recommend using default parameters, which are better because they only match undefined.

adamshone avatar Jan 08 '17 09:01 adamshone

Hey there Adam! I think this should now be fixed after #34

ryanmcdermott avatar Jan 11 '17 03:01 ryanmcdermott