使用 enum 作为 int 是一个好习惯吗?
所以,我在类中有一个变量“状态”。我想将其声明为整数,这样我就可以保存一些 if 语句。
int state;
一种方法是声明一个枚举 State {One = 0, Two = 1, Three = 3},然后在 switch 语句中,它会变成:
switch (state)
{
case One:
dosomething();
break;
case Two:
dosomething();
break;
case Three:
dosomething();
break;
}
那么,像这样使用 enum 是一个好习惯吗? 有更好的方法吗?
谢谢!
So, I have a variable "state" in a class. I want to declare it as an integer so I can save some if statements.
int state;
One way to do this is to declare an enum State {One = 0, Two = 1, Three = 3}, and then in the switch statement, it would become:
switch (state)
{
case One:
dosomething();
break;
case Two:
dosomething();
break;
case Three:
dosomething();
break;
}
So, is it a good practice to use enum like this?
Is there a better way to do this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这是一个好方法。您通常使用
枚举
来让生活变得更轻松,许多不同的数字并不能真正告诉其他编码人员任何东西,这并不是很有帮助。所以这是一种非常好的使用方式,它使您的代码具有可读性和可理解性。
尽管正如 @James McNellis 指出的那样,将枚举命名为“1,2,3,4”是一个坏主意,因为它并没有表达它真正的作用。
但我怀疑这只是你这边的一个例子。
请考虑这一点:
在这种情况下,“操作”是:等待、运行或结束,这使得它可读且易于理解。现在考虑没有枚举的方式:
997 告诉你什么?绝对什么都没有!使用可读且易于理解的代码让每个人的生活更轻松。
Yes that is a good way to do it. You generally use
enums
to make life easier, a lot of different numbers that don't really tell other coders anything isn't quite helpful.So this is a perfectly good way of using it, it makes your code readable and understandable.
Altough as @James McNellis pointed out, naming your enums like "1,2,3,4" is a bad idea, since it doesn't express what it really does.
But I suspect that was only an example from your side.
Consider this instead:
In this case, the "operation" is either: Waiting, Running or Ended, which makes it readable and understandable. Now consider the way without enums:
What does 997 tell you? Absolutely Nothing! Use readable and understandable code to make everyones life easier.
如果您将
state
声明为State
而不是int
,那么上面的所有代码都可以正常工作。您可以在 switch 语句中使用它,为它分配新值,与它进行比较等。这里使用int
确实没有任何好处,因为这本质上是“位于源代码中” ”。您的变量不是整数。将其乘以或除以一个值,或者将其左移或右移是没有意义的。将变量标记为State
可以更清楚地表明您确实持有多个值之一,并可以防止您犯上述一些错误。另外,它使编译器有更好的机会诊断如下问题:一般来说,使用类型系统对您有利。如果它是
int
,请将其设为int
。如果是枚举类型,则将其设为枚举类型。All of the code you have above works just as well if you have
state
declared as aState
rather than anint
. You can use it in a switch statement, assign it new values, do comparisons with it, etc. There really aren't any benefits to using anint
here, since that's essentially "lying in the source code." Your variable isn't an integer. It doesn't make sense to multiply or divide it by a value, or to bit shift it left or right. Marking the variable as aState
makes it clearer that you're really holding one of multiple values and prevents you from making some of the above mistakes. Plus, it gives the compiler a better chance to diagnose things like this:In general, use the type system to your advantage. If it's an
int
, make it anint
. If it's an enumerated type, make it an enumerated type.在 switch 语句中使用可读的状态名称而不是数字,对我来说似乎很好。
而且我认为这也不会影响性能。
所以没有理由不使用枚举!
Using readable state names in a switch statement instead of numbers, seems good to me.
And I do not think it will affect the performance as well.
So no reason not to use enums!