My question may seem dumb, but I couldn't find a proper answer for it after searching for a while.
Imagine we have an enum called MissionStateEnum as below(since I am a c# developer I'm writing my code in c#, but I guess my question is a general so I will appreciate any further helps)
enum MissionStateEnum{
Pending = 1,
Assigned = 2,
Started = 3,
Done = 4,
Canceled = 5
}
Now we would like to check if the state of a mission is pending, assigned or one of started, done or cancelled to decide something based on one of those 3 conditions.
The common and best approach I know and used so many times is to use a switch case:
public void CheckMissionState(MissionStateEnum status)
{
switch (status)
{
case MissionStateEnum.Pending:
{
//Do something based on Pending
break;
}
case MissionStateEnum.Assigned:
{
//Do something based on Assigned
break;
}
case MissionStateEnum.Started:
case MissionStateEnum.Done:
case MissionStateEnum.Canceled:
{
//Do something based on Started, Done or Canceled
break;
}
default:
throw new ArgumentOutOfRangeException(nameof(state), state, null);
}
}
My question is, how about if? our part of the logic is that if the case is higher than the MissionStateEnum.Assigned, can we hanlde this case with a simple 2 line if instead of 10 lines of switch case since the state is higher than MissionStateEnum.Assigned.
if(state > MissionStateEnum.Assigned)
{
//Do something based on Started, Done or Canceled
return;
}
Is it okay to use if instead of switch case?
You can use
if, but you should absolutely not rely on the integer value of the enum - especially make no assumptions about the order etc. That value is just an implementation detail and the coupling and possible bugs are not worth the "time saved" by not typing out all the switch cases.(tbh there is no time to be saved because your IDE should do that for you.)
Also your IDE can help you check if you have handled all cases, and if you do not want to handle a specific value, you could include it anyway with a comment why.