if( !condition ) vs. if(condition == false)

Pointless debate started with readability, turned into heated insults under 30 seconds 😂

  • 7
    At least if should be fun to watch
  • 21
    if( !condition ) vs. if ( false === condition )

    I prefer the first one.
    Every developer should know about ! negation.
  • 4
    @TylerDDevRant not tripple equals.. !1 is still mostly false and !0 is mostly true.
  • 7
    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
  • 9
    How barbaric! I always do if (a || false) {...}
  • 1
    !condition is "not equals true", it can be not equals to false too.
  • 6
  • 4
    Looking at CSS language developer.
    Oh no! He's retarded!....
  • 3
    depends on the language. sometimes if (condition == false) is necessary if you're using a nullable type, for example in Kotlin.
  • 0
    Sadly Swift doesn’t even let you do the former. Typical babysitter language.
  • 0
    I 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 -> !)
  • 2
    I don't care, just be consistent.
    I prefer the first, simply because I like less code when it doesn't impact readability and I believe negation is one of the most easily read things.
  • 0
    The second one looks way more readable to me, and it's kinda mandatory when you're dealing with non-boolean stuff
  • 1
    @Giocol like the `Boolean?` (nullable Boolean) type in Kotlin
  • 0
    p.s. devRant needs code snippet formatting
  • 1
    Technically good readability and clean code means to avoid negative conditional clause.

  • 1
    Why not both:
    if( condition != true ) or if( condition == !true )
  • 2
    The 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.
  • 2
    @Wack your logic is sound, except for the fact that compiler optimization exists...
  • 1
    @mjones44 agreed. As a professor I had used to say "compilers are your friends!" And I fore one would like to treat them nice ;)
  • 0
    I 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?
  • 0
    @devios1 yes, they both would compile to the same instructions
  • 1
    More 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.
Your Job Suck?
Get a Better Job
Add Comment