ihtkwot
ihtkwot

Reputation: 1252

Should I return int or bool as a "truth value"?

If I have a function that performs some procedure and then needs to return the truth value of something, is there a compelling reason to use int or bool as the return type?

bool Foo() {
  // ...
}

// vs.

int Foo() {
  // ...
}

Is there an appreciable difference between these two functions? What are some potential pitfalls and advantages to both of them?

Upvotes: 15

Views: 18800

Answers (9)

Terry Mahaffey
Terry Mahaffey

Reputation: 11981

Re: Pitfalls. The problem with int-as-bool is when people did this (kind of contrived, but you get the idea):

typedef int BOOL
#define TRUE 1
#define FALSE 0

BOOL FDoSomething()
{
     // code code code
     return numTimesIDidSomething;
}

void Func()
{
     // code code code
     if(FDoSomething() == TRUE) // ERROR!
     {
          //etc
     }
}

Which is a bug if numTimesIDidSomething is greater than 1.

This lead to two things,

  1. The requirement that you use strange constructs like !!boolVariable to "normalize" all ints-as-bools for TRUE
  2. The invention of a dedicated bool type in C++

Just use bool.

Upvotes: 2

Bill Forster
Bill Forster

Reputation: 6297

One thing that's not emphasized enough in the existing answers: Use bool certainly, but to get the most benefit, you simply must use good names for your bool variables and functions. I mention this because truthValue is a typical terrible name. Your bool names should always make the convention you are using obvious. Don't make other coders guess which way round the bool variable works. So;

Good names

bool okay;
bool error;
bool success;
bool IsWidget();

Bad names

bool truthValue;
bool status;
bool result;
bool Widget();

You see the difference ? Admittedly your truthValue is arguably better than status or result. But basically the bad names make for confusing code;

// Bad
bool result = Func();
if( result )
{
   // Did Func() succeed or fail ? who knows
}

// Good
bool okay = Func();
if( okay )
{
   // Func() succeeded
}

Upvotes: 7

Roger Nelson
Roger Nelson

Reputation: 1912

If truthValue is a bool return bool.

This prevents a type coersion (no biggie).

Another reason is it is conventional to return an int to report status where 0 is successful and non-zero number represents an error condition. So the following would be unintutive from the callers perspective:

int process()
{
   bool success = false
   if (the process was successful)
      success = trrue;
   return success;
};

Conventionally, it should be either

bool process()
{
   bool success = false
   if (the process was successful)
      success = true;
   return success;
}

so main is:

int main()
{   return process() ? EXIT_SUCCESS : EXIT_FAILURE; }

or

int process()
{
  int result = EXIT_FAILURE
  if (the process was successful)
    result = EXIT_SUCCESS
  return result;
};

so main is:

int main()
{ return process(); }

Upvotes: 0

RC.
RC.

Reputation: 28217

If it's a genuine truth value, then you should use a bool as it makes it very clear to the caller what will be returned. When returning an int, it could be seen as a code/enum type value.

Code should be as clear and explicit as possible whether it be function names, parameter names and types, as well as the types of the return codes. This provides more self-documenting code, code that is easier to maintain, and a lower probability that someone will misinterpret what you "meant".

Upvotes: 33

Logan Capaldo
Logan Capaldo

Reputation: 40336

There are a couple of considerations. If Foo is intended to be callable from C, you can't return a bool because C doesn't know what a bool is.

int can also return values other than true or false. This is either a good thing or a bad thing depending on whether or not Foo can have other meaningful return values. If it can however, it may make more sense for Foo to return an enum value instead of an int.

If you don't have compatibility with C as a requirement, I would suggest the bool.

Upvotes: 2

pib
pib

Reputation: 3313

The big advantage of using bool is semantics. If I see a function that returns a bool, I know it will only return true or false, without even looking at or thinking too hard about the name of the function. If it returns an int, I have to think harder and look closer at the function.

While you're at it (using a bool for return value, that is), also name your bool-returning functions in such a way that makes it obvious what true and false mean as return values. Think jeopardy: state your function name in the form of a question.

Upvotes: 2

Adam Rosenfield
Adam Rosenfield

Reputation: 400284

Use bool -- it more clearly conveys the semantic meaning of your code. If another developer reads your code and sees a return type of bool, he immediately understands that it returns a truth value. If instead he sees that it returns an int, he has to dig a little further and spend more time figuring out the interface to the function (e.g. reading documentation or reading the code), which slows him down.

The only reason to use int is if you need to interface with legacy code and/or conform to a particular interface (e.g. your function gets its address taken and passed to a third-party library that expects a function of a certain prototype).

Upvotes: 6

paxdiablo
paxdiablo

Reputation: 881563

If you want to return a binary truth value then you should be using a boolean data type where available.

Compilers will optimise things if necessary, you should be writing your code as much as possible to be clear. That way, you or your successor will have no doubt five years down the track what was intended.

Upvotes: 6

Hamish Grubijan
Hamish Grubijan

Reputation: 10820

bool used to be just a macro ... now compilers force it to have values 0 or 1 only (unless it is uninitialized). I would definitely use a bool - it is self-documenting and you do not need anything other than bool. Keep it simple, specific.

Upvotes: 1

Related Questions