Comment On How Not to compareTo()

"With more changes being requested by the business and less time allowed to turn around those changes, we've fallen a bit behind on our deadlines," writes Zarar S, "so I requested that we bring on a contractor to help fill in. It was a long shot, but hey, anything not to come in to the office weekend after weekend." [expand full text]
« PrevPage 1 | Page 2Next »

Re: How Not to compareTo()

2009-06-03 08:08 • by fw (unregistered)
frist!

Re: How Not to compareTo()

2009-06-03 08:09 • by sd (unregistered)
frest!

Re: How Not to compareTo()

2009-06-03 08:10 • by stEvil (unregistered)
266962 in reply to 266960
fw:
frist!

tawt

Re: How Not to compareTo()

2009-06-03 08:16 • by me (unregistered)
Get lost babies

Re: How Not to compareTo()

2009-06-03 08:17 • by !compareTo() (unregistered)
Giggeligiggeligiigeligi... One must love the contractor and the experience they bring...

Re: How Not to compareTo()

2009-06-03 08:20 • by jan (unregistered)
Duh!
return Float.compare(f1, f2);

Re: How Not to compareTo()

2009-06-03 08:30 • by Alex (yes there are more then one) (unregistered)
Surely you know that one can never fix a project by adding more staff in the late stages of a project to make up the time. However, such bad code is unforgivable.

Re: How Not to compareTo()

2009-06-03 08:33 • by stEvil (unregistered)
266970 in reply to 266969
Alex (yes there are more then one):
Surely you know that one can never fix a project by adding more staff in the late stages of a project to make up the time...


no, but I admire their scapegoat-diversification scheme!

Re: How Not to compareTo()

2009-06-03 08:47 • by B. Grenger (unregistered)
I could have downloaded and installed Eclipse and had Eclipse do the compareTo() method for me in about 15 mintues.

Re: How Not to compareTo()

2009-06-03 08:53 • by Rufus Dufus (unregistered)
Eleventh

Re: How Not to compareTo()

2009-06-03 08:56 • by CanCountNot (unregistered)
266976 in reply to 266974
one-thousand-seven-hundred-twenty-first

Re: How Not to compareTo()

2009-06-03 08:56 • by Moe (unregistered)
Although I guess the contractor didn't know that, his method is better than it looks. Comparing floats is inherently difficult, as there is always a finite amount of precision in seemingly infinite-precision floats, let alone inherent precision of input data (measurements, monetary values etc.). Calculation results of floating point data should always be suitably rounded before comparison. His second attempt is a valid compareTo implementation assuming two decimals precision throughout the application.

Re: How Not to compareTo()

2009-06-03 08:56 • by Georgem (unregistered)
20 years experience in what? Hoodwinking clients, by the sound of it

CAPTCHA: conventio. I'll get back to you when I've retro-fitted a meaning to it, but it sounds sexy

Re: How Not to compareTo()

2009-06-03 09:01 • by eby (unregistered)
266982 in reply to 266960
public int amIfrist(Object 1st) {
(int)(100.0 * (frist() - first()));
return 1;
}

i'm always nr 1!

Re: How Not to compareTo()

2009-06-03 09:09 • by Alargule (unregistered)
16th/17th/whateverth!

OMG...Had a good laugh, though. Maybe this guy should consider a career switch to standup comedian. 100% Failure Guaranteed!

Re: How Not to compareTo()

2009-06-03 09:26 • by Thief^
266989 in reply to 266978
Moe:
Although I guess the contractor didn't know that, his method is better than it looks. Comparing floats is inherently difficult, as there is always a finite amount of precision in seemingly infinite-precision floats, let alone inherent precision of input data (measurements, monetary values etc.). Calculation results of floating point data should always be suitably rounded before comparison. His second attempt is a valid compareTo implementation assuming two decimals precision throughout the application.

Does java truncate or round on a float to int cast?
C truncates, meaning a compare function like that would consider 3.99999999 and 4.00 as different numbers, despite your "two decimal places of precision".

Re: How Not to compareTo()

2009-06-03 09:26 • by Zapp Brannigan (unregistered)
266990 in reply to 266985
The consultant was probably experienced in another language and was new to java. When I see others struggle with similar types of errors I try to nicely teach them what is going wrong. It's usually easy to spot because the code just doesn't work. Most people appreciate the help and WE ALL started as noobs! Most people will eventually get it. However if I detect any attitude or arrogance, I am merciless.

Re: How Not to compareTo()

2009-06-03 09:28 • by Alexander (unregistered)
266991 in reply to 266978
Moe:
Although I guess the contractor didn't know that, his method is better than it looks. Comparing floats is inherently difficult, as there is always a finite amount of precision in seemingly infinite-precision floats, let alone inherent precision of input data (measurements, monetary values etc.). Calculation results of floating point data should always be suitably rounded before comparison. His second attempt is a valid compareTo implementation assuming two decimals precision throughout the application.


Agreed. Directly comparing floats for equivalence is not a good idea.
What is the Requirement Specification says for this method?

Re: How Not to compareTo()

2009-06-03 09:30 • by apz (unregistered)
Actually, you should NEVER implement Comparables that way.

Look what would happen if the values were more than Integer.MAX_VALUE away.

Read `Effective Java`, it explains this and other common pitfalls very well.

Re: How Not to compareTo()

2009-06-03 09:37 • by Gumpy Guss (unregistered)
There are so many things wrong here. First of all, you can only make a project later by adding inexperienced help. And they're all inexperienced in your app.

Next as many others have mentioned, you should think about what you mean by "equal". Floating-point numbers can have up to 128 bits of significance and can go down to 10^-308 or so. Do you really want to consider 1.0000000000001E-302 different from 1.0000000002E-302?

If there is an exact requirement like "within one cent" (although you should not be using fp numbers for money, really), he should implement that. If the requirement is "within one part per million", then it gets tricky, you have to use a relative compare, with much care:

Diff = abs( n1 - n2 )
MaxErr = Max( abs(n1), abs(n2) ) * 1.0E-6;
AboutEqual = Diff <= MaxErr;

Re: How Not to compareTo()

2009-06-03 09:39 • by Bobble (unregistered)
266997 in reply to 266990
Zapp Brannigan:
The consultant was probably experienced in another language and was new to java. When I see others struggle with similar types of errors I try to nicely teach them what is going wrong. It's usually easy to spot because the code just doesn't work. Most people appreciate the help and WE ALL started as noobs! Most people will eventually get it. However if I detect any attitude or arrogance, I am merciless.


If I wanted someone to mentor, I'd have hired a new employee or an intern. If I need a contractor, they had better be ready to go the first day and have the advertised skill set.

Re: How Not to compareTo()

2009-06-03 09:39 • by Flipper (unregistered)
And yes, I'm still coming in the office, weekend after weekend.

I hope the HR department doesn't hear about this. Or your girlfriend / wife, unless of course that's who you're coming with.

Re: How Not to compareTo()

2009-06-03 09:41 • by revenant (unregistered)
TRWTF is that they needed a contractor to write this code. It'd take more time to go to someone and to explain the task then to do it.

Re: How Not to compareTo()

2009-06-03 09:42 • by snoofle
267002 in reply to 266993
apz:
Actually, you should NEVER implement Comparables that way.

Look what would happen if the values were more than Integer.MAX_VALUE away.

Read `Effective Java`, it explains this and other common pitfalls very well.
Agreed, but sometimes you know the range of the variable (it would be nice if it were documented)

Re: How Not to compareTo()

2009-06-03 09:42 • by Someone You Know
Did this "veteran" actually claim to have twenty years of Java experience?

Re: How Not to compareTo()

2009-06-03 09:42 • by Married Guy (unregistered)
267004 in reply to 266998
Flipper:
And yes, I'm still coming in the office, weekend after weekend.

I hope the HR department doesn't hear about this. Or your girlfriend / wife, unless of course that's who you're coming with.

A girlfriend might be up for something like that. A wife, not likely.

Re: How Not to compareTo()

2009-06-03 09:45 • by snoofle
267006 in reply to 266996
Gumpy Guss:
There are so many things wrong here. First of all, you can only make a project later by adding inexperienced help. And they're all inexperienced in your app.

Next as many others have mentioned, you should think about what you mean by "equal". Floating-point numbers can have up to 128 bits of significance and can go down to 10^-308 or so. Do you really want to consider 1.0000000000001E-302 different from 1.0000000002E-302?

If there is an exact requirement like "within one cent" (although you should not be using fp numbers for money, really), he should implement that. If the requirement is "within one part per million", then it gets tricky, you have to use a relative compare, with much care:

Diff = abs( n1 - n2 )
MaxErr = Max( abs(n1), abs(n2) ) * 1.0E-6;
AboutEqual = Diff <= MaxErr;

Been there, coded that, hated it; randomize is your friend!

Re: How Not to compareTo()

2009-06-03 09:48 • by monkeyPushButton (unregistered)
267007 in reply to 266960
fw:
frist!
Being a site about programming, shouldn't the first poster be 0th?

Well, language dependent I guess.

Re: How Not to compareTo()

2009-06-03 09:58 • by snoofle
267010 in reply to 267003
Someone You Know:
Did this "veteran" actually claim to have twenty years of Java experience?

I have about 35 years of experience with java, and 20 with java beans, but only about 12 with Java.

Re: How Not to compareTo()

2009-06-03 09:59 • by Eh (unregistered)
267011 in reply to 266973
B. Grenger:
I could have downloaded and installed Eclipse and had Eclipse do the compareTo() method for me in about 15 mintues.


I think it's time you got a faster computer ...

Re: How Not to compareTo()

2009-06-03 10:03 • by SCB (unregistered)
267013 in reply to 267011
Eh:
B. Grenger:
I could have downloaded and installed Eclipse and had Eclipse do the compareTo() method for me in about 15 mintues.


I think it's time you got a faster computer ...

That 15 minutes probably includes the "download and install" time.

Re: How Not to compareTo()

2009-06-03 10:08 • by Yanman.be (unregistered)
267015 in reply to 267013
SCB:
Eh:
B. Grenger:
I could have downloaded and installed Eclipse and had Eclipse do the compareTo() method for me in about 15 mintues.


I think it's time you got a faster computer ...

That 15 minutes probably includes the "download and install" time.


He needs a faster computer, hard drive and some more RAM.

Re: How Not to compareTo()

2009-06-03 10:15 • by Mr B
267018 in reply to 267015
Yanman.be:
SCB:
Eh:
B. Grenger:
I could have downloaded and installed Eclipse and had Eclipse do the compareTo() method for me in about 15 mintues.


I think it's time you got a faster computer ...

That 15 minutes probably includes the "download and install" time.


He needs a faster computer, hard drive and some more RAM.


He needs more internets.

Re: How Not to compareTo()

2009-06-03 10:16 • by Rootbeer
267020 in reply to 266990
Zapp Brannigan:
The consultant was probably experienced in another language and was new to java.


20 years of experience, and has never used a language with a floating-point datatype?

Re: How Not to compareTo()

2009-06-03 10:16 • by ubersoldat
267021 in reply to 267013
SCB:
Eh:
B. Grenger:
I could have downloaded and installed Eclipse and had Eclipse do the compareTo() method for me in about 15 mintues.


I think it's time you got a faster computer ...

That 15 minutes probably includes the "download and install" time.


Then he need to upgrade his DSL line

Re: How Not to compareTo()

2009-06-03 10:24 • by Coincoin
That compareTo() is as much a WTF as WHERE 1=1 is.

Hint: It's not.

Hiring a contractor 'to catch up' is a nice one though. Never heard it before.

Re: How Not to compareTo()

2009-06-03 10:34 • by SCB (unregistered)
267023 in reply to 267021
ubersoldat:
SCB:
Eh:
B. Grenger:
I could have downloaded and installed Eclipse and had Eclipse do the compareTo() method for me in about 15 mintues.


I think it's time you got a faster computer ...

That 15 minutes probably includes the "download and install" time.


Then he need to upgrade his DSL line

Yes, you're right.
Eclipse docs say you need around 140 megabytes disk space. Assuming that this is zipped down to 70 megabytes for downloading, on an 8 megabit broadband connection this should still take less than 90 seconds.
I don't know how long the installation takes though...

Re: How Not to compareTo()

2009-06-03 10:35 • by Paul W. Homer (unregistered)
Surely there must be something more unique about an "Activity" than just a VarianceAmount?

There seems to be a whole series of WTFs working their way through this story. Adding a contractor will slow things down for a while. Poor documentation results in wasted effort. A possibly ambiguous object makes for badly hacked behaviors, and poor management leads to even more wasted effort. Eek!

Re: How Not to compareTo()

2009-06-03 10:37 • by pitchingchris
267026 in reply to 267022
Coincoin:
That compareTo() is as much a WTF as

Hiring a contractor 'to catch up' is a nice one though. Never heard it before.



Theres a reason for that. Its a bad idea. You can't blame problems on the contractor because you're responsible for him.

Re: How Not to compareTo()

2009-06-03 10:42 • by Bobblehead Troll (unregistered)
267027 in reply to 267020
Rootbeer:
Zapp Brannigan:
The consultant was probably experienced in another language and was new to java.


20 years of experience, and has never used a language with a floating-point datatype?


Perhaps it was just 20 years of inexperience.

Re: How Not to compareTo()

2009-06-03 10:44 • by Georgem (unregistered)
267028 in reply to 267020
Rootbeer:
Zapp Brannigan:
The consultant was probably experienced in another language and was new to java.


20 years of experience, and has never used a language with a floating-point datatype?


To be fair, at no point was the phrase "20 years technical experience" used

Re: How Not to compareTo()

2009-06-03 10:48 • by Paula (unregistered)
Comment.01
Comment.02

Re: How Not to compareTo()

2009-06-03 10:52 • by Sumatra (Not Java) (unregistered)
267030 in reply to 267023
SCB:
ubersoldat:
SCB:
Eh:
B. Grenger:
I could have downloaded and installed Eclipse and had Eclipse do the compareTo() method for me in about 15 mintues.


I think it's time you got a faster computer ...

That 15 minutes probably includes the "download and install" time.


Then he need to upgrade his DSL line

Yes, you're right.
Eclipse docs say you need around 140 megabytes disk space. Assuming that this is zipped down to 70 megabytes for downloading, on an 8 megabit broadband connection this should still take less than 90 seconds.
I don't know how long the installation takes though...


Well, we all know that Java is slow!

Re: How Not to compareTo()

2009-06-03 10:53 • by Henning Makholm (unregistered)
267031 in reply to 266996
Gumpy Guss:
Next as many others have mentioned, you should think about what you mean by "equal". Floating-point numbers can have up to 128 bits of significance and can go down to 10^-308 or so. Do you really want to consider 1.0000000000001E-302 different from 1.0000000002E-302?

If you're implementing java.lang.Comparable, then: Yes, definitely so. Its contract is that compareTo implements a total preorder. There MUST NOT be any a, b and c where a.compareTo(b)==0 and b.compareTo(c)==0 yet a.compareTo(c)>0. If this is violated -- as your proposal would -- lots of standard sorting or container algorithms can get very confused.

In these contexts one is typically not terribly concerned with whether two close values compare as equal or different, so long as what they do is consistent with all other comparisons.

Re: How Not to compareTo()

2009-06-03 10:55 • by lolwtf
Some people have 20 years of experience. Some have 1 year of experience 20 times.

Re: How Not to compareTo()

2009-06-03 10:55 • by CaptainOblivious
267034 in reply to 266969
Huh. Why can't I delete this post? Is this a Daily WTF WTF?

Re: How Not to compareTo()

2009-06-03 10:56 • by CaptainOblivious
267035 in reply to 266969
Alex (yes there are more then one):
Surely you know that one can never fix a project by adding more staff in the late stages of a project to make up the time.
Yeah. Everyone knows only a mystical mammoth could pull off such a feat!

Re: How Not to compareTo()

2009-06-03 11:01 • by twatter (unregistered)
TRWTF is that Java was born in 1991 and was, in that fase, not yet that usable.

So I'm not sure what his 20 years of experience were, but them being in Java is scientifically impossible.

Captcha: facilisis (not a clue what it is, but it sounds gross)

Re: How Not to compareTo()

2009-06-03 11:05 • by Its an INT compare (unregistered)
So why are you worried about that code inside? It is an ***INT*** compare, so since there aren't decimals in integers, 3.1 and 3.2 *ARE* equal.... You need to consider using the decimal compare if you want to know whether 3.1= 3.2.

Re: How Not to compareTo()

2009-06-03 11:14 • by Georgem (unregistered)
267039 in reply to 267030
[quote user="Sumatra (Not Java)"][quote user="SCB"][quote user="ubersoldat"][quote user="SCB"][quote user="Eh"][quote user="B. Grenger"]I could have downloaded and installed Eclipse and had Eclipse do the compareTo() method for me in about 15 mintues.[/quote]

I think it's time you got a faster computer ...
[/quote]
That 15 minutes probably includes the "download and install" time.
[/quote]

Then he need to upgrade his DSL line[/quote]
Yes, you're right.
Eclipse docs say you need around 140 megabytes disk space. Assuming that this is zipped down to 70 megabytes for downloading, on an 8 megabit broadband connection this should still take less than 90 seconds.
I don't know how long the installation takes though...
[/quote]

Who "installs" Eclipse?
« PrevPage 1 | Page 2Next »

Add Comment