使用 enum 作为 int 是一个好习惯吗?

发布于 2024-10-16 14:58:42 字数 384 浏览 5 评论 0原文

所以,我在类中有一个变量“状态”。我想将其声明为整数,这样我就可以保存一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

触ぅ动初心 2024-10-23 14:58:42

是的,这是一个好方法。您通常使用枚举来让生活变得更轻松,许多不同的数字并不能真正告诉其他编码人员任何东西,这并不是很有帮助。

所以这是一种非常好的使用方式,它使您的代码具有可读性和可理解性。

尽管正如 @James McNellis 指出的那样,将枚举命名为“1,2,3,4”是一个坏主意,因为它并没有表达它真正的作用。

但我怀疑这只是你这边的一个例子。

请考虑这一点:

switch (operationState)
{
    case Waiting:
        dosomething();
        break;
    case Running:
        dosomething();
        break;
    case Ended:
        dosomething();
        break;
}

在这种情况下,“操作”是:等待、运行或结束,这使得它可读且易于理解。现在考虑没有枚举的方式:

switch (iState)
{
    case 997:
        dosomething();
        break;
    case 998:
        dosomething();
        break;
    case 999:
        dosomething();
        break;
}

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:

switch (operationState)
{
    case Waiting:
        dosomething();
        break;
    case Running:
        dosomething();
        break;
    case Ended:
        dosomething();
        break;
}

In this case, the "operation" is either: Waiting, Running or Ended, which makes it readable and understandable. Now consider the way without enums:

switch (iState)
{
    case 997:
        dosomething();
        break;
    case 998:
        dosomething();
        break;
    case 999:
        dosomething();
        break;
}

What does 997 tell you? Absolutely Nothing! Use readable and understandable code to make everyones life easier.

七月上 2024-10-23 14:58:42

如果您将 state 声明为 State 而不是 int,那么上面的所有代码都可以正常工作。您可以在 switch 语句中使用它,为它分配新值,与它进行比较等。这里使用 int 确实没有任何好处,因为这本质上是“位于源代码中” ”。您的变量不是整数。将其乘以或除以一个值,或者将其左移或右移是没有意义的。将变量标记为 State 可以更清楚地表明您确实持有多个值之一,并可以防止您犯上述一些错误。另外,它使编译器有更好的机会诊断如下问题:

state = 137; // Error!  Can't do this assignment without a cast.

一般来说,使用类型系统对您有利。如果它是 int,请将其设为 int。如果是枚举类型,则将其设为枚举类型。

All of the code you have above works just as well if you have state declared as a State rather than an int. 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 an int 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 a State 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:

state = 137; // Error!  Can't do this assignment without a cast.

In general, use the type system to your advantage. If it's an int, make it an int. If it's an enumerated type, make it an enumerated type.

肩上的翅膀 2024-10-23 14:58:42

在 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文