- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
So zip codes are immutable, eh? Frist time I hear that...
Admin
Shouldn't that be better "if (!obj instanceof ZipCodeInfo) {"
Admin
That's a beauty!
Admin
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)
Admin
Wait, what?
Admin
Admin
Because I heard using prime numbers for hashing is a good idea (but I forgot what prime numbers are)
Admin
I wonder whether the guy who programmed this read a book by Josh Bloch and misunderstood it ...
Admin
Admin
"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 ..."
Admin
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.
Admin
Admin
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.
Admin
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.
Admin
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 ...
Admin
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; }
Admin
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.
Admin
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.
Admin
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!
Admin
Feature this, please.
Admin
Admin
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
Admin
Admin
I think you missed the reference there. What you need is a bag of scrabble tiles.
Admin
Never read the HGTTG? Or should I just say whoosh!?
abigo: Talking about HGTTG strokes my nerd abigo...
Admin
You stroke your nerd? Is that what the uncool kids are calling it these days?
Admin
Admin
[quote user="Bernie The Bernie"]That hashCode is not safe! Ough to be
I think this is the first Catch-22 reference I've ever seen on TDWTF...
Admin
Oh, no, not again!
Admin
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.
Admin
The hash and equals code where likely autogenerated by Eclipse, which unchanged would be,
Still a wtf the the developer messed with the prime...
Admin
if (obj instanceof ZipCodeInfo) { return false; }
It returns false here?!
Admin
Admin
Admin
Admin
Well, you can overload the new operator to return NULL, yay!
Admin
It must be Thursday. I could never get the hang of Thursdays.
Admin
Admin
Admin
Brain the size of a planet, and you've never read the best book ever written.
Admin
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.
Admin
Do you know how hard it is to write code that is both thread- and neutrino-safe?!?!
Admin
FTFY
Admin
In my universe, 6 * 9 = 54
Admin
Is your universe under a rock?
Admin
I also liked this:
But since 42 is prime in their world, everything's possible.
Admin
Admin
That's because you can't refer to Catch-22 on TDWTF.
Admin
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!
Admin
It's a Try-Catch-22.