title: Are there any techniques to split a method with a flag argument?
body: <p>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).</p>
  
  <p>I wonder if there are some general techniques for splitting methods with a flag argument into two separate methods. </p>
  
  <p>Here's the code of my method (Java):</p>
  
  <pre><code>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 &lt;= x + 1; i++) {
        for (int j = y - 1; j &lt;= y + 1; j++) {
           if (i == x &amp;&amp; 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;
  }
  </code></pre>