DEV Community

Discussion on: Washing your code: avoid conditions

Collapse
 
falco467 profile image
falco467

I would find the whole horns&hooves stuff a lot more readable with ternary operators instead of this function array construct.

const getSpecialOffersArray = withSessionCache(
  SPECIAL_OFFERS_CACHE_KEY,
  brand =>
    ({
      [BRANDS.HORNS_AND_HOOVES]: getHornsAndHoovesSpecialOffers,
      [BRANDS.PAWS_AND_TAILS]: getPawsAndTailsSpecialOffers
    }[brand]())
);

I would instead write as:

const getSOArray = wrapFnWithCache(SPECIAL_OFFERS_KEY, 
    brand === BRANDS.HnH ? getHnHSpecialOffers 
  : brand === BRANDS.PnT ? getPnTSpecialOffers
  : null)
Collapse
 
dexterstpierre profile image
Dexter St-Pierre

Although I agree that the original example wasn't the most readable, I don't think that your example would scale very well. If you moved the declaration of the object out of the function (possibly to a different file) and access the value the way that Artem did I think you would have a much more readable function. But that's just my opinion :)