What
Comparing enums with <, >, =, != is not ideal, because adding new entries to the enum could break the original assumptions.
Originally suggested at #392 (comment)
Acceptance Criteria (DoD)
- No comparisons using
< or >
- Minimal comparisons using
= or !=
How
In most cases, a switch without a default is much more robust.
For example:
FileExistenceState convertFileExistenceState(fb::FileExistenceState fb_state)
{
switch (fb_state)
{
case fb::FileExistenceState::Deleted:
return FileExistenceState::Deleted;
case fb::FileExistenceState::Exists:
return FileExistenceState::Exists;
}
SCORE_LANGUAGE_FUTURECPP_UNREACHABLE_MESSAGE("Invalid fb::FileExistenceState");
}
Not having a default triggers a compiler warning when a new entry is added to the enum, prompting the developer to consider the desired behaviour and add a new case.
Because it is possible (using casts) to create a value which does not correspond to any enum entry, SCORE_LANGUAGE_FUTURECPP_UNREACHABLE is needed to satisfy the compiler that the function will return in all cases. This will crash the program if an invalid value is seen.
What
Comparing enums with
<,>,=,!=is not ideal, because adding new entries to the enum could break the original assumptions.Originally suggested at #392 (comment)
Acceptance Criteria (DoD)
<or>=or!=How
In most cases, a
switchwithout adefaultis much more robust.For example:
Not having a
defaulttriggers a compiler warning when a new entry is added to the enum, prompting the developer to consider the desired behaviour and add a newcase.Because it is possible (using casts) to create a value which does not correspond to any enum entry,
SCORE_LANGUAGE_FUTURECPP_UNREACHABLEis needed to satisfy the compiler that the function will return in all cases. This will crash the program if an invalid value is seen.