Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [boolean]

The tag has no usage guidance.

37
votes
8answers
8k 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
7answers
2k 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 ...
0
votes
2answers
257 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
1answer
58 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
1answer
75 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 ...
11
votes
5answers
2k 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 ...
2
votes
1answer
330 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 ...
75
votes
9answers
12k 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 ...
23
votes
8answers
6k 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 ...
3
votes
5answers
346 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)...
4
votes
4answers
168 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
13answers
9k 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 ...
7
votes
2answers
3k 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 ...
5
votes
6answers
698 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
4answers
1k 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
3answers
251 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
1answer
181 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
1answer
45 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 ...
1
vote
1answer
136 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 ...
11
votes
2answers
5k 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
3answers
291 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
0answers
102 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 ...
9
votes
2answers
834 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
2answers
1k 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
1answer
912 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
3answers
433 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
2answers
119 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 ...
1
vote
4answers
4k 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
2answers
198 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
5answers
460 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
1answer
167 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: ...
11
votes
6answers
2k 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
3answers
1k 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 ...
5
votes
3answers
5k 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
1answer
550 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
2answers
3k 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
3answers
186 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
2answers
2k 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
1answer
228 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 ...
3
votes
1answer
2k 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 ...
37
votes
2answers
28k 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 ...
29
votes
3answers
5k 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 ...
8
votes
1answer
2k 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 ...
-2
votes
4answers
508 views

What are the advantages of converting empty strings to evaluate to true as compared to false? [closed]

When converting a string to a boolean, what are the advantages of having a programming language evaluate an empty string as true and what are the advantages of having it evaluate it to false?
0
votes
1answer
709 views

Is there any reason zero should still equal false in a new programming language? [duplicate]

I understand that 0 is false because math established that a long time ago and C established it in the programming world, as talked about here. However, other than following established conventions, ...
18
votes
12answers
3k 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 ...
4
votes
6answers
780 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))...
3
votes
3answers
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
1answer
136 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 ...
2
votes
5answers
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? ...