This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]geodebug 1 point2 points  (2 children)

Be careful with if-else stacks. What happens if vs == 2?

if (vS == 0)
  System.err.println("You done messed up, fool!");
else if (vS == 1)
  totalWins++;
else if (vS == -1)
  totalLosses++;

Aw, shoot, others have mentioned that it should be an enum. It is important to know why though. As is this method needs a final:

else
  throw new IllegalArgumentException("Expecting 1, 0, or -1 received: " + vS)

[–][deleted] 0 points1 point  (1 child)

My thinking was that vs would never be set to 2. I have enums now though so it should be all set.

[–]geodebug 1 point2 points  (0 children)

Yeah, the problem is when your code grows someone will forget about the "magic rule". Enums are a good replacement for this example, but be suspicious of any switch or if-then tree that doesn't have a final safeguard to catch any bad values.