Is it nice to mix bool and ret codes

I have several programs that make heavy use of libraries with enumerations of error codes.

Type where 0 (the first value of the enumeration) is successful, and 1 is an error. In some cases, I have my own helper functions that return a bool indicating an error; in other cases, I bubble up an error listing. Unfortunately, sometimes I am mistaken for one another, and something fails.

What would you recommend? Are there some missing gcc warnings that would warn in these cases?

PS It seems strange to me to return an error code that is completely unrelated to my code, although, I think, I could return -1 or some other invalid value.

+4
source share
5 answers

It is a bad idea? No, you should do what makes sense, and not follow some abstract rule (the likes of which almost never serve all the situations that you will encounter in any case).

One way to avoid trouble is to make sure that the entire logical return function is read as correct English, for example, isEmpty() , userFlaggedExit() or hasContent() . This is different from my usual noun verb constructs, such as updateTables() , deleteAccount() or crashProgram() .

For a function that returns a boolean indicating the success or failure of the function that usually follows this noun verb construct, I usually use something like deleteAccountWorked() or successfulTableUpdate() .

In all of these Boolean cases, I can build an easily readable if :

 if (isEmpty (list)) ... if (deleteAccountWorked (user)) ... 

And so on.

For non-logical return functions, I still adhere to the agreement that 0 is ok and all other values ​​are incorrect. The use of smart function names usually means that it is obvious.


But keep in mind that is my decision. It may or may not work for other people.

+4
source

In the parts of the application that you control and the parts that make up your external API, I would say choose one type of error handling and stick to it . Which type is less important, but should be consistent. Otherwise, people working on your code will not know what to expect, and even you yourself will scratch yourself when you return to the code in a year or so;)

+1
source

If the standardization according to the error scheme is null ==, you can mix and match both enum and bool if you create your tests as follows:

err = some_func (); if! err ...

Since the first enumeration is null, as well as a success case, it goes well with returning bool errors.

However, as a rule, it is better to return int (or enum), since this allows you to extend the error codes returned without changing the calling code.

+1
source

I would not say that this is bad practice.

There is no need to create tons of enum -s if you just need to return true / false and you have no other parameters (both true and false pretty sensible).

In addition, if your functions are named “OK,” you will have fewer “errors”

For example - IsBlaBla - expects true . If you have [Do|On]Reload , the reboot may fail for many reasons, so enum is expected. The same goes for IsConnected and Connect , etc.

+1
source

IMHO naming helps here.

eg. for functions that return a boolean value, is_foo_bar (...) or for functions that return success or an error code, do_foo_bar (...).

0
source

All Articles