是否有任何技术可以使用flag参数拆分方法?

sna*_*ile 8 java flags refactoring coding-style

我有一个带有flag参数的方法.我认为将布尔值传递给方法是一种不好的做法(使签名复杂化,违反了"每种方法做一件事"的原则).我认为将方法分为两种不同的方法更好.但如果我这样做,这两种方法将非常相似(代码重复).

我想知道是否有一些通用技术将带有flag参数的方法拆分为两个独立的方法.

这是我的方法(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;
}
Run Code Online (Sandbox Code Playgroud)

sys*_*out 5

如果您不喜欢签名上的布尔值,可以在没有它的情况下添加两种不同的方法,重构为private主要方法:

int calculateNumOfLiveNeighbors(Cell c, int gen) {
  return calculateNumOfLiveOrDeadNeighbors(c, gen, true);
}
int calculateNumOfDeadNeighbors(Cell c, int gen) {
  return calculateNumOfLiveOrDeadNeighbors(c, gen, false);
}
Run Code Online (Sandbox Code Playgroud)

要么

您可以将Result Classint数组编码为输出参数,以存储结果; 这会让你摆脱烦人的布尔参数.


Man*_*zzi 4

我想这取决于每个案例。

在我看来,在这个例子中你有两个选择。

假设您想要拆分通话calculateNumOfLiveOrDeadNeighbors()

成两半:

calculateNumOfLiveNeighbors() 
Run Code Online (Sandbox Code Playgroud)

calculateNumOfDeadNeighbors()
Run Code Online (Sandbox Code Playgroud)

您可以使用模板方法将循环移动到另一个方法。您可以使用它在两种方法中对死/活细胞进行计数。

private int countCells(Cell c, int gen, Filter filter)
{
    int x = c.getX();
    int y = c.getY();
    CellState state;
    int counter = 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 (filter.countMeIn(state))
            {
                counter++;
            }
        }
    }
    return counter;
 }

 private interface Filter
 {
      boolean countMeIn(State state);
 }

 public int calculateNumOfDeadNeighbors(Cell c, int gen)
 {
     return countCells(c, gen, new Filter()
                       { 
                           public boolean countMeIn(CellState  state)
                           {
                              return (state == CellState.DEAD || state == CellState.DEAD4GOOD);
                           }
                        });
  }

 public int calculateNumOfLiveNeighbors(Cell c, int gen)
 {
     return countCells(c, gen, new Filter()
                       { 
                           public boolean countMeIn(CellState  state)
                           {
                              return (state == CellState.LIVE || state == CellState.SICK);
                           }
                        });
  }
Run Code Online (Sandbox Code Playgroud)

这很麻烦,甚至可能不值得痛苦。或者,您可以使用monad来存储统计计算的结果,然后在 monad 上使用getDeadCounter()或,正如许多人已经建议的那样。getLiveCounter()