There are two functions:

private void sendLeft() { leftSendersIndexes = newLeftSendersIndexes; Agent rightRecepient; int rightRecepientIdx = 0; Agent leftSender; for (int i = 0; i < leftSendersIndexes.size(); i++) { rightRecepientIdx = leftSendersIndexes.get(i) + 1; rightRecepient = list.get(rightRecepientIdx); leftSender = list.get(rightRecepientIdx - 1); rightRecepient.setNewLeftMsg(leftSender.getLeftMsg()); rightRecepient.setLeftMsg(0); // reset left messages } } private void sendRight() { rightSendersIndexes = newRightSendersIndexes; Agent leftRecepient; int leftRecepientIdx = 0; Agent rightSender; for (int i = 0; i < rightSendersIndexes.size(); i++) { leftRecepientIdx = rightSendersIndexes.get(i) - 1; leftRecepient = list.get(leftRecepientIdx); rightSender = list.get(leftRecepientIdx + 1); leftRecepient.setNewRightMsg(rightSender.getRightMsg()); } } 

They are very similar. But everything spoils that in the first function, first + 1, and then minus 1, and vice versa in the second function. In addition, at the end setNewLeftMsg() is called in the first case and setNewRightMsg() in the second. Only one option comes to mind: enter a boolean parameter. Is there no better way?

  • You have 2 fairly simple and consistent functions. By making one more complicated and confusing - will you simplify your life? - Kromster
  • @KromStern, perhaps not) I just heard that duplication under the penalty of the death penalty should not be allowed). And then duplication is obtained. - Alexander Elizarov
  • If it were only in + | - 1. It is not known what your agent is. Yes, and external. And their functions are called different. Tormenting conditions stick. - Sergey
  • one
    The last lines in both functions hint that architectural problems are above what is presented. - D-side

0