eclipse-score / eclipse-score/lifecycle

Better handling of enum comparisons

Open
#401 0 comments 0 reactions 0 assignees View on GitHub
cleanup good first issue
Dominant language
C++
Stars
6
Forks
34
Avg merge
2d 3h
Merged PRs (30d)
80

Description

### What

Comparing enums with `<`, `>`, `=`, `!=` is not ideal, because adding new entries to the enum could break the original assumptions.

*Originally suggested at https://github.com/eclipse-score/lifecycle/pull/392#discussion_r3691420273*

### 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:

```cpp
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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.