C#:枚举的默认值应该是 None 还是 Unknown?

发布于 2024-09-08 01:39:55 字数 552 浏览 4 评论 0原文

假设您有一个表示错误代码的枚举。将会有多个代码,每个代码都有自己的底层 int 值;然而,获得默认 0 值的枚举值似乎应该仔细考虑。

在错误代码枚举的情况下,我可以想到两个特殊值:None(对于没有错误的情况)和Unknown(对于没有现有错误代码合适的情况,或者甚至当错误状态不能时)被检测到)。

其中一个值似乎应该为 0,而另一个值可能会为 -1 等其他值。将 None 值设置为 0 更合适,还是将 Unknown 值设置为 0 更合适?

public enum ErrorCode
{
    None = -1,
    Unknown = 0,
    InsufficientPermissions,
    ConnectivityError,
    ...
}

public enum ErrorCode
{
    Unknown = -1,
    None = 0,
    InsufficientPermissions,
    ConnectivityError,
    ...
}

我的直觉告诉我默认值应该是“未知”,但我很好奇是否有人做了不同的事情。

Say you have an enum that represents an error code. There will be several codes, each with their own underlying int value; however, the enum value that gets the default 0 value seems like it should be carefully considered.

In the case of an error code enum, there are two special values that I can think of: None (for cases when there is no error) and Unknown (for cases when no existing error code is appropriate, or perhaps even when error state cannot be detected).

One of these values seems like it should get 0, where the other will probably get something else like -1. Is it more appropriate to set the None value to 0, or the Unknown value to 0?

public enum ErrorCode
{
    None = -1,
    Unknown = 0,
    InsufficientPermissions,
    ConnectivityError,
    ...
}

public enum ErrorCode
{
    Unknown = -1,
    None = 0,
    InsufficientPermissions,
    ConnectivityError,
    ...
}

My instinct tells me that the default should be Unknown, but I'm curious if anyone has done it differently.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

征棹 2024-09-15 01:39:55

由于您使用最佳实践标记您的问题:当您可以使用异常时,请勿使用错误代码。

Since you're tagging your question with best-practices: don't use error codes when you can use exceptions.

攒眉千度 2024-09-15 01:39:55

在我看来,UnknownNoneErrorCode 枚举的上下文中含义相同。我的理由是,如果我检查错误代码,那是因为我已经有错误了。

我还认为,错误代码枚举仅在自定义异常或作为现有异常类型的自定义数据中有用,并且在这两种情况下,您总是会遇到错误。

In my opinion both Unknown or None mean the same thing in the context of the ErrorCode enumeration. My reasoning is that if I'm checking an error code than it's because I already have an error.

I'm also of the opinion that an error code enumeration is only useful in a custom exception or as custom data of an existing exception type and in both scenarios you will always have an error.

短暂陪伴 2024-09-15 01:39:55

绝对不是一个好的做法。但如果没有其他方法...那么我通常会采用第二个选项:

public enum ErrorCode 
{ 
    Unknown = -1, 
    None = 0, 
    InsufficientPermissions, 
    ConnectivityError, 
    ... 
} 

0 符合“无错误”约定,而 -1 符合理解存在一些错误(可能是未知的)。

Definitely not a good practice. But if there is no other way...then I would generally go by the second option:

public enum ErrorCode 
{ 
    Unknown = -1, 
    None = 0, 
    InsufficientPermissions, 
    ConnectivityError, 
    ... 
} 

0 goes well by the convention 'No Error' and -1 goes fine with the understanding that there is some error (which may be unknown).

诗化ㄋ丶相逢 2024-09-15 01:39:55

我想在这一点上我会不同意其他人的观点。

当以“此功能的当前状态是什么”的意义使用时,错误代码没有任何问题。

举个例子:如果您有一个可选的网络临时文件夹,您想要访问该文件夹,并且您想要显示最近一次尝试访问它的结果,您可以将其保存在错误代码中。也许您想在状态栏上向用户显示当前状态。

对我来说,我会使用 None 作为默认值 0。我认为没有理由将“未知”定为负面。未知是一种完美的错误状态。您可以将其放在列表的末尾。在实践中,我已经这样做了

public enum ErrorCode
{
    None = 0,
    InsufficientPermissions,
    ConnectivityError,
    ...
    Unknown,
}

。为了回答你的问题,我说没有必须是默认值。 None 表示:不存在您意识到的错误。未知对我来说意味着:存在一个非常模糊的错误,以至于您无法在代码中解释它。

I guess I'll disagree with everyone else on this.

There's nothing wrong with error codes when used in the sense of "what is the current state of this feature."

To make up an example: if you have an optional networked temp folder, that you want to access, and you want to display the results from the most recent time that you attempted to access it, you would save that in an error code. Perhaps on a status bar you want to show the current state to the user.

For me, I would use None as 0, as the default. I see no reason to make Unknown be negative. Unknown is a perfectly fine error state. You can just put it at the end of the list. In practice, I have done

public enum ErrorCode
{
    None = 0,
    InsufficientPermissions,
    ConnectivityError,
    ...
    Unknown,
}

And to answer your question, I say None must be the default. None means: no error exists that you are aware of. Unknown would mean to me: an error exists that is so obscure that you can't account for it in your code.

梦罢 2024-09-15 01:39:55

当没有错误时为什么要返回 ErrorCode 值?

没有多大意义。删除它可以解决您的问题。您可以将 0 表示未知:

public enum ErrorCode
{
    Unkown = 0,
    InsufficientPermissions,
    ConnectivityError
}

更新

您的评论有点可怕。您永远不应该有返回 ErrorCode 类型的方法。这是非常糟糕的做法。由于错误代码只应在异常情况下返回,因此抛出异常是个好主意。

如果需要,您可以在自定义异常中包含一个包含 ErrorCode 值的字段。

Why return an ErrorCode value at all when there is no error?

Doesn't make a whole lot of sense. Removing that would resolve your issue. You could just make 0 be for Unkown:

public enum ErrorCode
{
    Unkown = 0,
    InsufficientPermissions,
    ConnectivityError
}

UPDATE

Your comment is a little scary. You should never have a method that returns a type of ErrorCode. It's very poor practice. Since ErrorCodes should only be returned in Exceptional circumstances, it's a good idea to throw Exceptions.

If you want, you can have a field in your custom Exception that contains the ErrorCode value.

世界和平 2024-09-15 01:39:55

首先,您不应该有 None 错误代码。相反,称之为“成功”。

现在考虑一下如何检查错误代码。大多数人期望这样的事情:

if (errorCode != Success)

或者他们使用速记

if (errorCode != 0)

所以你就明白了。您的成功代码为 0,您没有“无”代码,并且“未知”可以是您想要的任何内容。

First of all, you shouldn't have a None error code. Instead, call it "Success".

Now think about how you are going to check the error code. Most people expect something like this:

if (errorCode != Success)

or they use the shorthand

if (errorCode != 0)

So there you have it. Your Success code is 0, you don't have a None code, and Unknown can be anything you want.

∝单色的世界 2024-09-15 01:39:55

我实际上不同意任何人说你应该只使用异常。当使用任何类型的使用数据库的应用程序时,您可能希望在系统中存储数据的状态。无论它们是错误代码还是某种状态,它们最好在数据库中表示为整数。

使用和处理异常是一项非常重要的技能,应该使用,但我相信将错误代码与这些异常一起使用通常是一个好主意。它不需要太多额外的资源,并且适合在数据库连接的应用程序中使用。

我实际上建议按照建议将“None”更改为“Success”,但将“Success”的值设置为 1,并让所有其他错误从该值开始递增。其背后的原因是因为作为大多数数据库应用程序中的标准,状态列不可为空,并且通常作为整数进行跟踪。如果您使用成功状态 0,这可能会导致问题,因为默认情况下,如果用户没有显式插入其他内容,不可为空的整数列将设置为 0。这将导致错误的状态/错误代码,并在您的应用程序的未来造成问题。不仅整型变量在 C# 中默认自动初始化为 0,导致它自动成功,如果您不设置初始值,枚举也会自动成功,而这不是您想要的。

另外,从编码的角度来看,1 也可以表示 true,因此它会成功。但在某些情况下,情况可能并非如此。在基于 Unix 的系统中,返回 0 意味着成功/没有错误,而在其他语言(例如 c++)中,主函数返回 0 是标准的,也表示主函数成功执行。

I would actually have to disagree with anyone that says you should only use exceptions. When using any kind of application that uses a database you may want to store statuses for the data in the system. Whether they are error codes or some kind of status they are best represented in the database as integers.

Using and handling exceptions is a very important skill and should be used, but I believe that having an error code along with those exceptions is generally a good idea. It doesn't take too many additional resources and lends itself to be used in a database connected application.

I would actually suggest that change "None" to "Success" as suggested, but that you make "Success" a value of 1, and have all other errors increment from there. The reason behind this is because as a standard in most database applications the status columns are not nullable, and are generally tracked as integers. This can lead to issues if you use a success status of 0 because by default non nullable integer columns will be set to 0 if a user doesn't explicitly insert something else. This would lead to a false status / error code and cause problems in the future of your application. Not only that integer variables are automatically initialized to 0 by default in C# causing it to be an automatic success, so are enumerations if you don't set the initial value, and that is not something you want.

Also from a coding standpoint a 1 can also mean true, and so it goes well with success. There are situations where this may not be the case though. In Unix based systems a return of 0 means success/ no errors, and in other languages like c++ a 0 return is standard from the main function also indicating a successful execution of the main function.

无名指的心愿 2024-09-15 01:39:55

错误代码错误。例外很好。但要回答所提出的问题:

如果您的枚举具有“未知”值,则它应该是默认值。

ErrorCodes bad. Exceptions good. But to answer the question as asked:

If your enum has an "Unknown" value, it should be the default.

滥情稳全场 2024-09-15 01:39:55

所有“我不会这样做”的回答也是如此,但如果你坚持的话,这是我的 0.02 美元。

ErrorCodes.None 没有任何意义,因为没有错误。 ErrorCodes.Unknown 没有帮助。尝试返回可为 null 的错误代码:

public ErrorCode? DoFoo()

现在您可以检查 null

var error = DoFoo();
if (error != null)
    // react

仍然很糟糕,但至少它允许您在没有错误的情况下返回错误代码。

Ditto to all of the "I wouldn't do it responses," but if you insist, here's my $0.02.

ErrorCodes.None makes no sense, since there's no error. ErrorCodes.Unknown is not helpful. Try returning a nullable error code:

public ErrorCode? DoFoo()

Now you can check for null

var error = DoFoo();
if (error != null)
    // react

Still bad, but at least it allows you to not return an error code if there's no error.

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