bool true or false 和负返回

发布于 2024-09-05 11:02:00 字数 40 浏览 3 评论 0原文

返回 bool(true 或 false)和返回负值之间有什么区别

what is the deference between returning a bool (true or false) and returning negative value

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

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

发布评论

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

评论(9

懒猫 2024-09-12 11:02:00

一个是二进制的(成功或失败),另一个具有可用于错误代码的离散值。我所做的通常是负值表示错误,0 表示成功,正值表示警告。

One is binary (either success or failure) The other has discrete values that you can use for error codes. What I do is typically negative values for errors, 0 for success and positive values for warnings.

○愚か者の日 2024-09-12 11:02:00

这取决于您使用返回值的目的。

It depends on what you are using the return value for.

行雁书 2024-09-12 11:02:00

布尔值通常用于描述流程的成功或失败。

某件事要么“成功”(返回true),要么“失败”(返回false)。

一些函数(就像socket())返回一个“套接字描述符”,它是一个正值。在这种情况下,报告出现问题的一种方法是返回超出有效范围的值,即负值。

然而,请记住这并不是绝对的事实。一位开发人员可能决定为错误返回正值,并为成功返回 0。 (就像程序退出状态一样)。

了解返回值含义的唯一方法是阅读文档

A boolean value is usually used when to describe the success or the failure of a process.

Something either "succeeded" (return true) or "failed" (return false).

Some functions (juste like socket()) return a "socket descriptor" which is a positive value. In this case, one way to report that something went wrong is to return an out-of-valid-range value, thus a negative value.

However, keep in mind this is not an absolute truth. One developer might decide to return a positive value for error, and to return 0 for a success. (Just like program exit statuses).

The only way to know what a return value means, is to read the documentation.

黯然#的苍凉 2024-09-12 11:02:00

有时您希望方法根据该方法是否成功执行某些代码来返回 true/false。

但是,您可能需要有关方法失败原因的更多详细信息,但您不能抛出异常(例如,该语言没有异常)。在这种情况下,您可以返回一个整数值作为一种错误代码。

Sometimes you want a method to return true/false based on whether the method executed some code successfully.

However, you might want more detail on why a method failed, but you can't throw an exception (e.g., the language doesn't have exceptions). In this case, you can return an integer value as a sort of error code.

2024-09-12 11:02:00

您返回的类型值可以有 2 个可能的输出。

另一方面,您返回的类型的值可以有更多可能的输出。

顺便说一下,在大多数编程语言中,0 被认为是 false,而其他所有内容都被认为是 true。我认为您认为在大多数语言中任何否定都会被视为错误。

您可能想要返回多个不同的负数,例如不同的错误代码。或者您可能只是只有通过/失败的标准。您可以根据您的情况使用布尔值或整数返回类型。

One you are returning a value of a type which can have 2 possible outputs.

The other you are returning a value of a type which can have many more possible outputs.

By the way, in most programming languages 0 is considered false and everything else is considered true. I get the idea that you think that any negative would be considered false in most languages.

You may want to return a number of different negative numbers for example for different error codes. Or you may simply have a pass/fail only criteria. And you could use a boolean or integer return type depending on which scenario you have.

风蛊 2024-09-12 11:02:00
  • 当您的
    方法仅返回是/否。
  • 当需要表示时使用数字
    除了只是之外还有别的东西
    成功完成。
  • Use boolean return value when your
    method returns only yes/no.
  • Use numbers when you need to indicate
    something else besides just
    successful completions.
不知在何时 2024-09-12 11:02:00

如果您需要布尔结果,请返回布尔值(卡车),如果您需要整数,请返回整数(苹果)。
例如,在 javascript 中你可以这样做

var ret = 0;
if (ret){//is ret true?
  alert('Never gets here');
}

if (!ret){//is ret false?
  alert('I will popup');
}

,但如果你确实需要测试布尔值,你必须这样做:

var ret = 0;
if (ret === true){//if it is true AND boolean
  alert('Never gets here');
}

var ret = false;
if (ret === false){//if it is false AND boolean
  alert('I will popup');
}

If you need a bool result, return a bool(the truck) if you need an integer, return an integer(the apple).
For instance, in javascript you can do

var ret = 0;
if (ret){//is ret true?
  alert('Never gets here');
}

if (!ret){//is ret false?
  alert('I will popup');
}

but if you really need to test for a boolean value you MUST do:

var ret = 0;
if (ret === true){//if it is true AND boolean
  alert('Never gets here');
}

var ret = false;
if (ret === false){//if it is false AND boolean
  alert('I will popup');
}
随波逐流 2024-09-12 11:02:00

如果您想要两个以上的返回码(真/假),那么您必须返回一个整数。在 C++ 中,有一个 BOOL 到 int 的 typedef,这意味着您可以使用 BOOL 作为返回类型,使用 TRUE 和 FALSE 作为返回值,如果您愿意,还可以使用 -1 表示错误。 BOOL、TRUE 和 FALSE 的 typedef 只是用来提高代码的可读性,但它只是返回一个 int。

If you want more than two return codes (true/false) then you will have to return an integer. In C++ there's a typedef of BOOL to int meaning that you can use BOOL as your return type, and TRUE and FALSE for return values, and -1 for an error if you'd like. The typedefs of BOOL, TRUE, and FALSE just serve to improve the readability of your code, but it's just returning an int.

長街聽風 2024-09-12 11:02:00

例如,您可能更喜欢使用“out”参数:

public bool MyFunction(object input, out int status) {...}

这样您就可以将 true 或 false 作为函数的成功/失败,并使用 staus 变量来表示有关所发生事件的任何其他信息。

you may prefer to use an "out" param, for example:

public bool MyFunction(object input, out int status) {...}

so you can have true or false as they success/fail of the function, and a staus variable for any other information about what happened set as well.

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