Good Code
The good version names every current status and throws for unknown values. New enum values become review-visible because tests or runtime fail fast.
Lesson 08
Use pattern matching to make state-specific branches explicit and keep default cases from hiding unhandled states.
public static string LabelFor(ReviewStatus status)
{
return status switch
{
ReviewStatus.Draft => "Draft",
ReviewStatus.Published => "Published",
ReviewStatus.Archived => "Archived",
// Unknown enum values fail loudly during review and tests.
_ => throw new ArgumentOutOfRangeException(nameof(status), status, null)
};
}public static string LabelFor(ReviewStatus status)
{
switch (status)
{
case ReviewStatus.Published:
return "Published";
default:
// Default hides Draft, Archived, and future states.
return "Draft";
}
}The good version names every current status and throws for unknown values. New enum values become review-visible because tests or runtime fail fast.
The bad version sends every non-published status to Draft. Future statuses can ship with the wrong label and no compiler or test signal.