I have a class with a "mode" state. There are currently two modes and we are not likely to add more in the future.
The class has a CurrentMode
property, and a function to swap to the other mode.
I have implemented this using another property called ModeNotInUse
which returns the opposite value of CurrentMode
.
However, I am in two minds about how to implement this ModeNotInUse
public enum PlayerMode
{
Puzzle,
Action
}
public class ModeController
{
public PlayerMode CurrentMode { get; private set; }
private PlayerMode ModeNotInUse => ...
public void SwitchMode()
{
CurrentMode = ModeNotInUse
}
}
My two implimentations of this are:
private PlayerMode ModeNotInUse
=> CurrentMode switch
{
PlayerMode.Puzzle => PlayerMode.Action,
_ => PlayerMode.Puzzle
};
Or
private PlayerMode ModeNotInUse
=> CurrentMode == PlayerMode.Puzzle
? PlayerMode.Action
: PlayerMode.Puzzle;
Which better captures my intent here? The first is my preference but I worry it's difficult to read.
(edit) I could of course do this with a backing boolean field but that feels even less clean
private bool inPuzzleMode;
public PlayerMode CurrentMode
{
get => inPuzzleMode switch
{
true => PlayerMode.Puzzle,
false => PlayerMode.Action
};
private set => inPuzzleMode = value == PlayerMode.Puzzle;
}
private PlayerMode ModeNotInUse
=> inPuzzleMode switch
{
true => PlayerMode.Action,
false => PlayerMode.Puzzle
};
Which do you think is best?
ModeInUse
, so that I don't end up with! ModeNotInUse
. \$\endgroup\$