Let's say we have a list of players in our Event
class. And we have a dictionary with the score of each player. We can add a score to a player using the addScore
method:
public class Event {
private List<Player> players;
private Map<Player, Integer> score;
public void addScore(Player p, int playerScore) { /* ... */ }
}
Now say we cannot allow the player to be either null or not to be contained of the player list defined in the event.
I just asked about null values and it seems the best approach would be to throw an exception. But trying to add a score to a player that doesn't belong to that event is another story:
public void addScore(Player p, int playerScore) {
if (p == null) throw new IllegalArgumentException("The player cannot be null");
if (!players.contains(p))
// ???
// the rest of the method
}
What should I do? What's the best approach? Should I throw a similar exception? Should I just do nothing? Or should I do something else?
Best Answer
The best idea to follow here is known as "fail fast". The basic philosophy goes as follows: