• faoileag (unregistered)

    So zip codes are immutable, eh? Frist time I hear that...

  • faoileag (unregistered)
    if (obj instanceof ZipCodeInfo) {
      return false;
    }

    Shouldn't that be better "if (!obj instanceof ZipCodeInfo) {"

  • Daniel Bos (unregistered)
    zipCache.put(newZipInfo, new WeakReference(newZipInfo));
    

    That's a beauty!

  • Coward (unregistered)

    final int prime = 42;

    then he multiples it by one...

    This code is so good I thought I was looking at the codebase I'm working on at the moment.

    Dug up this little nugget yesterday

    List l = new ArrayList();

    if(l != null)

  • Squiggle (unregistered) in reply to Daniel Bos
    Daniel Bos:
    zipCache.put(newZipInfo, new WeakReference(newZipInfo));
    

    That's a beauty!

    Wait, what?

  • (cs) in reply to Coward
    Coward:
    final int prime = 42;

    then he multiples it by one...

    This code is so good I thought I was looking at the codebase I'm working on at the moment.

    Dug up this little nugget yesterday

    List l = new ArrayList();

    if(l != null)

    You can never be too careful. Those pesky cosmic rays are always messing things up.

  • Daniel Bos (unregistered)
    final int prime = 42;
    

    Because I heard using prime numbers for hashing is a good idea (but I forgot what prime numbers are)

  • QJo (unregistered)

    I wonder whether the guy who programmed this read a book by Josh Bloch and misunderstood it ...

  • (cs) in reply to Daniel Bos
    Daniel Bos:
    zipCache.put(newZipInfo, new WeakReference(newZipInfo));
    

    That's a beauty!

    Because sets are for math nerds. LAAAAAAAAAME.

  • QJo (unregistered) in reply to faoileag
    faoileag:
    if (obj instanceof ZipCodeInfo) {
      return false;
    }
    Shouldn't that be better "if (!obj instanceof ZipCodeInfo) {"

    "D'oh! And all this time I thought that Java had a bug in its HashMap implementation! I've been using Lists and sorting through them serially as a result ..."

  • sysKin (unregistered) in reply to Daniel Bos

    In case someone misses it: storing the same thing as strong reference (key to the map) and weak reference completely bypasses the point of weak references -- the object cannot be GCed.

  • faoileag (unregistered) in reply to Daniel Bos
    Daniel Bos:
    final int prime = 42;
    
    Because I heard using prime numbers for hashing is a good idea (but I forgot what prime numbers are)
    Actually, I think he used "prime" as a synonym for "first" or "start" value. Like in "I primed that hash code with 42".
  • < (unregistered)

    The real WTF is, that a site about code doesn't properly escape less-than symbols in code snippets... ;-)

    ** YEAH I KNOW, RIGHT? Slapped a band-aid on it. --Mark B.

  • faoileag (unregistered)

    This CodeSOD is really a gem.

    Why is ZIP_INFO a member field, reused every time getZipCodeInfo() is called? Because creating an instance is considered too expensive?

    So perhaps you save a few milliseconds, but at the price that getZipCodeInfo() is not thread safe.

    Admitted, in a lot of applications thread-safety doesn't matter at all, but still.

  • Andrew (unregistered) in reply to Coward
    Coward:
    final int prime = 42;

    then he multiples it by one...

    This code is so good I thought I was looking at the codebase I'm working on at the moment.

    Dug up this little nugget yesterday

    List l = new ArrayList();

    if(l != null)

    The fact that he adds 42 x 1 to the string hashCode is really just a translation of the existing (and good) string hashCode. From a collision perspective it should be just fine.

    I worry more about integer overflow there, but if the default behavior for Java is to just leave it alone then we should be fine there too.

    The code is just stupid ...

  • Bernie The Bernie (unregistered)

    That hashCode is not safe! Ough to be

    @Override public int hashCode() { final int prime = 42; int result = 1; try { result = prime * result + ((zipCode == null) ? 0 : zipCode.hashCode()); catch () { final int thereIsACatch = 22; result = thereIsACatch.hashCode(); } return result; }

  • Fredrik (unregistered) in reply to Squiggle
    Squiggle:
    Daniel Bos:
    zipCache.put(newZipInfo, new WeakReference(newZipInfo));
    

    That's a beauty!

    Wait, what?

    WeakReferences allow garbage collection of an object, so it's commonly used in stuff like caches (we don't really need a reference to it, but hey, if you haven't collected it already, we'll reuse it instead of reloading it). Only problem is, the key is the same object being referenced, so it'll never ever be eligible for collection. This snipped is just plain dumb.

  • foo AKA fooo (unregistered) in reply to faoileag
    faoileag:
    This CodeSOD is really a gem.

    Why is ZIP_INFO a member field, reused every time getZipCodeInfo() is called? Because creating an instance is considered too expensive?

    So perhaps you save a few milliseconds, but at the price that getZipCodeInfo() is not thread safe.

    Admitted, in a lot of applications thread-safety doesn't matter at all, but still.

    First rule of business software: If it's not in the specs, you don't do it.

    Obviously, the function was not required to be thread-safe, so he made it not thread-safe.

    Don't worry: The meetings for discussing the preliminary proposal for adding thread-safety are already scheduled.

  • foo AKA fooo (unregistered)
        final int prime = 42;
        int result = 1;
        result = prime * result + ...
    
    That's really brillant!

    First, as everyone knows 42 is not a prime because it's 6 * 9.

    However, a prime is a number that's divisible by itself and 1. Therefore, multiplying itself by 1 can turn any number into a prime. That's a really clever and little known trick. The true master shines here!

  • (cs) in reply to foo AKA fooo
    foo AKA fooo:
        final int prime = 42;
        int result = 1;
        result = prime * result + ...
    
    That's really brillant!

    First, as everyone knows 42 is not a prime because it's 6 * 9.

    However, a prime is a number that's divisible by itself and 1. Therefore, multiplying itself by 1 can turn any number into a prime. That's a really clever and little known trick. The true master shines here!

    Feature this, please.

  • Andrew (unregistered) in reply to foo AKA fooo
    foo AKA fooo:
    First, as everyone knows 42 is not a prime because it's 6 * 9.
    Bzzt. Wrong. Thanks for playing!
  • bob (unregistered) in reply to Fredrik
    Immutable is my Name:
    Squiggle:
    Daniel Bos:

    zipCache.put(newZipInfo, new WeakReference(newZipInfo));

    That's a beauty!

    Wait, what?

    WeakReferences allow garbage collection of an object, so it's commonly used in stuff like caches (we don't really *need* a reference to it, but hey, if you haven't collected it already, we'll reuse it instead of reloading it). Only problem is, the key is the same object being referenced, so it'll never ever be eligible for collection. This snipped is just plain dumb.

    I think this would be slightly less mental if zipCache was a WeakHashMap, then I guess the newZipInfo map entry could drop off if there were no external references to it

  • The Mole (unregistered) in reply to Andrew
    Andrew:
    foo AKA fooo:
    First, as everyone knows 42 is not a prime because it's 6 * 9.
    Bzzt. Wrong. Thanks for playing!
    In fact I believe they win a prize of a bowl of petunias.
  • Fred (unregistered) in reply to Andrew

    I think you missed the reference there. What you need is a bag of scrabble tiles.

  • Sean (unregistered) in reply to Andrew
    Andrew:
    foo AKA fooo:
    First, as everyone knows 42 is not a prime because it's 6 * 9.
    Bzzt. Wrong. Thanks for playing!

    Never read the HGTTG? Or should I just say whoosh!?

    abigo: Talking about HGTTG strokes my nerd abigo...

  • (cs) in reply to Sean
    Sean:
    Andrew:
    foo AKA fooo:
    First, as everyone knows 42 is not a prime because it's 6 * 9.
    Bzzt. Wrong. Thanks for playing!

    Never read the HGTTG? Or should I just say whoosh!?

    abigo: Talking about HGTTG strokes my nerd abigo...

    You stroke your nerd? Is that what the uncool kids are calling it these days?

  • Tzolkin (unregistered) in reply to Andrew
    Andrew:
    foo AKA fooo:
    First, as everyone knows 42 is not a prime because it's 6 * 9.
    Bzzt. Wrong. Thanks for playing!
    6 * 9 is 42. At least in base 13.
  • faoileag (unregistered) in reply to Bernie The Bernie

    [quote user="Bernie The Bernie"]That hashCode is not safe! Ough to be

    catch () {
        final int thereIsACatch = 22;
        result = thereIsACatch.hashCode();
    }
    

    I think this is the first Catch-22 reference I've ever seen on TDWTF...

  • (cs) in reply to The Mole
    The Mole:
    Andrew:
    foo AKA fooo:
    First, as everyone knows 42 is not a prime because it's 6 * 9.
    Bzzt. Wrong. Thanks for playing!
    In fact I believe they win a prize of a bowl of petunias.

    Oh, no, not again!

  • Your Name (unregistered) in reply to Coward
    Coward:
    List l = new ArrayList();

    if(l != null)

    I was going to say that maybe he's hung up on a C++-ism, but that's not right there either; if new fails it throws a std::bad_alloc exception. Certainly you check for null there in C with malloc, I suppose.

  • Andrew (unregistered)

    The hash and equals code where likely autogenerated by Eclipse, which unchanged would be,

     @Override
     public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((zipCode == null) ? 0 : zipCode.hashCode());
      return result;
     }
    

    Still a wtf the the developer messed with the prime...

  • PawelB (unregistered) in reply to foo AKA fooo

    if (obj instanceof ZipCodeInfo) { return false; }

    It returns false here?!

  • tmechanic (unregistered) in reply to foo AKA fooo
    foo AKA fooo:
        final int prime = 42;
        int result = 1;
        result = prime * result + ...
    
    That's really brillant!

    First, as everyone knows 42 is not a prime because it's 6 * 9.

    I always knew there was something fundamentally wrong with the universe.
  • Does That Work? (unregistered) in reply to QJo
    QJo:
    I wonder whether the guy who programmed this read a book by Josh Bloch and misunderstood it ...
    the guy who programmed this read a book by Douglas Adams and misunderstood it ...
  • C-Derb (unregistered)
    public boolean equals(Object obj) {
        if (this == obj) { 
          return true;
        }
        if (obj == null) {
          return false;
        }
        if (obj instanceof ZipCodeInfo) {
          return false;
        }
        
        return Equal.equals(this.getZipCode(), ((ZipCodeInfo) obj).getZipCode());
      }
    Bonus WTF points for comparing the two objects BEFORE checking if obj is null.
  • Anonymous (unregistered) in reply to Your Name
    Your Name:
    Coward:
    List l = new ArrayList();

    if(l != null)

    I was going to say that maybe he's hung up on a C++-ism, but that's not right there either; if new fails it throws a std::bad_alloc exception. Certainly you check for null there in C with malloc, I suppose.

    Well, you can overload the new operator to return NULL, yay!

  • (cs) in reply to tmechanic
    tmechanic:
    foo AKA fooo:
        final int prime = 42;
        int result = 1;
        result = prime * result + ...
    
    That's really brillant!

    First, as everyone knows 42 is not a prime because it's 6 * 9.

    I always knew there was something fundamentally wrong with the universe.

    It must be Thursday. I could never get the hang of Thursdays.

  • anonymous (unregistered) in reply to C-Derb
    C-Derb:
    public boolean equals(Object obj) {
        if (this == obj) { 
          return true;
        }
        if (obj == null) {
          return false;
        }
        if (obj instanceof ZipCodeInfo) {
          return false;
        }
    
    return Equal.equals(this.getZipCode(), ((ZipCodeInfo) obj).getZipCode());
    

    }

    Bonus WTF points for comparing the two objects BEFORE checking if obj is null.
    Why does it matter if obj is null? He's not dereferencing it.

  • Evan (unregistered) in reply to faoileag
    faoileag:
    So perhaps you save a few milliseconds, but at the price that getZipCodeInfo() is not thread safe.
    Just add synchronized. Duh.
    Andrew:
    I worry more about integer overflow there, but if the default behavior for Java is to just leave it alone then we should be fine there too.
    Java defines overflow semantics to match 2's complement. For something like hash codes, overflow shouldn't be a problem.
    foo AKA fooo:
    However, a prime is a number that's divisible by itself and 1. Therefore, multiplying itself by 1 can turn any number into a prime. That's a really clever and little known trick. The true master shines here!
    This is one of the best comments I have ever read.
    Your Name:
    Coward:
    List l = new ArrayList();

    if(l != null)

    I was going to say that maybe he's hung up on a C++-ism, but that's not right there either; if new fails it throws a std::bad_alloc exception. Certainly you check for null there in C with malloc, I suppose.

    Could be either a really old-school C++ programmer (new used to not throw an exception) or one who works on a project where they disable exceptions. There are an unfortunate number of those.

    C-Derb:
    public boolean equals(Object obj) {
        if (this == obj) { 
          return true;
        }
        if (obj == null) {
          return false;
        }
        ...
    }
    Bonus WTF points for comparing the two objects BEFORE checking if obj is null.
    Actually, this seems quite reasonable. Which seems more likely... o1.equal(alias_of_o1) or o1.equal(something_null)? My guess would be the former, especially if you're making this so that you can store it in a map because you're unlikely (or maybe even can't, I don't know the API) have a null key.
  • (cs) in reply to Andrew
    Andrew:
    foo AKA fooo:
    First, as everyone knows 42 is not a prime because it's 6 * 9.
    Bzzt. Wrong. Thanks for playing!

    Brain the size of a planet, and you've never read the best book ever written.

  • Immutable Coward (unregistered)

    Just love those class section comments like

    //---Constructors---//

    Could also start the top of each file with

    //----Java code----//

    just to be sure everyone's spoon-feeding needs are met.

    Captcha: tation, retalliation of a tition.

  • (cs) in reply to Zacrath
    Zacrath:
    You can never be too careful. Those pesky cosmic rays are always messing things up.

    Do you know how hard it is to write code that is both thread- and neutrino-safe?!?!

  • (cs) in reply to Immutable Coward
    Immutable Coward:
    Just love those class section comments like //---Comment---// //---Constructors---//

    Could also start the top of each file with

    //---Comment---// //----Java code----//

    just to be sure everyone's spoon-feeding needs are met.

    Captcha: tation, retalliation of a tition.

    FTFY

  • Fritz'dUpp (unregistered) in reply to foo AKA fooo

    In my universe, 6 * 9 = 54

  • (cs) in reply to Fritz'dUpp
    Fritz'dUpp:
    In my universe, 6 * 9 = 54

    Is your universe under a rock?

  • Vlad Patryshev (unregistered)

    I also liked this:

    if (obj instanceof ZipCodeInfo) {
          return false;
        }
    

    But since 42 is prime in their world, everything's possible.

  • faoileag (unregistered) in reply to C-Derb
    C-Derb:
    public boolean equals(Object obj) {
        if (this == obj) { 
          return true;
        }
        if (obj == null) {
          return false;
        }
        if (obj instanceof ZipCodeInfo) {
          return false;
        }
    
    return Equal.equals(this.getZipCode(), ((ZipCodeInfo) obj).getZipCode());
    

    }

    Bonus WTF points for comparing the two objects BEFORE checking if obj is null.
    this == obj does not compare the two objects. It compares references. That check is simply testing if someone is comparing the object with itself, so the obj == null test can come after this == obj, it doesn't matter.

  • (cs) in reply to faoileag
    faoileag:
    I think this is the first Catch-22 reference I've ever seen on TDWTF...

    That's because you can't refer to Catch-22 on TDWTF.

  • Miriam (unregistered) in reply to chubertdev

    I don't understand at all why so many people do such things. Even Eclipse with its sort of overloaded GUI manages to give you a good overview about things inside a class. You really shouldn't add those comments.

    Some of my coworkers love putting #Regions for exactly such purposes in their C♯ and VB.NET. It's horrible!

  • (cs) in reply to DCRoss
    DCRoss:
    faoileag:
    I think this is the first Catch-22 reference I've ever seen on TDWTF...

    That's because you can't refer to Catch-22 on TDWTF.

    It's a Try-Catch-22.

Leave a comment on “Immutable is my Name”

Log In or post as a guest

Replying to comment #:

« Return to Article