Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
if( !condition ) vs. if ( false === condition )
I prefer the first one.
Every developer should know about ! negation. -
@TylerDDevRant not tripple equals.. !1 is still mostly false and !0 is mostly true.
-
If it's a single condition I use "!condition", but if multiple conditions are stacked I use "condition == false && variable = 'foo' " to get better readability
-
depends on the language. sometimes if (condition == false) is necessary if you're using a nullable type, for example in Kotlin.
-
ODXT32836yI read !isEnabled as "is not enabled". Plus when writing on a white board, I write it as NOT(isEnabled). Which in most languages translates easily (NOT -> !)
-
Giocol626yThe second one looks way more readable to me, and it's kinda mandatory when you're dealing with non-boolean stuff
-
cursee165956yTechnically good readability and clean code means to avoid negative conditional clause.
https://github.com/jupeter/... -
Wack61916yThe first one. Second one is bad style at best.
At worst, a computer executing this will look at `!bool`, load the `bool` in a register and compare it to `0` processors usually have zero in a special register. The second one on the other hand has to first load `bool` in a register, then lookup `false` and load it in a register and then finaly compare the two.
Sure a "smart" compiler will take care of this. However IMHO the second one is plain stupid and thus wrong. It doesn't help with readability and in the worst case will slow the execution of the program down.
Note: I used `bool` as allready evaluated expression here as with both the same work would be required.
Fight me. -
@Wack your logic is sound, except for the fact that compiler optimization exists...
-
devios157016yI was going to say, isn't false typically defined as 0 anyway, such that in both cases it would just compile to a BZ or BNZ?
-
Wack61916yMore or less yes, but false usually is defined as a constant on the language level, which in the processor then (probably) is loaded in an arbitrary register, while the zero register could be used for this, which is one register load less.
Related Rants
-
highonsleep27Frontend Dev: That's not possible in Frontend, we should do it in Backend. Backend Dev: That's not possible i...
-
kmccmk914Her: What's source control? Me: Explain Her: Why do we need that? I don't see a purpose
-
marcorodnav15Friend: Atom Me: Vs Code Friend: Light theme Me: Dark theme Friend: I believe there's some kind of energy th...
if( !condition ) vs. if(condition == false)
Pointless debate started with readability, turned into heated insults under 30 seconds 😂
rant
wk112