How to make a function's code more reusable

I want to make my functions more reusable, but I don’t know how(and ask for your guidance).

Say I have something like that:

function One() {
  // code...
  // lot's of code...
  const doSomething = magic.sayWhat(); // or anything else
  // other code...
  // lot's of other code...
  return something;
}

function Two() {
  // code...
  // lot's of code...
  const doSomething = magic.sayWho(); // Here is a change. This change can be a bit more than that, but it's a fraction compared to all the rest of the code in function. It's the same as in function `One` except this piece of code.
  // other code...
  // lot's of other code...
  return something;
}

Can I make maybe somehow another, sort of a parent function that will include the general, common code and somehow wrap the changeable code? And yes, I know I can pass some params into the function and based on that make something with the if else, but I don’t like this approach in this case. Any ideas?

Update:

Did my best to provide a mimic of what I’m actually doing:

function myLameAssFunction(someArg1, someArg2) {
  let gettingSomethingFromService1 = callService1(someArg1);
  let gettingSomethingFromService2 = callService2(someArg1);

  // code, code, code...

  unextractableFunction(() => {
    let somethingFromExternalLibrary = magicLibrary.do.magic();
  
    const doSomething = async () => {
      try {
        const someData = await magicLibrary.sayWhat(someArg2, gettingSomethingFromService1, somethingFromExternalLibrary);
        gettingSomethingFromService2(someData);
      } catch(err) {
        // whatever
      }
    }
  });

  // code, code, code...
  
  return gettingSomethingFromService2;
}

The only part that needed to change is the way I call/receive someData

148 thoughts on “How to make a function's code more reusable”

  1. Pingback: buy viagra online
  2. Pingback: viagra otc
  3. Pingback: canada viagra
  4. Pingback: viagra online usa
  5. Pingback: order viagra
  6. Pingback: cialis 10 mg
  7. Pingback: mail order cialis
  8. Pingback: cialis capsule
  9. Pingback: viagra pills
  10. Pingback: viagra cheap
  11. Pingback: buy cialis cheap
  12. Pingback: overnight cialis
  13. Pingback: liquid cialis
  14. Pingback: sildenafil citrate
  15. Pingback: viagra sample
  16. Pingback: viagra for women
  17. Pingback: generic viagra
  18. Pingback: sildenafil viagra
  19. Pingback: herb viagra
  20. Pingback: natural viagra
  21. Pingback: what is viagra
  22. Pingback: cialis vs viagra
  23. Pingback: herbal viagra
  24. Pingback: viagra cost
  25. Pingback: goodrx sildenafil
  26. Pingback: viagra alternative
  27. Pingback: walmart viagra
  28. Pingback: viagra tablet
  29. Pingback: cialis savings

Leave a Comment