Advantage of switch over if-else statement

后端 未结 22 2122
梦谈多话
梦谈多话 2020-11-22 11:08

What\'s the best practice for using a switch statement vs using an if statement for 30 unsigned enumerations where about 10 have an ex

22条回答
  •  清酒与你
    2020-11-22 12:06

    I agree with the compacity of the switch solution but IMO you're hijacking the switch here.
    The purpose of the switch is to have different handling depending on the value.
    If you had to explain your algo in pseudo-code, you'd use an if because, semantically, that's what it is: if whatever_error do this...
    So unless you intend someday to change your code to have specific code for each error, I would use if.

提交回复
热议问题