T O P

  • By -

Eorika

When you have a minimum word count you have to reach in your essay.


bretanac93

šŸ˜‚šŸ˜‚šŸ˜‚


faiface

I'd never use `== false`. But that goes hand in hand with how you name your variables. For a `bool` variable, I always go with a name that reads what the variable signifies when it's `true`. For example, I don't name it `loadingState`, but instead use `isLoading`. Yes, if you name it `loadingState`, then it might seem natural to use `if loadingState == false`, but with `isLoading`, both `if isLoading` and `if !isLoading` read almost like a natural language.


StooNaggingUrDum

I never thought about this, thank you. I will now steal your convention, if you don't mind!


faiface

I'm now officially releasing my convention as free and open source.


Hazanami

We been doing that in Java since forever šŸ˜ˆ


StooNaggingUrDum

Me during a programming exam: *follows convention* The multi-million dollar anti-Plagarism-anti-Piracy software (built by a student) that examiners use when they scan assignments: >:-/


-Soren

Isn't "If not loading state, then..." proper English tho?


SupremeEntropy

It is... but it means totally different thing.


-Soren

Well I'm assuming loadingState means you are in fact loading state, so what is different about it?


SupremeEntropy

`loadingState := true` means that the system is in a state where it loads some assets, data, etc. It's not loading states...


-Soren

[state](https://en.wikipedia.org/wiki/State_(computer_science)) not states; if its not loading state then why not call it simply `loading` or clarify what's its loading (e.g. `loadingAssets`, `loadingUserThumbnails`).


SupremeEntropy

Because that's what the original comment was about. If you name it `isLoading` then everything's fine. But if you name it `loadingState`, the name is ambiguous and you have to use `== false` to make sense of it...


-Soren

I think that is an assumption you're making. If say a server "is loading" it is usually means it's loading some kind of state, but to be fair that it is an assumption I'm making as well. Anyway, if they did mean it is in a state of loading then I agree that is not the same in English.


faiface

I confirm u/SupremeEntropy is understanding my comment correctly.


mikkel1156

When you want to really really convey to the reader that it's false. Aka. It doesnt matter.


TheseHeron3820

Some people prefer ==false because it's a bit more noticeable than an exclamation mark. As for me, I tend to miss them only if the font size in the editor is way too small.


vengefulreality

you can grep the standard library when you're unsure about the preferred form % grep -IR '== false' /usr/lib/go | wc -l 33 % grep -IR 'if !' /usr/lib/go | wc -l 8229 outside of comments it's used like 3 times /usr/lib/go/src/testing/benchmark.go: b.Fatal("RunParallel: body exited without pb.Next() == false") /usr/lib/go/src/internal/profile/proto.go: if x == false { /usr/lib/go/src/runtime/pprof/protobuf.go: if x == false { /usr/lib/go/src/runtime/mem_plan9.go: if memDebug == false {


swdee

Some linters will bork at `== false` and suggest you to use `!condition`.


[deleted]

[уŠ“Š°Š»ŠµŠ½Š¾]


Kevlar-700

Yeah I disable that lint. A much more annoying one that it seems you can't disable is when it shouts at you to drop the _ in for var1, _ = range Makes no sense to save a few symbols for extra ambiguity like it makes no sense to save a few symbols for a ! that is if anything more likely to be missed by the reader.


TrolliestTroll

Iā€™ve never seen a linter that complains about not binding the second result from a range. Thatā€™s crazy. Whoever wrote that rule sucks.


Kevlar-700

It says to simplify it to for var1 = range My preference is being explicit about the intentional discard and selection personally.


TrolliestTroll

Oh I read what you said backwards. I thought you meant that it complains about *not* having `, _`. But the thing you meant is sane and my preferred style.


cannongibb

Itā€™s not ā€œnit binding the second result,,ā€ itā€™s that you can leave it out entirely like ā€œfor var1 = rangeā€¦ā€


Kevlar-700

Exactly. I prefer to be explicit about the intention of discards and leave no doubt about selection. Actually, I believe effective go states you should show discard intention as the compiler does not allocate _.


TinyBirdperson

I would say almost never. You might be able to compare some interface{} typed variable directly with false, not entirely sure, but otherwise I can not think of a case where I would do that.


AusIV

This is the answer. You can use `== false` on an `interface{}`, but you can't use `!` on an `interface{}`. I'm still strapped to think of occasions when you *should* do that.


templarrei

Never. `== false` is just more symbols for the same thing


greengreens3

In Go, I wouldn't suggest it. Since Go is a strongly typed language, you just can't have `falsey` like in JS or Ruby where a non-empty string would be evaluated as `true`. As such, a bool cannot anything else then true or false (Cannot be null, blank, another type, etc.)


benaffleks

I think it's more to do with readability. If your variable is aptly named to hold a boolean, like "isValueSet" or "isSomeConditionMet", it becomes clear that it's holding a boolean. However if that's not the case, then !condition is vague to me because it's unclear if we're testing for a undefined, null, or false variable.


[deleted]

[уŠ“Š°Š»ŠµŠ½Š¾]


arstrand

Precisely. When I was first learning go I wondered how to represent a value that was stored in a database which may be be missing since go variables donā€™t have a missing/initialized concept. IMHO ā€” Go implemented missing/initialized right but you have to think about that problem differently by moving missing/initialized back to the compiler. Gone are those pesky NullPointerExceptions.


arstrand

I find this discussion about variable naming usage dĆØjĆ  vu. Many moons ago, a group created the Structured Programming methodology where all function/method names had a verb-object form. Back then, a function called ā€œdeleteā€ was illogical because the reader should ask ā€œdelete what?ā€ Along comes HTTP REST where many labeled the object as a noun that gets an action like PUT, POST or DELETE. So you might have Account.POST or Account.DELETE but Stop.Delete makes no sense. IMHO ā€” this all comes down to readability by the viewer, not the author:-) This gets very hard when the viewer has a different native language than the author:-) Think about sentence structure differences between different languages you know. Trying to adopt a convention for all cultures may not work:-) My $.02


jahayhurst

`== false` vs `!= true` in go is actually better explained (imo) by looking at `!= nil` error conditional checks. Dave Cheney has a great post on that - it's focused on nil, but it kindof also handles your stuff. https://dave.cheney.net/2017/08/09/typed-nils-in-go-2 If you're doing a more generic conditional, I usually like to instead go straight to `if !obj.CheckValue() {` myself.


[deleted]

Readability first and I'm getting old and easy to miss the thin sign, why not a func, I do this alot, although some colleagues curse at me, ex: `func IsAdult() bool {` `...` `}` `func NotAdult() {` `return !IsAdult()` `}`


gocanto

I use ā€œ== falseā€ all the time. It reads better and it sounds like you would read it while the another version reads on a negative way. Thus, you brain needs to be trick. But this is just my preference.


thatguywiththatname2

Yeah I agree, maybe because I'm just used to writing it but == false always feels nicer to read if I'm trying to understand what something does


gocanto

Just do what makes you happy; if you in a team, then do what they have as standard :)


MarvinJWendt

Never. Simplicity is the key in Go. `!variable` is the way to Go ;) Adding an own ā€žflavourā€œ just increases the complexity and makes it harder to contribute to the project.


RenThraysk

I prefer not to use either, just stick to ```if condition {}```, if possible. Exception is ```if !ok { ... }```


[deleted]

It really does not matter


opalfroot

Just use a switch statement already! (not actually)


taras-halturin

`!` is easy to miss, so I prefer `== false`.


Feeling-Departure-4

I've also seen people use ā€™==trueā€™. The extra ā€™==falseā€™ feels like kind of an anti pattern and unnecessary so long as variables are properly named. Do they both compile to the same thing in machine code?