Questions tagged [boolean]
The boolean tag has no summary.
70 questions
7
votes
8
answers
945
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:
...
0
votes
3
answers
252
views
Is "the boolean value is from dynamic loaded data (eg:user input, database)" a reason to use boolean parameter?
I know there are some questions about boolean flags: Is it wrong to use a boolean parameter to determine behavior?, Multiple boolean arguments - why is it bad? which indicates the following code is ...
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?
...
0
votes
2
answers
548
views
How to Represent Functional Boolean
.NET Boolean type usually makes if else pair all over the code.
Functional Boolean should be more like Either type. Ideally represented as Either<Unit, Unit>.
However, my issues with Either type ...
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 ...
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....
1
vote
6
answers
4k
views
Naming boolean fields in classes
I have had hard times naming boolean fields.
On the one hand, i've read that it is good to name them as affirmative statements, like hasAge, or canDance. It's not a problem when naming local ...
-1
votes
1
answer
331
views
Better stdbool.h
How about instead of
#define bool _Bool
#define true 1
#define false 0
#define __bool_true_false_are_defined 1
We should have this:
#define bool _Bool
#define true (bool)1
#define false (bool)0
#...
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 ...
1
vote
8
answers
4k
views
Is there a use case for boolean parameters? [closed]
Most programmers (including me) believe that methods with a boolean flag parameter should be refactored into two methods without the flag parameter. Are there any use cases for a boolean parameter ...
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 ...
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 ...
-1
votes
1
answer
80
views
How to mimic an event system with a boolean system
In a boolean system, say you have a while loop going on. Then you might have this:
if (just_did_x) {
do_something()
}
To prevent it calling do_something() more than once, you might do:
if (...
0
votes
1
answer
199
views
Data-binding for Boolean values
Wondering if there is anything closely resembling data-binding but for boolean values / triggers. It seems like it could be related to Binary Decision Diagrams (BDDs), but they are precomputed rather ...
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 ...
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
...
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 ...
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 ...
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)...
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") { ...
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 ...
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 ...
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 "...
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:
...
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 ...
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 ...
1
vote
1
answer
63
views
Eliminate areas of binary space and increment through result efficiently
The goal is to take a binary space, say 64 bits, and make certain parts of it "valid" and certain parts "invalid", then increment, starting at 0, through that space, avoiding the invalid areas.
Space ...
2
votes
1
answer
259
views
Need to match database tags using a boolean expression
I have a table, let's call it Widgets, a Tags table with about 30 tags, a WidgetsTags table that joins each widget with any number of tags, and a table called LandingPages, the landing pages are for ...
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?
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 ...
0
votes
0
answers
239
views
Qt Naming Schemes for returns of boolean member variables: enabled(); vs isEnabled(); ?
In building my classes, I have noticed that I have not been very consistent about naming my boolean returns. In Qt; I notice that many of their classes use the is prefix, but checking their coding ...
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(...
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
2k
views
How to generate random Boolean functions in Algebraic Normal Form in Python?
I am looking at A SAT-based Public Key Cryptography Scheme and got inspired to challenge myself to write an implementation of this Cryptography Scheme on Python.
A part of the cipher encoding would ...
0
votes
3
answers
1k
views
What are the problems with boolean variables? [closed]
Why is it that some languages don't even have a boolean type (and uses a constant TRUE instead), but they have many other and modern types? Sometimes it can be trouble if you make a boolean and then ...
-2
votes
2
answers
412
views
Tables vs. Booleans: Which of the two are more secure for setting admin roles in an app? [closed]
I'm currently in the process of setting up roles where there are regular users, admins, and super admins, each of which have special permissions. I deferred to this post on how to do so.
It ...
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
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:
/**
* ...
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 ...
-1
votes
1
answer
202
views
Short Circuit Execution [closed]
Modify the car painting example car.color = favoriteColor ||"black"; so that the car is painted with your favorite color if you have one; otherwise it is painted with the the color of your garage: ...
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....
-4
votes
3
answers
6k
views
Save flags in an int32 [closed]
Unity3d PlayerPrefs only allow saving certain types - string, float, and int32. Since int is composed of 32 bits, it should be able to store 32 flags or a bool[] of length 32.
So far, I've thought ...
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 ...
0
votes
1
answer
826
views
Better pattern than large if/else if/else for checking conditionals [duplicate]
I've got a fairly large set of booleans I'm checking in javascript, and then using them to alter the state of a layout in my React app. The whole thing is unwieldy, difficult to read, inelegant, and ...
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
...
0
votes
3
answers
217
views
negative boolean expressions and eager boolean OR [closed]
I come across these kinds of expressions and I have hard time reading it.
How do you read it properly to see if it is the right logic when debugging.
if (!(userLoggedIn || isAdmin)) {
console....
0
votes
2
answers
4k
views
boolean operations in C using bitfields
I am trying to implement boolean data type in C. Basically, I am working with sets.
The following code can be used to access each bit but I am unsure whether I can represent sets using this method.
...
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 ...