|
|
|
| Non-WTF Job: C++ Developer at Good Grievance (Ronkonkoma, NY) |
| « Prev | Page 1 | Page 2 | Page 3 | Page 4 | Next » |
|
Let's get these out of the way:
Brillant! He shoud have used if(IsTrue(milliDiff==0))! The real WTF is (insert lame callback here). |
Re: The Millisecond Converter
2005-10-27 13:42
•
by
Anonymous Coward
|
|
I don't get how making it positive then negative again is any easier than just doing it with the negative value.
|
|
I bet that took him -1029 millseconds to write
|
|
I like this:
As if any part of his algorithm would be "hard" if there were the possibility of a negative number. |
|
Is there a prize for when the number of WTF's exceeds the number of lines of code?
Nice to see the function accounts for the posibility of negative milliseconds and minutes. Must be used in conjunction with the flux capacitor. And I don't know *how many* times I've forgotten to watch out for the exceptional value 0. Maybe this comment will help me remember. |
|
I wish I could code that well, maybe I would make more money!!!!
|
I suppose he is a little confused about what "divison by zero" really means. The last time I checked dividing zero by any number always returns zero, so the extra check here is not necessary. |
|
He's not avoiding that. He's avoiding a bug that showed up in
.NET (and hasn't been fixed!) whereby if you divide zero by a number, you keep getting zero, instead of a DivisionWithZero exception. I attempt to avoid such problems in my code by writing only Javascript. |
I like how the comments on this site are usually bigger WTFs than the posts themselves. |
Re: The Millisecond Converter
2005-10-27 13:57
•
by
Martin Carolan
|
|
Actually, if you divide by zero a DivisionByZero exception will be
thrown in C#, something similar in Java and IIRC C++ will throw an exception aswell |
|
I'm still trying to figure out when this would be used. My guess is that he's subtracting two different time values (anyone want to wager how many lines he took to do that? how many type conversions?) then converting that difference to minutes. But any system that measures and stores enough milliseconds to make subtractable minute values is borked up enough as it is. At that point you might as well meaure fractional minutes (5.25 minutes = 5 minutes, 15 seconds).
|
You might want to re-read his comment. Try turning your sarcasm detector on also. |
I don't get it Anonymous. What's wrong with what Anonymous said? Anonymous is right. |
Not always true that 0/x = 0: x could be 0 as well. And if these were float/double values, x could also be NaN, +inf, -inf, or -0.0. Of course, this doesn't change the fact that there is nothing exceptional about 0 in the WTF code. |
Sarcasm escapes some people... |
|
0 / n = 0 // This is NOT a bug in .NET, this is valid math! n / 0 = DivideByZero Exception In this case his math should be; milliseconds / 60000 = minutes. If milliseconds is 0 then minutes will be 0 (try it if you don't believe me) |
This is true, but I meant it in the more general sense that dividing zero is the same as dividing any other number. You still have to look out for division by zero, etc. |
What's sarcasm? |
I don't believe you! I'm joining up with Dave Markle and sticking with Javascript from now on! Screw .NET! Oh by the way, everyone knows the example isn't valid C# anyways cuz the coder mispelled "bool"! LOLOLOL! |
And where do I find a detector for it? |
|
Comic Book Guy: A sarcasm detector, that's a real useful invention.
KABOOM! |
Re: The Millisecond Converter
2005-10-27 14:16
•
by
Lethargically Anonymous
|
|
"Nice to see the function accounts for the posibility of negative milliseconds and minutes. Must be used in conjunction with the flux capacitor." From the name of the passed parameter, it looks like he's expecting a difference of two timestamps, which could well be negative. Possibly he converts to positive to get a consistent round-towards-zero regardless of the sign of the input, rather than floor rounding, as well? Maybe this isn't quite as WTF as it looks at first glance. |
|
Why worry about division by zero in this example anyway?
|
That's exactly the point. This guy was writing the code and thought, "oh man, I better be careful not to divide by zero" and so put in this crazy check. I guess he didn't understand the difference of divison by zero as opposed to divison of zero. |
Me too. There seems to be a prevalance of dumb people with no sense of humour who read thedailywtf.com. My request to these dumb people, is please keep posting, I enjoy laughing at you. And the best thing is that the dumb people i'm talking about don't even know i'm talking about them. |
Possibly because he wants to round towards zero rather than to the floor. That is -2.3 minutes should round to -2 rather than -3. |
|
Well, nothing wrong with handling those exceptionals of negative differences and 0. But what if the difference in milliseconds is evenly divible by 617? Isn't that exceptional too? He needs to divide the milliseconds by 2 then multiply the resulting minutes by 2. Or is it divide and multiply by 4? Help me out here. And there's so many other exceptionals that could happen. What if the number of milliseconds happens to equal the number hours since I was born? Wouldn't that be exceptional? I don't see an if block for it. Oh, and rounding those milliseconds to the nearest (int) minute? That's just for dweebs who don't get Web 2.0. --Rank
|
|
Actually I could see somebody writing something like this if they were for some reason worried about the rounding behavior of division with negative integers. e.g. if he were concerned that (-11)/3 might be different than -(11/3) and that difference was important for some reason. It's a stretch I'll admit. The check for zero looks like an extremely common case of premature optimization. He knows what the answer is so he avoids the overhead of doing the divide. |
As far as I know, integer division does not necessarily use a floor function to perform the division. It just simply takes the integer portion of the number without regard to the numbers after the decimal point. -2.3 as a result of integer division becomes -2 2.3 as a result of integer division becomes 2 I tested it in .NET and this is the case for that implementation of integer division at least. |
Yeah, okay - but what's the WTF here? I mean, just look: descriptive variable names (and no Hungarian notation to boot!), nicely indented, just enough comments - I wish my colleagues would write code like this! (Later) Ah, NOW I see it - he should have used public instead of private, to encourage re-use of this great piece of code! Oh, and for those who just don't get it: Best, Hugo |
I think it's like rhetorics. Do you know what that is? |
I used to think otherwise until I read your comments.
|
There's no rounding involved here. He is simply taking the result of a divison operation and casting it to an integer, which drops off the decimal portion. Casting -2.3 to an integer produces -2 just as casting 2.3 to an integer produces +2. Try evaluating the following (C# code): Console.WriteLine(((int) -2.3).ToString()); |
Except integer division doesn't round, it truncates, and will therefore always go towards zero anyways. |
Aye, I didn't consider that at first. However, would there be any significance to a negative return value from the function? The difference is still a positive number of milliseconds, regardless of which one went first. I suppose we'd need to see the rest of the implementation in the app for that (not that I'd especially *like* to, mind you).
But doesn't integer division just truncate the decimal anyway? Plus, based on the rest of the function, I'd be willing to bet that wasn't the coder's thought process.... |
Re: The Millisecond Converter
2005-10-27 14:43
•
by
Chris in Edmonton
|
|
Assuming there are more than ten additional bits in a long data type compared to an int data type, there's also an overflow here.
|
Ok, I guess my responding before reading the rest of the comments is a WTF in itself. |
|
I know how we always try to explain why the code was written in such a way...
... I just can't come up with anything for this grand masterpiece! |
|
Wow. Wow. This is definitely brillant.
The only mitigating factor is that the code works for all inputs. |
Re: The Millisecond Converter
2005-10-27 14:54
•
by
Anonymous coward
|
Is that like generics in Java 5.0? |
You're right in that there is a possible overflow; I didn't even notice that. Although, to hit the overflow, the difference passed in would have to be equal to just at 4082 years, so it's doubtful that it would happen in practical usage. Still, I hate to see code where a bug is possible even if it's not practical. |
Do I know what rhetorical means? (8(1) |
On x86 (I doubt the coder was using something else), longs and ints are both 4 bytes long. |
Re: The Millisecond Converter
2005-10-27 15:07
•
by
Rank Amateur
|
Worse than that. 2^15 minutes is only about 23 days, so overflow hits at about 2 billion (US) milliseconds. Wasn't there a bug in Windows like that that forced you to reboot servers after they were left on for about a month? --RA |
And for those of you just tuning in, today's WTF is brought to you by the letters J, A, V, and A. |
BWAAAAHAHAHAHHAHAHAHAAHHAHA! What a tosser! |
Re: The Millisecond Converter
2005-10-27 15:11
•
by
Rank Amateur
|
Oh, wait, integers take 32 bits these days, don't they? Gah! I'm showing my age! --RA |
Re: The Millisecond Converter
2005-10-27 15:15
•
by
ComputerGuyCJ
|
|
|
With a 32-bit integer, the max value it can handle would be 2^31 - 1. This comes out to 2,147,483,647 minutes or |
To be fair, some of those zeroes are pretty exceptional.... except for the ones that aren't. |
| « Prev | Page 1 | Page 2 | Page 3 | Page 4 | Next » |