#How'd you format this code for readability?

11 messages · Page 1 of 1 (latest)

dense summit
#

public enum Rank {
    
    BRONZE, SILVER, GOLD, PLATINUM, DIAMOND, MASTER, GRANDMASTER;
    
    public static Rank box(int rating) {
        
        if (rating < 1_500) return BRONZE;
        else if (rating < 2_000) return SILVER;
        else if (rating < 2_500) return GOLD;
        else if (rating < 3_000) return PLATINUM;
        else if (rating < 3_500) return DIAMOND;
        else if (rating < 4_000) return MASTER;
        else return GRANDMASTER;
    }
}
barren owlBOT
#

This post has been reserved for your question.

Hey @dense summit! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

olive marlin
#

like that

#

but i'd probably refactor it to have each of those have some kind of threshold field and just use that

#

so you could just have

public static Rank box(int rating) {
  for (Rank rank: values()) {
    if (rating < rank.threshold)
      return rank;
  }
  return GRANDMASTER;
}
```or some variation thereof
#

you could use that threshold for other stuff, of course

dense summit
#

i see, that's a good idea, thank you

#

public enum Rank {
    
    BRONZE(1_499),
    SILVER(1_999),
    GOLD(2_499),
    PLATINUM(2_999),
    DIAMOND(3_499),
    MASTER(3_999),
    GRANDMASTER(Elo.MAX_RATING);
    
    private final int threshold;
    
    Rank(int threshold) {
        
        this.threshold = threshold;
    }
    
    public int getThreshold() {
        
        return threshold;
    }
    
    public static Rank box(int rating) {
        
        for (Rank rank : values()) {
            
            if (rating < rank.getThreshold()) {
                
                return rank;
            }
        }
        
        return GRANDMASTER;
    }
}```

How does this look?
olive marlin