Questions tagged [boolean]
68 questions
81
votes
9 answers
How can I make a call with a boolean clearer? Boolean Trap
As noted by in the comments by @benjamin-gruenbaum this is called the Boolean trap:
Say I have a function like this
UpdateRow(var item, bool externalCall);
and in my controller, that value for externalCall will always be TRUE. What is the best way…

Mario Garcia
- 813
- 1
- 6
- 9
69
votes
17 answers
Why Use !boolean_variable Over boolean_variable == false
A comment on this question: Checking if a method returns false: assign result to temporary variable, or put method invocation directly in conditional? says that you should use !boolean instead of boolean == false when testing conditions. Why? To me…

ell
- 966
- 1
- 10
- 14
65
votes
2 answers
Should I always use "is" as prefix for boolean variables?
Should I always use is as prefix for boolean variables? What about booleans that indicate something in past? Should I write isInitialized or wasInitialized? Should I write for properties IsManyMembers or HasManyMembers?
Is there any best practices?…

Mark Twain
- 761
- 1
- 5
- 6
42
votes
3 answers
Why is a Boolean value stored as a byte inside of a computer when it only requires one bit
I recently started learning to write code, and in my book I came across this question. "Why is a Boolean value stored as a byte inside of a computer when it only requires one bit?" can someone shed more light on this question?
user79817
40
votes
8 answers
Is a new Boolean field better than a null reference when a value can be meaningfully absent?
For example, suppose I have a class, Member, which has a lastChangePasswordTime:
class Member{
.
.
.
constructor(){
this.lastChangePasswordTime=null,
}
}
whose lastChangePasswordTime can be meaningful absent, because some members may…

ocomfd
- 5,652
- 8
- 29
- 37
39
votes
13 answers
Is it wrong to use a boolean parameter to determine values?
According to Is it wrong to use a boolean parameter to determine behavior?, I know the importance of avoid using boolean parameters to determine a behaviour, eg:
original version
public void setState(boolean flag){
if(flag){
a();
…

ocomfd
- 5,652
- 8
- 29
- 37
35
votes
6 answers
When should you use bools in C++?
We had an assignment for our class where we had to create a Tic-tac-toe game. People like to complicate themselves, so they wrote complex games which included menus. At the end of the game, you had to have the option to play again or quit the…

Bugster
- 4,013
- 8
- 37
- 44
31
votes
3 answers
Why does the boolean type in C++ support ++ but not --?
Why does the operator -- not exist for bool whereas it does for operator ++?
I tried in C++, and I do not know if my question apply to another language. I will be glad to know also.
I know, I can use the operator ++ with a bool. It makes any bool…

aloisdg
- 779
- 1
- 10
- 16
31
votes
3 answers
Why is Java boolean primitive type name not 'bool'?
Java has
int and Integer
boolean and Boolean
This seems a bit inconsistent, why not either
bool vs Boolean to use an established shorter name for primitive type?
or
integer vs Integer to keep type names consistent?
I think C++ had decided to…

hyde
- 3,744
- 4
- 25
- 35
27
votes
8 answers
Is there terminology for "true"ing, "false"ing, and toggling a boolean?
Lets say I am trying to describe my code in a technical meeting.
First, I set the boolean foobar to true
and
Second, I set the boolean foobar to false
seems a bit wordy. If foobar was toggled, I could probably say,
Third, I toggle…

Anon
- 3,565
- 3
- 27
- 45
21
votes
9 answers
How to turn truth table into smallest possible if / else block
How can I take a truth table and turn it into a compacted if block?
For instance, let's say I have this truth table where A and B are conditions and x, y and z are possible actions:
A B | x y z
-------------
0 0 | 0 0 1
0 1 | 0 0 1
1 0 | 0 1 0
1 1 |…

Juan
- 655
- 6
- 14
20
votes
4 answers
Returning a boolean when success or failure is the sole concern
I often find myself returning a boolean from a method, that's used in multiple locations, in order to contain all the logic around that method in a single place. All the (internal) calling method needs to know is whether the operation was…

Ben
- 728
- 2
- 7
- 17
19
votes
12 answers
Correct comment to put for boolean function arguments that are "false"?
From some open source projects, I gathered the following coding style
void someFunction(bool forget);
void ourFunction() {
someFunction(false /* forget */);
}
I always have doubt about what false means here. Does it mean "forget", or does…

Johannes Schaub - litb
- 1,011
- 1
- 9
- 16
13
votes
2 answers
Why PHP treats "0" as FALSE in boolean contexts?
"0", as a string containing one character, is not something empty intuitively. Why does PHP treat it as FALSE when converted to a boolean, unlike other programming languages?

Michael Tsang
- 810
- 2
- 7
- 13
12
votes
5 answers
When is short-circuit evaluation bad?
To be a bit more clear, I'll state that I've spent lots of time with different languages. But until now it's been either it'll use it all the time or it doesn't support it at all.
Now work has me starting on projects that require VB.net and I see…

Kit Ramos
- 231
- 2
- 6