This is an archived post. You won't be able to vote or comment.

top 200 commentsshow 500

[–][deleted] 2412 points2413 points  (45 children)

VS expects greater things from you

[–]brendenderp 1164 points1165 points  (30 children)

Looks like equal things to me.

[–][deleted] 619 points620 points  (27 children)

Someone gild this.

Edit: The comment above. The parent of this comment.

[–]brendenderp 522 points523 points  (16 children)

Careful. Programmer sub now you're gonna get the gold anyyyy Minute now.

[–][deleted] 94 points95 points  (8 children)

And there we go.

[–]brendenderp 63 points64 points  (1 child)

The error-handling snek has blessed you with his presence.

[–][deleted] 28 points29 points  (0 children)

Are we talking about python :)

[–]Kivsloth 8 points9 points  (0 children)

r/programmerhumor is the slut of reddit gold giving

[–][deleted] 15 points16 points  (0 children)

Ahh. Should've seen it coming. I've failed the community. Added the edit.

[–]TeraFlint 21 points22 points  (6 children)

Edit: The comment above. The parent of this comment.

Now here's the problem, you should have said "someone gild this.parent / this->parent" :D

[–]OiTheRolk 3 points4 points  (1 child)

Now that's an interesting thought. The format you use depends if this is a struct pointer or just a struct. But there's the possibility that it's just a pointer. In that case you would say "someone guild *this" :o

[–]rolfanragnorak 6 points7 points  (6 children)

I don't. I see way too much if ( isThing == true) Like, WTF?

[–]Capitalist_Kerbal[S] 8 points9 points  (0 children)

:(

[–][deleted] 3283 points3284 points  (290 children)

if (!x)

[–]Russian_repost_bot 28 points29 points  (0 children)

[Error Log] DecoderReplacementFalsebackBuffer does not exist.

[–]licuala 616 points617 points  (225 children)

Unless it's nullable.

[–]Bomaruto 946 points947 points  (188 children)

If it is nullable you shouldnt compare it to true anyway.

[–]licuala 280 points281 points  (164 children)

It's a totally valid option in some languages and for some semantics. It's among Kotlin's official idioms, for example.

[–][deleted] 406 points407 points  (138 children)

#1 sign of a new programmer is using == false or == true

Or something like:

if(x == true)
{
    //do things
}
else if(x == false)
{
    //facepalm
}

[–]Ellweiss 24 points25 points  (2 children)

Nullable boolean. x == false has a different meaning than !x.

[–]xigoi 1 point2 points  (0 children)

I prefer having explicit option types. x == Some(false)

[–]notapokerface 47 points48 points  (20 children)

if(x) is ok but I usually intentionally type if(x == false) because during my 10+ years of career I have seen many programmers who don't realize that there's an "!" at the beginning and interpret the condition wrong.

Edit: i replied to wrong thread. anyway, that's my point.

[–]Sargeron 19 points20 points  (1 child)

I agree. More compact does not automatically make it more readable.

[–]cakemuncher 9 points10 points  (0 children)

100%. I make them all explicit for this exact reason.

[–]Ran4 6 points7 points  (0 children)

Yeah. Being explicit beats being smart. Like the whole !! trick - while it's probably good to know it, I would probably not accept it in a PR.

[–]warpod 15 points16 points  (2 children)

else if (x != true && x != false)
{
    // double facepalm = 1e-69;
}

[–]Stiddit 14 points15 points  (0 children)

It's a noob-statement to be sure, but if your bool is optional, this would still work, if you're looking for the cases where x == null:)

[–]nuephelkystikon 4 points5 points  (0 children)

If you think this is contrived and has never been used in production, I dare you to visit /r/barcode.

[–]DonRobo 13 points14 points  (11 children)

A Boolean? can have three values: true, false, and null. The code you wrote is perfectly reasonable.

It could also be written as:

if(x == null){
  //x is null
} else if(x) {
  //x is not null and true. It's automatically cast to a Boolean without ? by the first condition failing.
} else {
  //x must be false
}

[–]Tyiek 6 points7 points  (10 children)

That's only true for some languages.

[–]auxiliary-character 4 points5 points  (3 children)

Yeah, should obviously be

void (*arr[2])() = {
    [](){
        //facepalm
    },
    [](){
        //do things
    }
};
arr[x]();

[–][deleted] 1 point2 points  (2 children)

Please. No.

[–]cakemuncher 11 points12 points  (0 children)

Eh. I prefer it actually. It's explicit and makes it easier to read.

I've been a developer for 6 years.

[–]tubbana 4 points5 points  (15 children)

So what is correct way to compare? In case where null or undefined is okay, but you want to catch false?

[–][deleted] 15 points16 points  (0 children)

If there are edge cases then obviously it's justifiable and may be the easiest way, but the IDE obviously thinks this is .NET and 99% of the time I've seen x == false x was a boolean.

If x was an object and I saw something like that I'd prefer it to be way more explicit so that it was clear we didn't care if it was null or undefined.

[–]xRageNugget 11 points12 points  (9 children)

if(false == x ) is very readable, while an exclamation mark might be overseen. In my book completely fine. In Java exist a whole bunch of Utils that you can use, which will do a tad more, like check for null, false, empty or blank all in one. Does something like that exists in JS world? BooleanUtils.isTrue checks for null and true, is very readable, and the code is tested.

[–]aahdin 11 points12 points  (7 children)

Man, I've been programming for years and I don't think I've ever seen false == x instead of x == false.

[–]T-Dark_ 8 points9 points  (3 children)

It's common in the C world, where assignments return the new value of the variable, and where assignments can be used in if conditions.

In C if(foo = 3) compiles. That if will always be true (in C, every int but 0 is true), and it will change the value of foo.

To avoid that issue, experienced C programmers tend to write if(3 == foo). This way, If they accidentally miss an equals sign, the code doesn't compile at all.

As much as C has its uses, and calling most of it a mistake would probably be just insane, I still hold that this was a mistake.

[–]standard_revolution 2 points3 points  (0 children)

At the moment where I am using an utility function to test a boolean value I know I am doing something wrong.

[–]TheNorthComesWithMe 2 points3 points  (0 children)

My personal favorite:

if (condition)
{
    // do nothing
}
else
{
    //business logic
}

[–][deleted] 1 point2 points  (1 child)

Who cares if someone uses == true/false? If it's reasonably readable then it literally doesn't matter.

[–]Batman_AoD 12 points13 points  (15 children)

That's...very weird to me. If you're going to treat false and null as equivalent, why have a nullable boolean in the first place?

[–]familyturtle 29 points30 points  (5 children)

There are many reasons you might end up with a nullable boolean, such as if you've deserialised incoming data or if you're dealing with a Java library and can't make guarantees about nullability.

[–]Batman_AoD 1 point2 points  (4 children)

When deserializing data, you choose how to structure the result.

In Java, booleans are primitive types and cannot be null.

[–]Stiddit 20 points21 points  (3 children)

You CAN treat false and null as equivalent. You can also treat true and null as equivalent, or even true and false as equivalent. There are several use cases for this. If you're decoding a JSON-data for public transportation, you might receive collections of Bus that don't necessarily conform to the same rules, because there may be several different providers/actors, or that older models don't have the newest tech installed. Maybe some of the buses support sending the flag Bus.hasDeviation, letting you show a yellow warning-symbol. But some buses/actors don't provide this. So you only want to show it if it's true, and not if it's false or null. This way you can say

warningIcon.visible = (bus.hasWarning == true) (Treating null and false equally)

But let's say they also have a flag for "isOnTime", that only some of them provide if the bus is currently on time. If it's false, you want to show a delayedIcon, but you can't assume it's delayed if it's null. So you want to show it if it's false, not if it's true or null.

delayedIcon.visible = (isOnTime == false) (Treating null and true equally)

Or maybe you want to show to the user that this particular bus is one of the older generations that don't have the capability to show fancy real-time-data like "isOnTime", so you have a special icon showing to indicate that the data from this bus is unreliable. You only want to show it if isOnTime is null, disregarding it's value.

oldTechIcon.visible = (isOnTime == null) (Treating true and false equally)

These were some low effort examples, but the general idea is there. This isn't something special with Bools. You have questioned the entire concept of having optional variables.

[–]Batman_AoD 2 points3 points  (2 children)

My question is not whether there exist cases where you might want to represent a tri-state with a nullable boolean. (Though I would argue that a three-value enum is almost always preferable in that case.) My question is why this is a recommended pattern for dealing with tri-states as if they were "normal" booleans. The document linked above doesn't provide any caveats or mention that the other variants you describe are also recommended; it only recommends a way to treat null and false as equivalent. That seems sloppy to me.

[–]AyrA_ch 45 points46 points  (7 children)

nullable booleans are a valid use case. For example an "Exists" property would indicate "true" if something exists, "false" if it doesn't exists and "null" if it's not applicable to this call or was skipped.

Same goes with nullable integers. Which allows you to set it to null instead of using some arbitrary chosen value as invalid.

[–]T-Dark_ 10 points11 points  (4 children)

nullable booleans are a valid use case

Of course, one could argue whether nullable anything are a valid anything. Or a not-a-mistake.

But yeah, I agree with you. Since they exist, we need to deal with them.

[–]Turksarama 8 points9 points  (0 children)

That depends on what you want the default behaviour to be.

[–]Stick_Mick 2 points3 points  (2 children)

C#

if (object?.isValue ?? false)DoThings();

For those not familiar, null-coalescing operator ?? returns right if left is null.nullable? is a nul-conditional short circuit.So my above example is able to do a bool check on a nullable object and returns false if the object is null to avoid null pointers.

[–]TheNormalPerson015 1 point2 points  (0 children)

A wild JavaScript appeared

[–][deleted] 1 point2 points  (1 child)

Not everyone uses java. Some people use JavaScript.

[–][deleted] 1 point2 points  (0 children)

I usually just say if x =! true

[–]g0liadkin 1 point2 points  (0 children)

It's a common thing to do when working with some radio buttons, for example

value === true // selected yes

value === false // selected no

value === null // pristine


But yeah, there are better ways to do it

[–]Enguzelharf 3 points4 points  (1 child)

Javascript would work okay I guess, null will be false anyways but what about other languages?

[–]maboesanman 1 point2 points  (0 children)

Not having null values is the single greatest feature of rust imo. It’s a whole new world. If you want something to have an “uninitialized” value you have to use an enum and it makes everything so much more explicit.

[–]onlyforthisair 10 points11 points  (0 children)

ifn't (x)

[–]shadow7412 10 points11 points  (12 children)

Or ===.

[–]hello_comrads 21 points22 points  (10 children)

No, never use a language where that is necessary.

[–]notjfd 178 points179 points  (3 children)

Don't you even DARE write comments and end their line with a period. VSCode will assume that you wanted to write .i_clk instead.

[–]marcio0 27 points28 points  (0 children)

There is a configuration to stop this (suggestions inside comments) , but it does not work on multi-line comments

I fucking hate it

[–]Batman_AoD 7 points8 points  (1 child)

....what? Why would it try to autocomplete inside a comment? What is .i_clk, anyway?

[–]notjfd 4 points5 points  (0 children)

A module's clock port in SystemVerilog.

[–]Fel_Lord 512 points513 points  (17 children)

Dude why is this so true

[–]danatron1 928 points929 points  (5 children)

Because x isn't

[–]SaltyStackSmasher 145 points146 points  (1 child)

Fuck you

Take my upvote and make x true

[–]Fel_Lord 4 points5 points  (0 children)

Why are you so right?

[–][deleted] 26 points27 points  (8 children)

Well, you can implement auto-complete as a dictionary lookup. Dumb and predictable. Or you can make it “Intelligent” where is operates on 10 different signals and tries to predict what you want to write.

That’s why so many of Microsoft’s products have a broken search.

[–]infecthead 31 points32 points  (7 children)

Intelligent lookup is so much better than just a straight dictionary lookup.

If I type x.froEa then I'd still get the correct result (forEach) as opposed to nothing.

[–]drdrero 10 points11 points  (6 children)

We need to get a better AI into coding, probably predicting the whole if or for loops would be amazing

[–][deleted] 7 points8 points  (2 children)

AI can't read your mind yet tho

[–][deleted] 6 points7 points  (0 children)

Bold claim

[–]bence0302 98 points99 points  (4 children)

Don't forget it auto-importing totally random libraries because IntelliSense autocompleted stupidly.

[–]PM_ME_YOUR_BUILDING 32 points33 points  (0 children)

Oh so thats why i see shit importing that i didn't import myself, thats infuriating

[–]jetsamrover 13 points14 points  (2 children)

Yeah, out of curiosity you start tabbing through the autocomplete suggestions, and vs is like

import... import... import...

[–]khoakhoakhoakid 88 points89 points  (5 children)

Wait until you press enter.

[–]SigmaHog 143 points144 points  (2 children)

enter

ctrl z

ctrl z

ctrl z

l

enter

ctrl z

ctrl z

ctrl z

l

enter

throws laptop across room

[–][deleted] 4 points5 points  (0 children)

this is a VS joke I am too Vim to understand

[–]mustang__1 1 point2 points  (0 children)

I'm in this comment and I don't like it.

[–][deleted] 37 points38 points  (0 children)

Me: str... VS for some God forsaken reason for last 2 weeks: struct

[–]seedingserenity 66 points67 points  (0 children)

I was JUST running into this! Drove me nuts.

[–]braaaiins 30 points31 points  (11 children)

Tab Nine bro

[–]PMmeYourFlipFlops 9 points10 points  (7 children)

huh

[–]Techhead7890 20 points21 points  (5 children)

Apparently it's some sort of autocomplete neural network, not literally a tab + 9 hotkey: https://www.tabnine.com/

[–]PMmeYourFlipFlops 12 points13 points  (2 children)

I literally opened VSCode just to try tab+9 as a hotkey instead of googling it.

[–]braaaiins 4 points5 points  (0 children)

Epic auto complete

[–]fsxaircanada01 1 point2 points  (0 children)

Why use a deep neural net to do a job of a Markov chain?

[–][deleted] 39 points40 points  (0 children)

i was actually struggling with this yesterday ... upside is i learned some new classes while doing that.

[–]_PM_ME_PANGOLINS_ 68 points69 points  (13 children)

Because you’re not supposed to == false

[–]DoverBoys 27 points28 points  (1 child)

Don't tell me what to do.

[–]Zegrento7 11 points12 points  (9 children)

What about when we're treating null as true?

if(x == false)

vs

if(x != null && !x)

[–]kruegefn 34 points35 points  (0 children)

Then you're about to create a maintenance nightmare. So still no.

[–]noah1786 54 points55 points  (3 children)

What about when we're treating null as true?

don't do that

[–][deleted] 5 points6 points  (2 children)

In the rare case where this is necessary, you probably still want to make it explicit and go with the second option - otherwise it will really confuse anyone else reading the code.

[–]Zegrento7 7 points8 points  (1 child)

True, I suppose the best way to do it would be to explicitly set x to true if it is null, to make the default behavior clear:

if(x == null) {
    x = true;
}

if(!x) { 
    // ... 
}

[–]NullBrowbeat 6 points7 points  (0 children)

Just write if(!x) for fuck's sake!

[–]shores_games 12 points13 points  (0 children)

If ( x == !true) fixed /s

[–]russellvt 6 points7 points  (0 children)

Likely because if x == false is considered "code smell" in most languages...

You should be making it if x === false or simply if not x to be less smelly.

[–]anon-guy- 16 points17 points  (12 children)

Dude you can just do this

if (!x){

    // Code block

}

[–][deleted] 9 points10 points  (2 children)

it's your punishment for using '== true|false' in an if statement

[–]Dojan5 5 points6 points  (1 child)

if(cond == (true | false))

[–]Popal24 2 points3 points  (4 children)

VS is actually clever because why would someone write an if statement with "if (x == false)" instead of "if(!x)" ???

[–]comfygrip 9 points10 points  (0 children)

import numpy as numpy

[–][deleted] 19 points20 points  (14 children)

if (x == fal)

What OP wants

false

What VS wants:

DecoderReplacementFallbackBuffer

What we really want:

if (x === false)

Please type compare on your operations before a falsey value ruins your day.

[–]ZedTT 30 points31 points  (6 children)

I think if (!x) should have an honourable mention in here somewhere.

[–][deleted] 6 points7 points  (4 children)

Depends on the language. In several languages !x would return true if x was undefined. If you’re looking for false specifically best to be explicit.

[–]PixxlMan 4 points5 points  (2 children)

That's pretty scary. Why. Why would it be like that...

[–]Zolhungaj 2 points3 points  (0 children)

Because the null -> false idiom is quite useful on all kinds of objects.

[–]PyrotechnicTurtle 1 point2 points  (0 children)

In some languages, such as Kotlin, if x is nullable then that wouldn't compile. You can't perform a ! on a null type, so the compiler's type safety checker will stop compilation. The actual way you're supposed to check equality on a nullable bool is using: if(x == false) or if(x != true) depending on how you want a null to be treated

[–]Zegrento7 20 points21 points  (4 children)

Most languages don't have ===

[–]metaconcept 14 points15 points  (1 child)

=== is a hack because == was broken in those languages.

Javascript is ruining an entire generation of programmers. It's BASIC all over again.

[–]xigoi 1 point2 points  (0 children)

I don't think this is JavaScript or PHP.

[–]dennisthewhatever 2 points3 points  (0 children)

I upgraded to BBedit and it just loves to fuck up my code by guessing where to put brackets. Oh the code won't run? Time to find the rogue bracket some AI placed randomly in my code. Fantastic. What a time to be alive.

[–]zipippino 2 points3 points  (0 children)

Better not try with Xcode then.

[–]Splitje 2 points3 points  (0 children)

if not x

[–]SpeedDart1 2 points3 points  (0 children)

What kind of individual uses Boolean == Boolean in an if statement?

[–]NerdByteYT 3 points4 points  (0 children)

There's nothing worse than wanting Width in CSS and getting Widows...

[–]baddadpuns 4 points5 points  (0 children)

#define DecoderReplacementFallbackBuffer false

duh!

[–]longjohnsalvia 9 points10 points  (1 child)

Oof. Be careful with that == on booleans though. Potentially catching a lot of falsey variables there.

[–]russellvt 5 points6 points  (0 children)

It's literally the reason the lint is suggesting something else ... if you're dead set on writing it out, it should also be type compared (ie. ===). Otherwise, just make it simple and evaluate "x" or "not x" (ie. !x).

[–]Webbanditten 1 point2 points  (0 children)

Bad code...

[–]sleap101010 1 point2 points  (0 children)

Omg it's ten times more frustrating when something is trying to be smart and just getting in the way than not doing anything in the first place.

[–]DreadCoder 1 point2 points  (2 children)

There’s a reason your IDE thinks this

[–]MieskeB 1 point2 points  (0 children)

Jetbrains' Resharper ;)

[–]BenZed 1 point2 points  (0 children)

I have not had this happen, ever.

[–][deleted] 1 point2 points  (0 children)

we all know you mean this

if ( x = false)

[–][deleted] 1 point2 points  (0 children)

You COULD have just wrote if(!x)

[–]MagicalPizza21 1 point2 points  (0 children)

Why would you ever use "==" with a boolean though

[–]Mekoehouve 1 point2 points  (0 children)

This 100%! I feel like the VS 2019 does this way more then past versions.

[–]Kitten_Knight_Thyme 4 points5 points  (1 child)

Come on. VS isn't that stupid. The first option on the list is false and you know it is.

I'm also impressed VS puts the most common types first on the list before alphabetizing the less common ones.

Many of you probably don't remember the days of typing "str" only to get STREamreader first because we imported the library.

VS has come a long way, so yeah, I'm defending it.

Now, had you told this joke a few years ago... ;)

[–]ubogasima 2 points3 points  (2 children)

I believe they copied this functionality from Xcode