I use magic numbers in class member initialization:
private static final RangeMap<Integer, String> MY_MAP = new ImmutableRangeMap.Builder<Integer, String>()
.put(Range.closed(10, 31), "random string 1")
.put(Range.closed(32, 36), "random string 2")
.put(Range.closed(37, 39), "random string 3")
.put(Range.closed(40, 78), "random string 4")
// lot of similar lines here
.build();
The code is quite clear and easy to read but checkstyle gives us tons of warnings regarding Checkstyle. I can suppress the warning, but I am looking for better solution in Java.
One way is to use constanst and the code would look like this:
private static final Integer RANDOM_STRING_1_START = 10;
private static final Integer RANDOM_STRING_2_START = 32;
private static final Integer RANDOM_STRING_3_START = 37;
private static final Integer RANDOM_STRING_4_START = 40;
private static final Integer RANDOM_STRING_1_END = 31;
private static final Integer RANDOM_STRING_2_END = 36;
private static final Integer RANDOM_STRING_3_END = 39;
private static final Integer RANDOM_STRING_4_END = 78;
private static final RangeMap<Integer, String> MY_MAP = new ImmutableRangeMap.Builder<Integer, String>()
.put(Range.closed(RANDOM_STRING_1_START, RANDOM_STRING_1_END), "random string 1")
.put(Range.closed(RANDOM_STRING_2_START, RANDOM_STRING_2_END), "random string 2")
.put(Range.closed(RANDOM_STRING_3_START, RANDOM_STRING_3_END), "random string 3")
.put(Range.closed(RANDOM_STRING_4_START, RANDOM_STRING_4_END), "random string 4")
// lot of similar lines here
.build();
and I don't think it is as nice and readable as before. Moreover it's more prone to error while typing.
Any ideas how to define table of values without checkstyle complaining? I don't want to suppress the warning, I'd like to find the "Java way".