Really set that value



  • private final static void VALUE1  = "ABC";
    // ...
    private final static void VALUE57 = "XXX";

    private String item;

    private void setValue(String item) {
    this.item = item;
    }

    private void checkAndSetValue(String []items) {
    for (int i=0; i<items.length; i++) {
    String[] item = items[i].split(",");
    // ToDo: add null check
    if (item[0].equals(VALUE1)) setValue(item);
    else if (item[0].equals(VALUE2)) setValue(item);
    else if (item[0].equals(VALUE3)) setValue(item);
    else if (item[0].equals(VALUE4)) setValue(item);
    // ...
    else if (item[0].equals(VALUE54)) setValue(item);
    else if (item[0].equals(VALUE55)) setValue(item);
    else if (item[0].equals(VALUE56)) setValue(item);
    else if (item[0].equals(VALUE57)) setValue(item);
    else setValue(item);
    }
    }


  • My interpreter part of the brain just crashed while trying to run the code...

    On a second read, the compiler part says "Syntax error: String expected, got String[] while calling setValue"

     



  • @edgsousa said:

    My interpreter part of the brain just crashed while trying to run the code...

    On a second read, the compiler part says "Syntax error: String expected, got String[ while calling setValue"

     

    Yeah, that'd be me screwing up while stream editing while annonymizing; that's supposed to be:

    String item =  items[i].split(",");

    and in the if-else: item[i].equals(VALUEnn)

     



  • @snoofle said:

    @edgsousa said:

    My interpreter part of the brain just crashed while trying to run the code...

    On a second read, the compiler part says "Syntax error: String expected, got String[ while calling setValue"

     

    Yeah, that'd be me screwing up while stream editing while annonymizing; that's supposed to be:

    String item =  items[i].split(",");

    and in the if-else: item[i].equals(VALUEnn)

     


    But surely the return value from split is an array so this still wont work...

    I really like most of the stuff you post, but in this case, TRWTF is that you've posted something that you don't seem to understand well enough to be able to anonymise without breaking completely or fundamentally changing what it is doing... ;)



  •  @snoofle said:

    private final static void VALUE1 = "ABC";

    This is Java right? It's got void variables now?



  • @Hmmmm said:

    you don't seem to understand well enough to be able to anonymise without breaking completely or fundamentally changing what it is doing... ;)
    ...more likely I wasn't paying anywhere nearly enough attention when I did the annonymizing... I'm usually much more attentive to stuff like that... cascading screwupson my part - mea culpa.

    OTOH, I've got a truly awesome feature article coming up soon, so that'll more than make up for it.



  • Atrocious. Another little gem is the comment "ToDo: add null check". Now it is weird that you take the trouble of checking 58 values, but overlook null, so it probably means: I've got no idea what to do when item is null. Then again, I don't think split() returns null. It throws an exception, AFAIK, which makes the comment even odder. So I'm thinking: does this person know that Java is meant to instruct a computer, not an overzealous PhD candidate that tries to look for a way to bend the rules?



  • I just had a chance to take a closer look - wow, I really botched that editing... It was three nested arrays with lots of intervening stuff. I was trying to strip out the outer arrays (that were irrelevant), the intervening stuff, and the constant strings that were all over the place. I did it quickly and pasted it into tohtml.com without even really looking at it (never again). Then I botched it again when trying to correct it (next time I promise to let work sit while writing a post - after all, we have priorities).

    Basically, the code was an if-else chain with nearly 60 clauses, all of which did the exact same thing: call a simple setter. Then, if nothing matched, just call the setter anyway. The whole mess could have been reduced to a single setter call.

    I meant well.

    Sigh.


  • ♿ (Parody)

    @snoofle said:

    Basically, the code was an if-else chain with nearly 60 clauses, all of which did the exact same thing: call a simple setter. Then, if nothing matched, just call the setter anyway. The whole mess could have been reduced to a single setter call.

    That seemed obvious, but the botched anonymization was just sweet, sweet pedantic dickweed bait. It was quite worthy of a front page article.



  • @boomzilla said:

    sweet, sweet dick
     

    Ah yes.


  • ♿ (Parody)

    @dhromed said:

    @boomzilla said:
    sweet, sweet dick

    Ah yes

    Again, I could be upset that you've quoted me out of context, but that just seems to make my posts better. Thanks for being my editor.



  • Ohhhh that is some beautiful code. twitches

    You couldn't just replace it all with a single setter call, though; then the name "checkAndSetValue" would no longer be appropriate. Descriptive naming is important.



  • @aihtdikh said:

    Ohhhh that is some beautiful code. *twitches*
    You couldn't just replace it all with a single setter call, though; then the name "checkAndSetValue" would no longer be appropriate. Descriptive naming is important.

    Well.. you could of course keep the "checkAndSetValue" name and implement the missing null check and have one problem less. I know, I know, it's not the WTF way, I'm sorry :(



  • @Cenan said:

    the
     

     I appreciate that you modified your icon, because that is the way of our people, but I don't think you expected your gif's dithering to be run through a jpg compressor.



  • @dhromed said:

    @Cenan said:

    the
     

    I appreciate that you modified your icon, because that is the way of our people, but I don't think you expected your gif's dithering to be run through a jpg compressor.

    It's programmer art, and isn't she a beauty?

    But no, I didn't expect it. I spent a couple of seconds with it in Paint, cause that's how I roll. Then when i finally came out victorious in my fight against the Community Server, I saw what had been done and I bowed my head in shame.

     



  • @Cenan said:

    I saw what had been done and I bowed my head in shame.
     

    You will wear that mark until the end of your days.



  • @snoofle said:

    private void checkAndSetValue(String []items) {
    for (int i=0; i<items.length; i++) {
    String[] item = items[i].split(",");
    // ToDo: add null check
    if (item[0].equals(VALUE1)) setValue(item);
    else if (item[0].equals(VALUE2)) setValue(item);
    else if (item[0].equals(VALUE3)) setValue(item);
    else if (item[0].equals(VALUE4)) setValue(item);
    // ...
    else if (item[0].equals(VALUE54)) setValue(item);
    else if (item[0].equals(VALUE55)) setValue(item);
    else if (item[0].equals(VALUE56)) setValue(item);
    else if (item[0].equals(VALUE57)) setValue(item);
    else setValue(item);
    }
    }

    Yes, this kind of code also really bothers me. People should start taking advantage of Java 7 features by now, it should've read

    switch(item[0]) {
         VALUE1:
             setValue(item);
             break;
         VALUE2:
             setValue(item);
             break;
         // ...
         VALUE59:
             setValue(item);
             break;
         default:
             setValue(item);
    }
    

    Which would also eliminate the need for a null check since default would catch it, I only see advantages here!



  • @dtech said:

    @snoofle said:

    private void checkAndSetValue(String []items) {
    for (int i=0; i<items.length; i++) {
    String[] item = items[i].split(",");
    // ToDo: add null check
    if (item[0].equals(VALUE1)) setValue(item);
    else if (item[0].equals(VALUE2)) setValue(item);
    else if (item[0].equals(VALUE3)) setValue(item);
    else if (item[0].equals(VALUE4)) setValue(item);
    // ...
    else if (item[0].equals(VALUE54)) setValue(item);
    else if (item[0].equals(VALUE55)) setValue(item);
    else if (item[0].equals(VALUE56)) setValue(item);
    else if (item[0].equals(VALUE57)) setValue(item);
    else setValue(item);
    }
    }

    Yes, this kind of code also really bothers me. People should start taking advantage of Java 7 features by now, it should've read

    switch(item[0]) {
         VALUE1:
             setValue(item);
             break;
         VALUE2:
             setValue(item);
             break;
         // ...
         VALUE59:
             setValue(item);
             break;
         default:
             setValue(item);
    }
    

    Which would also eliminate the need for a null check since default would catch it, I only see advantages here!

    I fail to see how that is in any way, shape or form better than

    private void checkAndSetValue(String[] items) {

       foreach(String item in items) {

          String itemWeCareAbout = item.subString(0, item.IndexOf(","));

          if (itemWeCareAbout == null)

             throw new WhateverException("Fuck you"); // ToDo: fire inept developer for not typing this in the first time

          else this.item = itemWeCareAbout;

       }

    }

    Excuse my Java, I have not used her in a while.

     


Log in to reply