DEV Community

Discussion on: if (!_if) what

Collapse
 
mahlongumbs profile image
Mahlon Gumbs

Both of those are pretty much the same. The first takes advantage of short circuiting but the flow is the same. In this case, it comes down to preference for the most part.

I would not keep the precisely because it is data. Better to look that up or have it injected so that data changes don't result in coding modifications.

Thanks for your comment.

Collapse
 
alainvanhout profile image
Alain Van Hout

Well, the compiled code (if there is such a thing) would be the same, but the denseness does differ a bit. Though whether the default value should be hardcoded, injected or looked up, that’s of course a different matter and will probably depend on business considerations (e.g. sometimes the default is, and can only be, a blank string, regardless of context).

Thread Thread
 
mahlongumbs profile image
Mahlon Gumbs

Sure. The example is a bit contrived. There would definitely be more error handling logic, as you've mentioned.

Help me understand what you mean by "denseness". From my pov, things look basically the same if you ignore the coding style.

const wow = arg => {
  return (
    animals.hasOwnProperty(arg) && animals[arg]
      || "gimme an animal";
  );
};

vs

const wow = arg => {
  if(animals.hasOwnProperty(arg)){ //WTF if, who invited you?
    return animals[arg];
  }
  return "gimme an animal";
};
Thread Thread
 
alainvanhout profile image
Alain Van Hout • Edited

I’m mostly referring to the fact that it’s a single versus two separate statement/expressions, with the corrolary that the latter allows a line of whitespace inbetween to emphasize them being separate concerns. Moving the second part of the A || B to the next line aids the separation a bit, most people tend to not do that unless the total statement/line is too long to fit.

Edit: thanks for the interesting discussion btw

Thread Thread
 
mahlongumbs profile image
Mahlon Gumbs • Edited

Ah...ok. Understood. I agree.

edit: Absolutely. That's what this is all about. Thanks for the feedback.