2
\$\begingroup\$

I have 16 methods in Java that return int values. I need to find the one that returns the highest value.

I initially implemented this:

private int best() {
    int max = playGame(ONES);
    max = Math.max(max, playGame(TWOS));
    max = Math.max(max, playGame(THREES));
    max = Math.max(max, playGame(FOURS));
    max = Math.max(max, playGame(FIVES));
    max = Math.max(max, playGame(SIXES));
    max = Math.max(max, playGame(SEVENS));
    max = Math.max(max, playGame(EIGHTS));
    max = Math.max(max, playGame(THREE_OF_A_KIND));
    max = Math.max(max, playGame(FOUR_OF_A_KIND));
    max = Math.max(max, playGame(FULL_HOUSE));
    max = Math.max(max, playGame(SMALL_STRAIGHT));
    max = Math.max(max, playGame(LARGE_STRAIGHT));
    max = Math.max(max, playGame(ALL_DIFFERENT));
    max = Math.max(max, playGame(CHANCE));
    max = Math.max(max, playGame(ALL_SAME));
    return max;
}

To make this slightly better, I could reduce one comparison by comparing two methods simultaneously.

private int best() {
    int max1 = Math.max(playGame(ONES), playGame(TWOS));
    int max2 = Math.max(playGame(THREES), playGame(FOURS));
    int max3 = Math.max(playGame(FIVES), playGame(SIXES));
    int max4 = Math.max(playGame(SEVENS), playGame(EIGHTS));
    int max5 = Math.max(playGame(THREE_OF_A_KIND), playGame(FOUR_OF_A_KIND));
    int max6 = Math.max(playGame(FULL_HOUSE), playGame(SMALL_STRAIGHT));
    int max7 = Math.max(playGame(LARGE_STRAIGHT), playGame(ALL_DIFFERENT));
    int max8 = Math.max(playGame(CHANCE), playGame(ALL_SAME));
    int max11 = Math.max(max1, max2);
    int max12 = Math.max(max3, max4);
    int max13 = Math.max(max5, max6);
    int max14 = Math.max(max7, max8);
    int max21 = Math.max(max11, max12);
    int max22 = Math.max(max13, max14);
    int max = Math.max(max21, max22);
    return max;
}

I mentioned 16 methods as playGame(CONSTANT) callbacks different methods based on the constant. However, this code is still bad. What could be a better way to achieve this?

\$\endgroup\$
6
  • 1
    \$\begingroup\$ How do you declare those constants, using an enum? \$\endgroup\$ Jul 24, 2017 at 20:56
  • \$\begingroup\$ No, these are all different constants, public static final values in a separate file. \$\endgroup\$ Jul 24, 2017 at 20:57
  • \$\begingroup\$ I think you should consider to collect them into an enum if it can be done. Regardless, I think that the most elegant solution would use Streams. \$\endgroup\$ Jul 24, 2017 at 21:07
  • 1
    \$\begingroup\$ This code looks bizarre, and the question needs more context. What does playGame() do? \$\endgroup\$ Jul 24, 2017 at 21:14
  • \$\begingroup\$ EIGHTS ? No nines, tens etc? What kind of poker is this? Some of us wrote a poker hand evaluator a few years ago, you might be interested in reading one. \$\endgroup\$ Jul 24, 2017 at 21:29

1 Answer 1

6
\$\begingroup\$

I think that it would be best if you could put those constants into a enum so that you can use Enum.values to get them all in an array. If so you can do this:

enum Play {
 TWOS, THREES, FOURS, FIVES, SIXES, SEVENS, EIGHTS, THREE_OF_A_KIND, 
 FOUR_OF_A_KIND, FULL_HOUSE, SMALL_STRAIGHT, LARGE_STRAIGHT, ALL_DIFFERENT,
 CHANCE, ALL_SAME; 
}

class Main {

  int playGame(Play play) { 
     // you need to implement this method returning something meaningful.
     return 0; 
  }

  int best() {
     return Stream.of(Play.values())
                  .mapToInt(this::playGame)
                  .max().getAsInt();
  }
}

If you want to keep those constants as independent static final (Strings?) then you will need to create a list of them explicitly:

//...
static final String TWOS = "TWOS";
static final String THREES = "THREES";
//...
static final List<String> PLAYS = Arrays.asList(TWOS, THREES, ...);
//...
int best() {
   return PLAYS.stream()
               .mapToInt(this::playGame)
               .max().getAsInt();
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.