What's the right way to handle "One, Both, or None" logic?
- by Stephen
I have a logic situation that is best described as two "Teams" trying to win a task. The outcome of this task could be a single winner, a tie (draw), or no winner (stalemate).
Currently, I'm using a nested if/else statement like so:
// using PHP, but the concept seems language agnostic.
if ($team_a->win()) {
if ($team_b->win()) {
// this is a draw
} else {
// team_a is the winner
}
} else {
if ($team_b->win()) {
// team_b is the winner
} else {
// This is a stalemate, no winner.
}
}
This seems rather spaghetti-like and repetitive. Is there a more logical, DRY pattern I could use?