如何使函数的代码更具可重用性
How to make a function's code more reusable
我想让我的函数更可重用,但我不知道怎么做(并请求您的指导)。
假设我有类似的东西:
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;
}
我能否以某种方式制作另一种父函数,其中包含通用的通用代码并以某种方式包装可更改的代码?是的,我知道我可以将一些参数传递到函数中,并基于此用 if else
做一些事情,但在这种情况下我不喜欢这种方法。有什么想法吗?
更新:
尽我所能模仿我实际做的事情:
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;
}
唯一需要改变的部分是我的方式 call/receive someData
从根本上说,您可以对函数进行参数化,以便传入执行不同位的内容,或者您可以将第一位和第二位抽象为您重用的函数。
正在参数化:
function unified(operation) {
// code...
// lots of code...
const doSomething = operation(); // ***
// other code...
// lots of other code...
return something;
}
function One() {
return unified(() => magic.sayWhat());
}
function Two() {
return unified(() => magic.sayWho());
}
A 摘要:
function theFirstBit() {
// code...
// lots of code...
}
function theSecondBit() {
// other code...
// lots of other code...
}
function One() {
theFirstBit();
const doSomething = magic.sayWhat();
theSecondBit();
return something; // This might have come from one of the bits above
}
function Two() {
theFirstBit();
const doSomething = magic.sayWho();
theSecondBit();
return something; // This might have come from one of the bits above
}
重新编辑:
The only part that needed to change is the way I call/receive someData
这是参数化的用例:
function myLameAssFunction(someArg1, someArg2, getSomeData) {
// *** −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^
let gettingSomethingFromService1 = callService1(someArg1);
let gettingSomethingFromService2 = callService2(someArg1);
// code, code, code...
unextractableFunction(() => {
let somethingFromExternalLibrary = magicLibrary.do.magic();
const doSomething = async () => {
try {
const someData = await getSomeData();
// *** −−−−−−−−−−−−−−−−−−−−−−−−^
gettingSomethingFromService2(someData);
} catch(err) {
// whatever
}
}
});
// code, code, code...
return gettingSomethingFromService2;
}
我想让我的函数更可重用,但我不知道怎么做(并请求您的指导)。
假设我有类似的东西:
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;
}
我能否以某种方式制作另一种父函数,其中包含通用的通用代码并以某种方式包装可更改的代码?是的,我知道我可以将一些参数传递到函数中,并基于此用 if else
做一些事情,但在这种情况下我不喜欢这种方法。有什么想法吗?
更新:
尽我所能模仿我实际做的事情:
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;
}
唯一需要改变的部分是我的方式 call/receive someData
从根本上说,您可以对函数进行参数化,以便传入执行不同位的内容,或者您可以将第一位和第二位抽象为您重用的函数。
正在参数化:
function unified(operation) {
// code...
// lots of code...
const doSomething = operation(); // ***
// other code...
// lots of other code...
return something;
}
function One() {
return unified(() => magic.sayWhat());
}
function Two() {
return unified(() => magic.sayWho());
}
A 摘要:
function theFirstBit() {
// code...
// lots of code...
}
function theSecondBit() {
// other code...
// lots of other code...
}
function One() {
theFirstBit();
const doSomething = magic.sayWhat();
theSecondBit();
return something; // This might have come from one of the bits above
}
function Two() {
theFirstBit();
const doSomething = magic.sayWho();
theSecondBit();
return something; // This might have come from one of the bits above
}
重新编辑:
The only part that needed to change is the way I call/receive someData
这是参数化的用例:
function myLameAssFunction(someArg1, someArg2, getSomeData) {
// *** −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^
let gettingSomethingFromService1 = callService1(someArg1);
let gettingSomethingFromService2 = callService2(someArg1);
// code, code, code...
unextractableFunction(() => {
let somethingFromExternalLibrary = magicLibrary.do.magic();
const doSomething = async () => {
try {
const someData = await getSomeData();
// *** −−−−−−−−−−−−−−−−−−−−−−−−^
gettingSomethingFromService2(someData);
} catch(err) {
// whatever
}
}
});
// code, code, code...
return gettingSomethingFromService2;
}