Skip to content

Instantly share code, notes, and snippets.

@flpms
Created November 9, 2019 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flpms/07f6a3e013a3cbfd70968cf3990c638f to your computer and use it in GitHub Desktop.
Save flpms/07f6a3e013a3cbfd70968cf3990c638f to your computer and use it in GitHub Desktop.
const saveOrderWrapper = (dependencies) => {
const { repository, config } = dependencies;
const { mongoDB } = repository;
const { orderTTLInDays } = config.app;
const orderTTL = (millesecondsInADay * orderTTLInDays);
const asyncCall = prepareCall(dependencies);
const order = async (payload) => {
const order = {
...payload,
};
await asyncCall();
}
};
const saveOrderWrapper = ({
repository,
cms,
getProperty,
config,
}) => {
const { mongoDB } = repository;
const { orderTTLInDays } = config.app;
const orderTTL = (millesecondsInADay * orderTTLInDays);
const asyncCall = prepareCall({
repository,
cms,
getProperty,
config,
});
const order = async (payload) => {
const order = {
...payload,
};
await asyncCall();
}
};
@ericelliott
Copy link

ericelliott commented Nov 9, 2019

Destructuring isn't always the right approach. For example, we could have taken this even further, but I think this does make more sense than either of the above:

const saveOrderWrapper = ({repository, config, ...deps}) => {
  const { mongoDB } = repository;
  const { orderTTLInDays } = config.app;
  const orderTTL = (millesecondsInADay * orderTTLInDays);

  const asyncCall = prepareCall({ repository, config, ...deps });

  const order =  async (order) => {
    // order wasn't used before, but let's
    // just assume asyncCall needs it.
    await asyncCall(order);
  }
};

IMO, this function knows too much about repository and config. There's a bunch of tight coupling here and lots of opportunity to improve things. Generally speaking, if you need to destructure a ton of things, that could be a code smell that your function is trying to do too much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment