Programming against an enum in a switch statement, is this your way to do?

前端 未结 12 2278
青春惊慌失措
青春惊慌失措 2020-12-24 09:15

Look at the code snippet:

This is what I normally do when coding against an enum. I have a default escape with an InvalidOperationException (I do not use ArgumentExc

12条回答
  •  伪装坚强ぢ
    2020-12-24 10:14

    Clear, obvious and the right way to go. If DrivingState needs to change you may need to refactor.

    The problem with all the complicated polymorphic horrors above is they force the encapsulation into a class or demand additional classes - it's fine when there's just a DrivingState.Drive() method but the whole thing breaks as soon as you have a DrivingState.Serialize() method that serializes to somewhere dependent on DrivingState, or any other real-world condition.

    enums and switches are made for each other.

提交回复
热议问题