Skip to main content

Questions tagged [boolean]

Filter by
Sorted by
Tagged with
81 votes
9 answers
14k views

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 ...
Mario Garcia's user avatar
73 votes
17 answers
38k views

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? What about booleans that indicate something in past? Should I write isInitialized or wasInitialized? Should I write for properties IsManyMembers ...
Mark Twain's user avatar
58 votes
8 answers
20k views

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") { ...
1.21 gigawatts's user avatar
44 votes
3 answers
20k views

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 ...
user avatar
40 votes
8 answers
11k views

For example, suppose I have a class, Member, which has a lastChangePasswordTime: class Member{ . . . constructor(){ this.lastChangePasswordTime=null, } } whose lastChangePasswordTime ...
ocomfd's user avatar
  • 5,760
39 votes
13 answers
14k views

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 ...
ocomfd's user avatar
  • 5,760
34 votes
6 answers
80k views

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 ...
Bugster's user avatar
  • 4,043
31 votes
3 answers
8k views

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 ...
aloisdg's user avatar
  • 829
30 votes
3 answers
13k views

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 ...
hyde's user avatar
  • 3,764
27 votes
8 answers
8k views

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 ...
Anon's user avatar
  • 3,649
21 votes
9 answers
9k views

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 -----...
Juan's user avatar
  • 655
21 votes
4 answers
29k views

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 ...
Ben's user avatar
  • 748
18 votes
12 answers
7k views

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 ...
Johannes Schaub - litb's user avatar
15 votes
2 answers
15k views

"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's user avatar
12 votes
5 answers
4k views

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 ...
Kit Ramos's user avatar
  • 231
11 votes
2 answers
11k views

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 "...
user3222249's user avatar
11 votes
2 answers
511 views

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 ...
preferred_anon's user avatar
11 votes
1 answer
5k views

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 ...
rick's user avatar
  • 2,005
10 votes
6 answers
5k views

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....
Zach Olivare's user avatar
10 votes
2 answers
3k views

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(...
Qqwy's user avatar
  • 4,947
9 votes
5 answers
5k views

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 ...
Phil Helix's user avatar
  • 1,966
7 votes
8 answers
946 views

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: ...
wcminipgasker2023's user avatar
6 votes
8 answers
1k views

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: ...
ocomfd's user avatar
  • 5,760
6 votes
3 answers
8k views

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 ...
Ky -'s user avatar
  • 565
5 votes
7 answers
4k views

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 ...
themhz's user avatar
  • 169
5 votes
3 answers
9k views

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 ...
ocomfd's user avatar
  • 5,760
5 votes
3 answers
834 views

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 ...
Govind Parmar's user avatar
4 votes
6 answers
1k views

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)) ...
Stijn's user avatar
  • 361
4 votes
2 answers
2k views

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 ...
tgwtdt's user avatar
  • 159
4 votes
5 answers
421 views

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 ...
Byebye's user avatar
  • 346
4 votes
5 answers
695 views

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 ...
Royce's user avatar
  • 49
4 votes
1 answer
3k views

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 ...
Sidharth Ghoshal's user avatar
4 votes
4 answers
2k views

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 ...
user avatar
3 votes
5 answers
8k views

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)...
Adrian Muntean's user avatar
3 votes
3 answers
2k views

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 ...
Shane Hsu's user avatar
  • 188
3 votes
4 answers
8k views

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.
Vegan Sv's user avatar
  • 227
3 votes
5 answers
1k views

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? ...
Inaimathi's user avatar
  • 4,874
3 votes
1 answer
172 views

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 ...
Niklas Rosencrantz's user avatar
3 votes
2 answers
6k views

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? ...
Ryan's user avatar
  • 167
3 votes
1 answer
225 views

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 ...
billpg's user avatar
  • 729
3 votes
2 answers
5k views

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 ...
Wolf's user avatar
  • 640
3 votes
2 answers
245 views

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: /** * ...
bishop's user avatar
  • 730
3 votes
1 answer
4k views

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 ...
user avatar
3 votes
5 answers
1k views

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....
PascCase's user avatar
2 votes
4 answers
5k views

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 *...
tonysdg's user avatar
  • 131
2 votes
3 answers
577 views

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 ...
sunyata's user avatar
  • 477
2 votes
2 answers
3k views

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 ...
Anon's user avatar
  • 3,649
2 votes
1 answer
265 views

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 ...
Jack's user avatar
  • 131
2 votes
3 answers
1k views

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 ...
Joel's user avatar
  • 211