是否有任何技术可以使用标志参数拆分方法?
我有一个带有标志参数的方法。我认为将布尔值传递给方法是一种不好的做法(使签名复杂化,违反了“每个方法只做一件事”原则)。我认为将方法分成两种不同的方法更好。但如果我这样做,这两种方法将非常相似(代码重复)。
我想知道是否有一些通用技术可以将带有标志参数的方法拆分为两个单独的方法。
这是我的方法的代码(Java):
int calculateNumOfLiveOrDeadNeighbors(Cell c, int gen, boolean countLiveOnes) {
int x = c.getX();
int y = c.getY();
CellState state;
int aliveCounter = 0;
int deadCounter = 0;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i == x && j == y)
continue;
state = getCell(i, j).getCellState(gen);
if (state == CellState.LIVE || state == CellState.SICK){
aliveCounter++;
}
if(state == CellState.DEAD || state == CellState.DEAD4GOOD){
deadCounter++;
}
}
}
if(countLiveOnes){
return aliveCounter;
}
return deadCounter;
}
I have a method with a flag argument. I think that passing a boolean to a method is a bad practice (complicates the signature, violates the "each method does one thing" principle). I think splitting the method into two different methods is better. But if I do that, the two methods would be very similar (code duplication).
I wonder if there are some general techniques for splitting methods with a flag argument into two separate methods.
Here's the code of my method (Java):
int calculateNumOfLiveOrDeadNeighbors(Cell c, int gen, boolean countLiveOnes) {
int x = c.getX();
int y = c.getY();
CellState state;
int aliveCounter = 0;
int deadCounter = 0;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i == x && j == y)
continue;
state = getCell(i, j).getCellState(gen);
if (state == CellState.LIVE || state == CellState.SICK){
aliveCounter++;
}
if(state == CellState.DEAD || state == CellState.DEAD4GOOD){
deadCounter++;
}
}
}
if(countLiveOnes){
return aliveCounter;
}
return deadCounter;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您不喜欢签名上的布尔值,您可以添加两种不带它的不同方法,将主要方法重构为
private
:OR
您可以编写一个 结果类或int数组作为输出参数,用于存储两个结果;这将使您摆脱烦人的布尔参数。
If you don't like the boolean on your signature, you could add two different methods without it, refactoring to
private
the main one:OR
you could code a Result Class or int array as output parameter for storing both the results; this would let you get rid of the annoying boolean parameter.
我想这取决于每个案例。
在我看来,在这个例子中你有两个选择。
假设您想将调用
calculateNumOfLiveOrDeadNeighbors()
分成两部分:
并且
您可以使用 Template将循环移动到另一个方法的方法。
您可以使用它在两种方法中对死/活细胞进行计数。
这很麻烦,甚至可能不值得痛苦。或者,您可以使用 monad 来存储统计计算的结果,并然后在 monad 上使用 getDeadCounter() 或 getLiveCounter() ,正如许多人已经建议的那样。
I guess it depends on every single case.
In this example you have two choices, in my opinion.
Say you want to split the call
calculateNumOfLiveOrDeadNeighbors()
in two:
and
You can use Template Method to move the loop to another method.
You can use it to count dead / alive cells in the two methods.
It's cumbersome, maybe not even worth the pain. You can, alternatively, use a monad to store the results of your statistics calculation and then use
getDeadCounter()
orgetLiveCounter()
on the monad, as many suggested already.在上面的例子中,我认为第二个和第三个选项更适用。
In the example above I think the 2nd and 3rd options are more applicable.
似乎最语义清晰的方法是返回一个包含两个值的结果对象,并让调用代码从结果对象中提取它关心的内容。
Seems like the most semantically clean approach would be to return a result object that contains both values, and let the calling code extract what it cares about from the result object.
就像Bozho所说:但是,但是以另一种方式结合第2点和第3点:
创建一个(可能的私有方法),它返回(活着和死了)并且(仅当您在大多数情况下需要分开死或活时)然后添加两个从结果中挑选出死亡或两者的方法:
Like Bozho said: But but combine point 2 and 3 in the other way arround:
Create a (possible private method) that returns both (living and dead) and (only if you need dead or alive seperate in the most cases) then add two methods that pick dead or both out of the result:
IMO,这种所谓的“每种方法只做一件事”原则需要有选择地应用。你的例子是一个最好不要应用它的例子。相反,我只是稍微简化一下方法实现:
IMO, this so-called "each method does one thing" principle needs to be applied selectively. Your example is one where, it is probably better NOT to apply it. Rather, I'd just simplify the method implementation a bit:
在使用重构方面,您可以做的一些事情是:
In terms of using refactoring, some things you can do are;
我倾向于保留 CellState 枚举中的映射进行计数,然后根据需要添加 LIVE 和 SICK 或 DEAD 和 DEAD4GOOD。
I would be inclined here to keep a map from the CellState enum to count, then add the LIVE and the SICK or the DEAD and the DEAD4GOOD as needed.
有一个私有方法,它是您当前拥有的方法的精确复制和粘贴。
然后创建两个新方法,每个方法都有一个更具描述性的名称,只需使用适当的布尔值调用您的私有方法
have a private method which is an exact copy and paste of what you currently have.
Then create two new methods, each with a more descriptive name that simply call your private method with appropriate boolean