bool true or false and negative return [closed]
what is the deference between returning a bool (true or false) and returning negative value
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.
It depends on what you are using the return value for.
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.
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.
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.
- Use boolean return value when your method returns only yes/no.
- Use numbers when you need to indicate something else besides just successful completions.
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');
}
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.
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.
精彩评论