Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are booleans better than integers?

In most programming languages, 1 and 0 can be used instead of True and False. However, from my experience, integers seem to always be easier to use.

Here are some examples of what I mean:

if x is True: x = False
else: x = True

vs

x = abs(x-1)

__

if x is False: a = 0
else: a = 5

vs

a = 5*x

In what cases are booleans simpler/more efficient to use than 1 or 0?

like image 434
John Howard Avatar asked Dec 24 '10 20:12

John Howard


People also ask

Is boolean more efficient than integer?

Using a bool IMO reflects its use much better than using an int . In fact, before C++ and C99, C89 didn't have a Boolean type. Programmers would often typedef int Bool in order to make it clear that they were using a boolean.

What is the advantage of using the type boolean rather than strings false true or integers 0 1?

What is the advantage of using the type boolean rather than strings "false"/"true" or integers 0/1? Answer: You are guaranteed that there are no other values. With strings or integers, you would need to check that no values such as "maybe" or –1 enter your calculations.

Is boolean faster than integer?

Booleans and integers are faster Integers and Booleans are generally much faster than strings.

What is the difference boolean and integer?

A boolean true is, well, a boolean value. Use it whenever you want to express that a certain binary condition is met. The integer literal 1 is a number. Use it, whenever you are counting something.


3 Answers

You should always use any boolean built-in type for boolean values in high-level languages. Your second example would be a horror to debug in the case that x is true, but equal to a value different from 1, and a tricky one to figure out for any developer new to the code - especially one not familiar with your coding style. What's wrong with

x = !x;

or

a = x ? 5 : 0;
like image 139
Gustav Barkefors Avatar answered Nov 09 '22 04:11

Gustav Barkefors


One example where an integer could be more efficient than a boolean would be in a relational database. A bit column generally can't be indexed (I can't necessarily speak for all databases on that statement, hence "generally"), so something like a tinyint would make more sense if indexing is required.

Keep in mind that, depending on the use and on the system using it, while a boolean "takes less space" because it's just a single bit (depending on the implementation), an integer is the native word size of the hardware. (Certain systems likely use a full word for a boolean, essentially saving no space when it actually runs on the metal, just to use a simple word size.)

In high-level programming languages, the choice between a boolean and an int is really more of code readability/supportability than one of efficiency. If the values are indeed limited to "true" or "false" then a boolean makes sense. (Is this model in a given state, such as "valid," or is it not? It will never be a third option.) If, on the other hand, there are currently two options but there could be more someday, it might be tempting to use a boolean (even just "for now") but it would logically make more sense to use an int (or an enum).

Keep that in mind also when doing "clever" things in code. Sure, it may look sleeker and cooler to do some quick int math instead of using a boolean, but what does that do to the readability of the code? Being too clever can be dangerous.

like image 42
David Avatar answered Nov 09 '22 03:11

David


For readibility and good intentions, it's always better to choose booleans for true/false.

You know that you have only two possible choices. With integers, things can get a bit tricky, especially if you're using 0 as false and anything else as true.

You can get too clever when using integers for true/false, so be careful.

Using booleans will make your intentions clearer to you 6 months later and other people who will maintain your code. The less brain cycles you have to use, the better.

like image 35
darioo Avatar answered Nov 09 '22 03:11

darioo