Using default in a switch statement when switching over an enum

后端 未结 16 1252
一个人的身影
一个人的身影 2020-12-15 02:57

What is your procedure when switching over an enum where every enumeration is covered by a case? Ideally you\'d like the code to be future proof, how do you do that?

<
16条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 03:50

    assert and then maybe throw.

    For in-house code thats in the same project as this (you didnt say what the function boundary is - internal lib, external lib, inside module,...) it will assert during dev. THis is what you want.

    If the code is for public consumption (other teams, for sale etc) then the assert will disappear and you are left with throw. THis is more polite for external consumers

    If the code is always internal then just assert

提交回复
热议问题