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
Fundamentally, you can parameterize the function so you pass in something that does the bit that differs, or you can abstract the first and second bits into functions that you reuse.
Parameterizing:
Aabstracting:
Re your edit:
That’s a use case for parameterizing: