- 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
Can it be? Frist?
Admin
LOL @ the boolean precision - they forgot to include "FILE_NOT_FOUND" in the precision allowances for the TRUE case.
Admin
Only organized for clarity, not optimized comment.
Clearly the second guy needs to reuse his equality functions like this:
Admin
Love that "predified" precision.
Admin
Quite a few of those look fine to me. == is all but useless with floating point numbers and a good epsilon depends on the context/expected range.
Admin
The parts with the doubles (ie. abs(double1-double2) < TOLERANCE_CONSTANT) is not really a WTF. Sometimes you certainly can wind up with two values that are only 0.0000001 off or so, which are clearly equal for our purposes, but different to the computer. Either you can round first, or do it this way.
The nulls checking logic is also only a possible WTF. Sometimes explicitly defining what happens when things are NULL is just a documentation requirement or somesuch. It IS mostly unnecessary, but it's not nearly worth posting here.
(Of course I just replaced a 6000 line C program that made use of tiny variable names and loops filled with insane Switch/Case statements, so perhaps my bar for WTFs has gotten too high.)
Admin
Most of the first ones are /valid/, but that's not necessarily 'fine' in my book. As for the second ones, they're simply atrocious. >:-P
Admin
No WTFs here, move along.
As others have pointed out: for floats, you need an epsilon. Either you put it in a common function like here, or you put it in every single compare (which would not be fun to maintain).
For the nulls, well, that's the alternative? obj1.equals(obj2) will give you a NullPointerException if obj1 is null. If your code really does have to deal with that condition, then this is the way to do it.
Admin
Also, irony.
Admin
The Date comparisons in #1 look fine to me also. I believe a Java Date includes hour, minute, second also. The comparison function is just comparing the year, month, day
Admin
In my point of view, NULLS are completely useless and stupid. Whoever came with NuLL concept need to be drawn, quartered and then left to rot on the pikes of the RDBMS compound.
Admin
Obviously, all this object computing is too difficult for most of you "programmers". I recommend a return to assembly language, maybe you "programmers" can handle that, although I doubt it.
Admin
The first example isn't a WTF at all, just poorly commented. The date comparison function is comparing two dates ignoring the time portion. In Java, Date.equals() returns false if you pass it another Date with the same calendar date but a different time. The function that checks for equality and handles the null cases is also completely fine and I have used a similar function when factoring redundant code. And as another commenter has pointed out, comparing doubles for equality REQUIRES the use of some epsilon. The only one I can't defend is the function checking two integers for equality, which looks like they just got a little too excited about this class.
James
Admin
fail fast perhaps?
I never found a case where assuming null==null was okay didn't obscure a deeper problem.
Admin
In other words, s/he needs to be NULLified. Then store his/her status in the data base as a... NULL?
Oh, it's Nagesh. Never mind.
Admin
Some equality are more equal than others
Admin
most of these are ok. Whoever submitted these just doesn't understand that sometimes you want to say that two things are equal even when they aren't exactly the same.
Admin
x = y // "y" is assigned to "x" x == y // "y" is logically equivalent to "x", can convert types if required x === y // "y" is identical to "x", no conversion of types allowed x ==== y // "y" is composed of the same electrons as "x" x ===== y // "y" ==== "x" and across all space/time continua
Admin
The RWTF is Java for not implement equality right.
Captch: enim - like enum, but with correct equality operators
Admin
Nice code. Partially, it makes sense; in total, it is total nonsense.
Thanks to the 1st amendment, any idiot can write code.
The problem is, we have to deal with the results. Unlike speech which can be just ignored. And if the author is more than an idiot... oh God.
Admin
People stating there's no WTF here:
public static bool isEqual(Object obj1, Object obj2) { ... if (obj1.Equals(obj2)) return false; return true; ... }
TRWTF is your reading ability. Even if you don't find this kind of checking ridiculous, the code is broken anyway.
Admin
6000 lines with crazy switches eh? Directly coded finite state machines often end up looking like that, good times
Admin
Um, why do the last 3 functions all look like they return the same thing.
isEquals is notEquals is isNotEquals
Admin
A few of the posters seem to have overlooked a couple of points.
equalsForMoney(double, double) implies that doubles are being used to store monetary values, which is an almost certain (in the measure-theoretic sense) indication of a design error.
The comment on equals(int, int) is rather misleading.
Admin
What if you are synchronizing two whatevers and need to know if a value has changed, null being one of the possibilities?
Admin
which throws an exception when obj1 is null? Well done that man.
Admin
Amazingly Nagesh, the guy who invented Null sort of agrees with you.
Admin
Some of those may have been forced on a maintenance coder by poor decisions elsewhere in the code. There's a point in working on a bug where you say to yourself, "Hmmm ... I can fix this by rewriting a few hundred lines each in several different subroutines, or I can throw together the world's ugliest kludge in a dozen lines or so ... (thinking) ... Hey, they don't pay by the line here, do they? Screw this, I'm hacking together something that works & then going to lunch on time for a change."
Still a WTF, we just wouldn't necessarily be seeing the real WTF in this snippet.
(Or is this where I say "TRWTF is Java"?)
Admin
Sounds like an idea for a new coder challenge... Prove in the language of your choice that 2 + 2 = 5 :)
Bonus points for using extremely high level languages (like C#) or extremely low level langauges (like assembly)... it seems like it would be easiest to accomplish this in mid level languages like C++!
Admin
Trying that again... The guy who invented null kind of agrees with you.
(And yet akismet thinks this one is spam?)
Admin
Completely agree. But using floats for money is quite WTFish. Ihope nobody ever does anything important with his code.
I'd settle fot a isEqual function that returns true if both arguments are equal. That does not seem to be the author's intention...
Admin
Have to agree that the Java code is largely perfectly reasonable. The only one that's really useless is the very last int comparison functions.
Admin
I write "defensive" comments quite often... although that one is pretty terrible. I would have just written it in an optimized way if I cared, then commented it for clarity... it's a pretty tiny and simple method, you'd have to work at it a bit to make it illegible (or write it in perl).
I do defensive comments for the maintainer when the code I'm writing would make me think the original writer was a freakin' idiot on first glance, but needs to be done that way for some reason... or if I've maintained code that is very brittle, as a bit of a "here be dragons" style warning. I always tell myself I'll go back and untangle it later.. but of course there is never time and it is later forgotten.
Admin
Hah, last time I used something that compiled a state machine I was at my undergrad. God times, debuging thousands of lines of machine generated switches, with randomly generated labels, just to discover what the compiler understood from the input, so I could fix the input and get an assignment done.
Good documentation is overrated.
Admin
Nobody's complained yet about the fact that there's a utilities class of static methods that ultimately replace the standard java "obj.equals(Object o)" methods that all classes inherit?
The fact that the methods are a little noobish (I've seen similar stuff written by otherwise perfectly reasonable developers at the start of the java part of their careers) is nothing near as bad as that.
Admin
Yes, storing monetary values in non-decimal floating point is always the wrong thing to do - unless some idiot is sending you monetary values in that form, in which case you may just have to deal with it.
Admin
Admin
Those equals() methods look like what Eclipse would generate automatically (more or less).
It's ugly, but not really a WTF. Unless you regards what Eclipse does a WTF.
Admin
Admin
Admin
Admin
Admin
Admin
Admin
Admin
Keep on trollin!
Admin
(equal x y) x and y evaluate to the same result (eq? x y) x and y are the same object
Admin
Admin
Sounds like you should go back to VB6 is this wasn't a dead giveaway..
Admin
Have you not found URL tag yet? I corrected the above link for you.