void processBalls(List balls) {
//Saved blue balls for the end of the list
List blueBalls = new ArrayList();
int count = 0;
//we only need a max of 12 balls here.
for (int i = balls.size() -1;
i > -1 && count < MAXIMUM_NUMBER_OF_BALLS; i--) {
Ball ball = (Ball)balls.get(i);
if (isBlueBall(ball)) {
//don't process small blue balls
if (!isSmallBall(ball)) {
blueBalls.add(ball);
}
} else {
processBall(ball);
count++;
}
}
//Put blue balls at the end if there is room
for (int i = 0; i < blueBalls.size() &&
count < MAXIMUM_NUMBER_OF_BALLS; i++) {
Ball ball = (Ball)blueBalls.get(i);
processBall(ball);
count++;
}
}
I was trying to express my frustration with this code to my pair, but my point wasn't getting across. So, I wrote the equivalent in Ruby:
def process_balls(balls)
balls = balls.reverse
balls = balls.select {|b| !is_blue(b)} +
balls.select {|b| is_blue(b) && !is_small(b)}
balls = balls.first(MAXIMUM_NUMBER_OF_BALLS)
balls.each {|b| process_ball(b)}
end