DEV Community

if (!_if) what

Mahlon Gumbs on May 24, 2018

From time to time, the use of if statements causes a bit of debate in my computing circles (it's funny to hear us start arguments with "if you use ...
Collapse
 
alainvanhout profile image
Alain Van Hout

Very interesting and clear, thanks!

I’d argue that the second version of the data map solution (with the if rather than the ||) is the most maintainable, since it had the advantage of the data map, yet also has the defaulting behaviour as a separate part (rather than have it be clumped together with the happy-flow-case).

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.

Collapse
 
jakebman profile image
jakebman

Is there a way to modify getAnimals() to change the default answer?

Collapse
 
mahlongumbs profile image
Mahlon Gumbs • Edited

Edit, I misread the question. Here is the corrected response.

Sure. As @Alain mentioned, you can use an optional parameter.

Optional Parameter

const loadAnimals = () => {  
  animals = {
    dog: "LOVELY",
    cat: "CUTE",
    pony: "AWESOME",
  };
};

const getAnimals = (overrides = {}) => {
  if(!animals) loadAnimals();
  return {
    ...animals,
    ...overrides,
  };
};

getAnimals({
  dog: "GREAT",
  monkey: "AGILE",
}); // -> {dog: "GREAT", cat: "CUTE", pony: "AWESOME", monkey: "AGILE"}

Thanks for your comment.

Collapse
 
alainvanhout profile image
Alain Van Hout

It could be passed as a(n optional) second parameter.