Just make damned sure it isn't null



  • in my own code, as I was surfing back through it in an effort to refactor some bits:

    if (entry.getMode() != null) {
    if (entry.getMode() != null) {
    list.add(entry.getMode());
    }
    }



  • VS2010 has a keyboard shortcut to duplicate your current line just below it. I don't know what it is but I sometimes hit it accidentally. Though the lowercase method names mean this may be Java instead of C#.



  • @zelmak said:

    in my own code, as I was surfing back through it in an effort to refactor some bits:

    if (entry.getMode() != null) {
    if (entry.getMode() != null) {
    list.add(entry.getMode());
    }
    }

     

    Hehe interestingly the other day I found something similar in my code:

    [code]if (object.getValue() != null && object != null) ...[/code]

    Luckily IntelliJ marks oversights like that, so it's easily spotted and "solved" (actually, it was impossible for either object or object.getValue() to be null at that point).

     



  • It is. And NetBeans has a similar thing ... I hit it accidentally all the time, although I don't think this instance was one of those times.

    TRWTF is why would you want to copy-pasta the same line multiple times? Loop anyone?

     



  • Just in case the other thread made it null while we weren't looking...

    Of course, what if it made it null after we checked the second time? Better be safe!

    while (entry.getMode() != null) {
      // you are slaying lots and lots of evil monsters! keep checking!
    }
    throw new NullPointerException("entry mode is null");


  • @zelmak said:

    in my own code, as I was surfing back through it in an effort to refactor some bits:

    if (entry.getMode() != null) {
    if (entry.getMode() != null) {
    list.add(entry.getMode());
    }
    }

    Rookie mistake: wasting cycles in preventing garbage-in instead of focusing on preventing garbage-out later (which is called the "reverse TSA" design pattern).



  •  @mott555 said:

    Kapla!

    Isn't it usually spelled Qapla'?

     



  •  Yesterday I realized I'd been writting stuff like

    var x = y!=null ? y : z;

     in C# because I had forgotten the  null-coalescing operator existed.

    I swear my own brain is trolling me.



  • @zelmak said:

    in my own code, as I was surfing back through it in an effort to refactor some bits:

    if (entry.getMode() != null) {
    if (entry.getMode() != null) {
    list.add(entry.getMode());
    }
    }

    Perhaps you were trying to do double-checked locking? :)

     



  • @jnz said:

    Isn't it usually spelled Qapla'?

    Don't bother, he doesn't react. I called him a Ferengi, but even that didn't provoke a response. I think he's a dyslectic Star Trek fan.



  • @zelmak said:

    TRWTF is why would you want to copy-pasta the same line multiple times? Loop anyone?
     

     

    Might be useful for things that aren't loopable enough to be useful, e.g.

     

    Params(0) = New SqlParameter("@foo", foo)

    Params(1) = New SqlParameter("@bar", bar)


     

     



  • @emurphy said:

    @zelmak said:

    TRWTF is why would you want to copy-pasta the same line multiple times? Loop anyone?
     

     

    Might be useful for things that aren't loopable enough to be useful, e.g.

     

    Params(0) = New SqlParameter("@foo", foo)

    Params(1) = New SqlParameter("@bar", bar)

    Fair enough. I guess I was reliving what I saw over someone's shoulder here:

     
    .
    .
    .
    } else {
      index++;
      index++;
      index++;
      index++;
      index++;
      index++;
      index++;
      index++;
      index++;
      index++;
      index++;
      index++;
      index++;
      index++;
      index++;
      index++;
      index++;
      index++;
      index++;
      index++;
      index++;
    }


  • @zelmak said:

    Fair enough. I guess I was reliving what I saw over someone's shoulder here:
     

    What happened next?



  • @dhromed said:

    @zelmak said:

    Fair enough. I guess I was reliving what I saw over someone's shoulder here:
     

    What happened next?

     

     

    He took the index out for its first beer.

     

     



  • @zelmak said:

    Fair enough. I guess I was reliving what I saw over someone's shoulder here:


    .
    .
    .
    } else {
    for( int index2 = 0; index2 <= 20; index2++ ) {
    index++;
    }
    }

    FTFY. Or even more elegantly, save a few loop cycles:

     

    .
    .
    .
    } else {
    for( int index2 = 1; index2 <= 6; index2++ ) {
    index += index2;
    }
    }

     



  • @Severity One said:

    Or even more elegantly, save a few loop cycles:
    .
    .
    .
    } else {
    for( int index2 = 1; index2 <= 6; index2++ ) {
    index += index2;
    }
    }

    Oh sure... replace the WTF with something more WTFish.  Then, when you come back to it 2 years later, instead of asking "What was I thinking when I wrote this" you can ask "What was I thinking when I wrote this, and what exactly am I doing" while you redo the math to figure out how to fix it.



  • @zelmak said:

    in my own code, as I was surfing back through it in an effort to refactor some bits:

    if (entry.getMode() != null) {
    if (entry.getMode() != null) {
    list.add(entry.getMode());
    }
    }

    Hey, that was a couple of days ago now.  Someone better go check it again, in case it's changed since then.



  • @Severity One said:

    @zelmak said:

    in my own code, as I was surfing back through it in an effort to refactor some bits:

    if (entry.getMode() != null) {
    if (entry.getMode() != null) {
    list.add(entry.getMode());
    }
    }

    Perhaps you were trying to do double-checked locking? :)

     

    I thought the same at first, but you would normally lock if it is null and then check again so

    if( entry.getMode() == null ) {    MutexLock lock = new MutexLock( mutex );    if( entry.getMode() == null )    {      entry.setMode( new Mode );   }   // don't you hate these garbage collected languages, I want my lock to automatically go now to free up the mutex.. }

    Of course in C++ you would use boost::once.

     


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.