38

One of my coworkers uses

if(null == something())

I know it's just coding style, but it fucking annoys me.

Comments
  • 6
    ? Whats the matter with that
  • 4
    In PHP:
    if (is_null($variable)) {
    // do something
    }
  • 7
    @sharktits He means that

    something() == null

    would be better.
  • 1
    @filthyranter it wouldnt, null checks always start with null for safety reasons. Same with "asd".equals(st)
  • 2
    @sharktits How is it safer?
  • 5
    If "value" = $value statement gives error but not other way around. That is why its safer.
  • 4
    @filthyranter because
    a()==null will break BEFORE the evaluation while
    null==a() will evaluate to whatever
  • 2
    @sharktits Never thought of that. That's really interesting.
  • 3
    @nicoco007 you can read a lot about null safety, its a fairly interesting topic
  • 1
    @sharktits Will do. Then I'll probably have to go change a whole lot of stuff in what I've done in the past.

    Shit.
  • 0
    @sharktits any good article on null safety?
  • 8
    @sharktits "a()==null will break BEFORE the evaluation while null==a() will evaluate to whatever"

    What?
  • 1
    📌
  • 6
    @fdgram i think he means something like this:

    String s = null;
    if (s.equals("test")) // This will throw a NullPointerException because you can't invoke "equals" on null

    if ("test".equals(s)) // works because "test" is a String on which equals can be invoked
  • 1
    @EaZyCode Ah, _that_ makes sense :)
  • 4
    Or its the yoda statements I think it's called... As a safer way to code

    Because if you so a single = instead of a double ==,

    This: null = someParam errors out while someParam = null just assigns the value null to the parameter and continues the if
  • 1
    i think they call it yoda notation
  • 0
    @youness Yoda conditions.

    There's also a Yoda-style type of programming where you don't use exceptions.

    "Do or do not; there is no try {"
Add Comment