Questions tagged [boolean]
The boolean tag has no summary.
70 questions
81
votes
9
answers
14k
views
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 ...
73
votes
17
answers
38k
views
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 ...
67
votes
2
answers
61k
views
Should I always use "is" as prefix for boolean variables? [closed]
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 ...
58
votes
8
answers
20k
views
Is there a keyword or operator for "nor"?
Is there an operator equivalent of nor? For example, my favorite color is neither green nor blue.
And the code would be equivalent to:
// example one
if (color!="green" && color!="blue") { ...
44
votes
3
answers
20k
views
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 ...
40
votes
8
answers
11k
views
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 ...
39
votes
13
answers
14k
views
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 ...
34
votes
6
answers
80k
views
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 ...
31
votes
3
answers
8k
views
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 ...
30
votes
3
answers
13k
views
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 ...
27
votes
8
answers
8k
views
Is there terminology for "true"ing, "false"ing, and toggling a boolean? [closed]
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 ...
21
votes
9
answers
9k
views
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
-----...
21
votes
4
answers
29k
views
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 ...
18
votes
12
answers
7k
views
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 ...
15
votes
2
answers
15k
views
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?
12
votes
5
answers
4k
views
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 ...
11
votes
2
answers
11k
views
boolean size not defined in java: why?
I see size of boolean is not defined. Below are two statements I see at java primitive data size
not precisely defined
Further explanation says
boolean represents one bit of information, but its "...
11
votes
2
answers
511
views
Is it possible to test if/else trees properly without coding to the implementation?
I have been writing tests for a lot of long if/else trees recently, and I'm finding it a little discouraging. I want to speak in concrete terms, so consider the following example (I'll write in Ruby ...
11
votes
1
answer
5k
views
Should methods that return boolean be named after a question or an assertion? [closed]
Many naming conventions recommend that methods returning a boolean (also called predicate methods) should be named after a question. My question is: don't they really mean the methods should be named ...
10
votes
6
answers
5k
views
Best practice boolean assignment [closed]
I came across the following conditional in a program that I have taken over from another developer:
if (obj.Performance <= LOW_PERFORMANCE)
{
obj.NeedsChange = true;
}
else
{
obj....
10
votes
2
answers
3k
views
Is it possible to define all bitwise operators using a 'bitwise nand' similar to how all boolean logic can be built using just 'boolean nand'?
Nand is known as a 'universal' logic gate, because it allows you define all other boolean logic gates:
not(x) = nand(x,x)
and(x, y) = not(nand(x, y))
or(x, y) = nand(not(x), not(y))
nor(x, y) = not(...
9
votes
5
answers
5k
views
Why is there both a short-circuit OR as well as unshort-circuited variation of that operator in C#?
Periodically, I wonder about this:
The short-circuit OR would always return the same value that the unshort-circuited OR operator would?
I expect that the short-circuit OR would always evaluate ...
7
votes
8
answers
946
views
Why is "one function do more than one thing" a disadvantage of "boolean parameter", but not in "polymorphism"?
According to Is it wrong to use a boolean parameter to determine behavior?, I know using boolean parameters to decide the behaviour is bad, for example, when using boolean parameters as the following:
...
6
votes
8
answers
1k
views
How to eliminate a if-else block which is used to determine behaviour according to a boolean input?
According to Is it wrong to use a boolean parameter to determine behavior?, I know it is bad:
public void myFunction(boolean b){
if(b){
}else{
}
}
and it should have separate function:
...
6
votes
3
answers
8k
views
Should a Java Boolean be used for ternary (3-state) logic?
I'm trying to make a basic cache of a boolean value, and I did it like such:
private Boolean _valueCache = null;
private boolean getValue() {
try {
if (_valueCache == null) { // if cache ...
5
votes
7
answers
4k
views
Can I use Boolean algebra to reduce the number of lines in my code?
I am recently studying computer science and I was introduced into Boolean algebra. It seems that Boolean algebra is used to simplify logic gates in hardware in order to make the circuit design minimal ...
5
votes
3
answers
9k
views
Is it necessary for a boolean to be "false" by default?
For example, to store whether sound is on, I have a boolean originally named "isSoundOn":
private boolean isSoundOn=true;
however, the default value of boolean is false, but I want my application to ...
5
votes
3
answers
834
views
Is it good practice to ever cast a boolean to an integer for arithmetic?
If I have a function where I am operating on a specific offset within a string, and the exact location of this offset depends on a previous test within the function, is it "bad practice" or "ugly" to ...
4
votes
6
answers
1k
views
Are there any valid use-cases for eager boolean evaluation?
Today I learned about eager boolean evaluation. In the following example, Bar() will be evaluated even when Foo() is true.
if (Foo() | Bar())
This answer on SO has an example:
if ((first = (i == 7)) ...
4
votes
2
answers
2k
views
How to name a Boolean variable that represents either of two options?
I'm developing an application with Python. I want to have a Boolean variable that represent whether something is buy or sell but I'm not sure how I should name it. Here are my current ideas:
isBuy
...
4
votes
5
answers
421
views
Using dedicated functions for opposites/inverse
I have a disagreement with one of my colleagues on whether or not functions should have inverse functions available.
I would like to know when/if inverse functions should be used.
For example, say we ...
4
votes
5
answers
695
views
What was the reason for the creation of boolean variables?
I found out that some languages like C don't have support for boolean variables and programmers use integers with values of 0 and 1 instead. Is there any specific reason why some languages moved away ...
4
votes
1
answer
3k
views
How to create an algorithm for determining Boolean Tautologies
So i am right now exploring some topics in a proof course and it occurred to me to try to create a boolean tautology solver. I would like an algorithm that is more efficient than brute force.
Problem ...
4
votes
4
answers
2k
views
Using a Break or Return instead of setting a Flag [duplicate]
I was reading on this page about setting a flag in a loop and using it later. Most of the answers agreed that it's a code smell.
One of the answers suggested refactoring the code by putting the loop ...
3
votes
5
answers
8k
views
What's the general option on passing a boolean parameter in a method or constructor in OOP languages? [closed]
I came across this issue at my second job interview. The technical interviewer said multiple times that booleans are not ok to be passed as parameters in methods, rather find another constructs (Enums)...
3
votes
3
answers
2k
views
8 bit and 1 byte, is this a valid question to be asked?
I saw these question in our school's past paper, and I'm wondering if this is a valid question.
How big is bool in C and C++?
A) 1 bit
B) 4 bit
C) 8 bit
D) 1 byte
What is ...
3
votes
4
answers
8k
views
Why do negative numbers evaluate to true in PHP? [closed]
function haha($lol)
{
if($lol) { echo "plus"; }
else { echo "minus"; }
}
haha(-1) echoes plus.
Is it because PHP uses twos complement? Google search wasn't really helpful.
3
votes
5
answers
1k
views
The rationale behind Falsy values [closed]
I'm wondering what the arguments for/against Falsy values are. On what principles would you decide to add or exclude them from a language? Are there any problems you could see them causing off-hand?
...
3
votes
1
answer
172
views
Tests for emptiness vs tests for nothingness
Is there any consensus between languages how tests for emptiness are distinct from tests for noneness? In python the following expression is false:
{} is {}
However this expression evaluates to True
...
3
votes
2
answers
6k
views
how to name functions that return booleans
I like the practice of naming boolean variables with a prefix like "is", "has", "should", or "can".
But what about the functions that produce those results?
...
3
votes
1
answer
225
views
Do any languages make evaluation of the right-hand of an "AND" operation optional?
The C# language has two "AND" operators when dealing with Boolean values, & and &&. (Leaving aside the bit-wise operators.)
When the left-hand value is False, the difference between these ...
3
votes
2
answers
5k
views
Is it always safe to shift bool values?
I stumbled about a Cppcheck warning (an inconclusive one), that I mistakenly used & instead of &&:
/// @param code identifies the command. Only the lower 16 bits of are being processed
...
3
votes
2
answers
245
views
Refactoring wordy conditional tests [duplicate]
My first attempt at this question was too theoretical, so I've rewritten it with actual code. See the edit history if you care.
Supposing this logic, "suffering" from the arrow anti-pattern:
/**
* ...
3
votes
1
answer
4k
views
boolean or Boolean? [closed]
I was doing edit reviews on Stack Overflow and found out that people keeps correcting the word "boolean", replacing it with "Boolean" when it appears in text (but not in source code, of course). And ...
3
votes
5
answers
1k
views
When to extract boolean conditions into their own function?
I commonly use a boolean condition/variable when it's something too big or complicated and takes too much space by itself inside ifs - basically to avoid repeatability and thus improve readability. E....
2
votes
4
answers
5k
views
How do I return an error from a boolean function in C?
I'm working on a little pet program in C where I have a game board that consists of a bunch of squares:
typedef struct _square {
bool checked;
} Square;
typedef struct _board {
Square *...
2
votes
3
answers
577
views
Bundling of related boolean values for an SQLite database or keeping them separate?
I'm writing a reminder application where reminders can be shown or not for each day of the week (similar to Google Calendar). So I'd like to store the following in my SQLite database:
Monday: True or ...
2
votes
2
answers
3k
views
In Qt or C++, how should I check whether my `int` variable has been defined?
Short Problem:
How should I check if integers are undefined, just as I can check QStrings for having NULL values?
Backstory:
This is my coding style when I am trying to avoid overloading my ...
2
votes
1
answer
265
views
What is this boolean law?
A friend of mine has shown sometime ago the name of the below boolean technique/law but I forget that name unfortunately. Does someone know what it's called?
example in C language:
!(a || b)
It's a ...
2
votes
3
answers
1k
views
Computers that operate exclusively on boolean algebra
I was wondering if there are any computers that operate exclusively on boolean operations. For example, no add, sub, mult, or div in the instruction set (although these could be emulated with the ...