I prefer to do it like this:
public void addCard(final Card card)
{
if(card == null)
{
throw new IllegalArgumentException("card must not be null");
}
cardList.add(card);
}
- less code to read
- it separates the error condition from the expected condition (no else = no indent)
- No one should see what is on line X to see that the map variable was zero = less time for people who are looking for what they did wrong.
- No need to clutter up the code with useless throw instructions - you should javadoc though with the @throws clause
Other than that, you are doing it right, in my opinion.
source
share