Comment On A Waste of Time

Every now and then, you’ll come across some code that’s just like a car crash. The twisted, mangled-up kind of crash where the car’s on its side – possibly even on fire – and you can’t help but stare at the wreck with a mix of disgust and interest. Can it be fixed? Hmm, maybe something can be salvaged from the mess. Sheesh, I hope no one died as a result of this. [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: A Waste of Time

2009-06-08 09:08 • by JoeBar (unregistered)
To get this started:

if ( comments <= NO_OF_SECOND_COMMENT )
return "first";

Re: A Waste of Time

2009-06-08 09:08 • by Man of Steel (unregistered)
One more time bug and I'm calling for a general strike. Let's all switch to POSIX time already.

Q: What time is it?

A: 1244466496

Q: When do you want to have lunch?

A: About 1244480000 work for you?

Re: A Waste of Time

2009-06-08 09:15 • by Addison (unregistered)
267759 in reply to 267757
Man of Steel:
One more time bug and I'm calling for a general strike. Let's all switch to POSIX time already.

Q: What time is it?

A: 1244466496

Q: When do you want to have lunch?

A: About 1244480000 work for you?


give us a couple hundred years to integrate with our machines and that should be perfectly reasonable.

Re: A Waste of Time

2009-06-08 09:16 • by Bobble (unregistered)
Protip: You don't have to wait until a Feb 29th to fix this bug.

Re: A Waste of Time

2009-06-08 09:23 • by Mark V Shaney (unregistered)
Most in-house time libraries I've seen were broken. Including the ones I wrote myself.

A fun one was detecting the numbers of days between two dates by asking the OS-provided library the number of seconds since a fixed date, and dividing the difference by 86400.

Worked like a charm... until daylight savings kicked in...

Re: A Waste of Time

2009-06-08 09:27 • by buja (unregistered)
*puts c++ beard on*

- no include guards, which is especially great when using so many #defines

- expects that someone puts a global "using namespace std;" before including time_help.h (or spreads specific using directives like "using std::cout;" etc. around)

- no "inline" linkage, so multiple source-files #including time_help.h will result in multiple definition errors

- mixing floating point with integer calculations without proper cast and relying on compiler specific behaviour

- "#define SECpYEAR 31536000"
some lines later
"#define YEAR time(NULL)/31536000+1970"
--> yay for still using magic numbers
--> double yay for for not putting the definition in round braces

Oh that code is so great. But regarding that it's not even 2^8 lines long, why not just rewrite instead of fixing?

Re: A Waste of Time

2009-06-08 09:32 • by zolf
"int dateformat;" and "int YEARSCOPE;" in header file is a very clever solution - each .c file can use its own time format and yearscope (whatever it means).

I like that the month length is a random variable with value (almost correct) of expected value in randomly taken month (AVGDAYINMONTH 30.4375). Although
#define AVGDAYINMONTH (0.25*365.0/12.0 + 0.25*365.0/12.0 + 0.25*365.0/12.0 + 0.25*366.0/12.0)
would not be so optimized.

Re: A Waste of Time

2009-06-08 09:33 • by Man of Steel (unregistered)
267765 in reply to 267760
Bobble:
Protip: You don't have to wait until a Feb 29th to fix this bug.

Managertip: The return on investment of fixing the bug now is zero. So, yes, you do have to wait. Hey, with any luck we'll be bankrupt before then and collecting our fat bailout bonuses.

Re: A Waste of Time

2009-06-08 09:34 • by akatherder
I remember the very moment, as a rookie coder, when I was using VBScript's date libraries and ran into yet another unexpected behavior. I made the crucial mistake of saying "Psh, I can do this better myself" instead of taking the time to understand how the date/time libraries really work.

I shunned built-in date/time functions and built workarounds for the next 2-3 years. It became second nature to just ignore their existence. Fortunately, I didn't work on a lot of products that used dates except for a timestamp here and there.

I don't know what caused me to revisit it (either working with a new language or looking at someone else's code) but I soon realized how much time and how many issues I introduced myself too and wasted my life on.

Re: A Waste of Time

2009-06-08 09:34 • by Coward (unregistered)
I see the bug! Sugust has 31 days not 30!:
else if ( days <= FEBLENGTH+31+31+30+31+30+31+30)

{
return 8;
}

Re: A Waste of Time

2009-06-08 09:34 • by SR (unregistered)
267768 in reply to 267761
Mark V Shaney:
Most in-house time libraries I've seen were broken. Including the ones I wrote myself.

A fun one was detecting the numbers of days between two dates by asking the OS-provided library the number of seconds since a fixed date, and dividing the difference by 86400.

Worked like a charm... until daylight savings kicked in...


My biggest daylight savings WTF was a calendar app that showed one row too many when the clocks went forward in spring and one too few in autumn.

The workaround was so ugly I had to code it looking through my fingers.

Re: A Waste of Time

2009-06-08 09:35 • by Coward (unregistered)
267769 in reply to 267767
Fixed!~
Coward:
I see the bug! August has 31 days not 30!:
else if ( days <= FEBLENGTH+31+31+30+31+30+31+30)

{
return 8;
}

Re: A Waste of Time

2009-06-08 09:35 • by SR (unregistered)
267770 in reply to 267767
Coward:
I see the bug! Sugust has 31 days not 30!:
else if ( days <= FEBLENGTH+31+31+30+31+30+31+30)

{
return 8;
}


Sugust only has 4.5 days ;o)

Re: A Waste of Time

2009-06-08 09:39 • by snoofle
*musical interlude*
Does anybody really know what time it is?

Re: A Waste of Time

2009-06-08 09:46 • by Ethan Qix (unregistered)
267772 in reply to 267770
Actually, with only 30 days in August, a bug would occur every day during half of the year. This code is obviously not used... or if it is, not finding said bug earlier is TRWTF.

Re: A Waste of Time

2009-06-08 09:47 • by silent d (unregistered)
267773 in reply to 267771
snoofle:
*musical interlude*
Does anybody really know what time it is?


If anybody really cares, let's hope they don't call the function from this WTF.

Re: A Waste of Time

2009-06-08 09:54 • by Kevin (unregistered)
Apparently:

August has 30 days
September has 31 days
October has 30 days
November has 31 days
December has 30 days

So during the month transitions of August, October and December, this function would return the wrong value.

But I bet the real WTF is that FEBLENGTH is a #defined constant that was set once when the code was compiled in a non-leap year.

Re: A Waste of Time

2009-06-08 09:55 • by bujo (unregistered)
267776 in reply to 267767
Strange, that dude forgot Sugust all over the place. But maybe the team fixes it soon enough in Deptember.

Re: A Waste of Time

2009-06-08 09:57 • by Kevin (unregistered)
267778 in reply to 267775
Sorry, didn't realise that the actual header was part of the article. I guess my suspicions about FEBLENGTH were unfounded.

Re: A Waste of Time

2009-06-08 10:02 • by buji (unregistered)
267779 in reply to 267778
Sorry, didn't realise that the actual header was part of the article. I guess my suspicions about FEBLENGTH were unfounded.

We accept your apologies. Just don't do it again, okay?

Re: A Waste of Time

2009-06-08 10:10 • by dpm
267780 in reply to 267761
Mark V Shaney:
Most in-house time libraries I've seen were broken. Including the ones I wrote myself.

A fun one was detecting the numbers of days between two dates by asking the OS-provided library the number of seconds since a fixed date, and dividing the difference by 86400.

Worked like a charm... until daylight savings kicked in...
In 1986 (roughly) I had to write date/time routines. I finished coding and ran the unit tests, only to see them fail consistently. Turned out that the code was fine; my mistake was using the test data in "Inside Macintosh" (at the time, the best source I could find) which had "May 12 1984" as a Wednesday. I still think very bad thoughts about whoever wrote that . . .

Re: A Waste of Time

2009-06-08 10:10 • by Ken B (unregistered)
267781 in reply to 267761
Mark V Shaney:
Most in-house time libraries I've seen were broken. Including the ones I wrote myself.
Sometimes, you need to use in-house libraries because the built-in ones are broken. Other times, people claim that the in-house library is broken when it's not.

Many years ago, we got bug reports that our IsLeap() function was broken because it said that the year 2000 was a leap year. (We got it right --- 2000 was a leap year --- while many of the "big names" got it wrong, and had to patch it along with their other Y2K fixes.)

Re: A Waste of Time

2009-06-08 10:24 • by SCB (unregistered)
267782 in reply to 267780
dpm:
Mark V Shaney:
Most in-house time libraries I've seen were broken. Including the ones I wrote myself.

A fun one was detecting the numbers of days between two dates by asking the OS-provided library the number of seconds since a fixed date, and dividing the difference by 86400.

Worked like a charm... until daylight savings kicked in...
In 1986 (roughly) I had to write date/time routines. I finished coding and ran the unit tests, only to see them fail consistently. Turned out that the code was fine; my mistake was using the test data in "Inside Macintosh" (at the time, the best source I could find) which had "May 12 1984" as a Wednesday. I still think very bad thoughts about whoever wrote that . . .

Probably because 05/12/1984 (5 December) IS a Wednesday.

Re: A Waste of Time

2009-06-08 10:29 • by cdosrun
These seem like rather important parts of the story that shouldn't have been left off...

#define LASTLEAPYEAR 2008
#define LEAPYEARINTERVAL 4
#define SINCELASTLEAPYEAR YEAR-LASTLEAPYEAR
#define YEAR time(NULL)/31536000+1970
#define LEAPYEAR ((((YEAR-LASTLEAPYEAR)%LEAPYEARINTERVAL)==0)?true:false)

Re: A Waste of Time

2009-06-08 10:33 • by Steve the Cynic (unregistered)
Let's not forget that in their world, day 22 of each month is formatted in mode 3 as the 22:th... (beats me why the : is there)

For real fun with ordinals, though, try Welsh, where depending on the number and the grammatical gender of the following noun, the suffix after digits may be -af, -il, -ydd, -edd, -ed, -fed, -eg, or -ain.

Re: A Waste of Time

2009-06-08 10:38 • by The_Assimilator (unregistered)
267785 in reply to 267782
It's not "car-crash" code, it's "WTF IS THIS SHIT?" code. I could probably take a dump and the result would make ,ore sense than this pile of garbage.

SCB:
dpm:
Mark V Shaney:
Most in-house time libraries I've seen were broken. Including the ones I wrote myself.

A fun one was detecting the numbers of days between two dates by asking the OS-provided library the number of seconds since a fixed date, and dividing the difference by 86400.

Worked like a charm... until daylight savings kicked in...
In 1986 (roughly) I had to write date/time routines. I finished coding and ran the unit tests, only to see them fail consistently. Turned out that the code was fine; my mistake was using the test data in "Inside Macintosh" (at the time, the best source I could find) which had "May 12 1984" as a Wednesday. I still think very bad thoughts about whoever wrote that . . .

Probably because 05/12/1984 (5 December) IS a Wednesday.


Someone got pwned by date formats...

Re: A Waste of Time

2009-06-08 10:39 • by Mr B
And hopefully, by the time the next February 29 rolls around, someone else will be there to fix it.


Yes, yet again, complain about something, do NOTHING about it, and leave it to someone else. Which is exactly why code like this exists.

a) someone weak creates it
b) someone with NO BALLS WHATSOEVER finds it
c) said person with NO SPINE reports to hilarious internet site, taking about as much time and energy as it would to probably FIX the original problem
d) someone like me says "that's why there are so many WTFs, it's because the shit developers are the only ones with spines".

:(

Re: A Waste of Time

2009-06-08 10:42 • by The_Assimilator (unregistered)
267787 in reply to 267785
*more

Actually I think this says it all:

#include <time.h> // Includes base header time.h

Protip: if you feel the need to comment an #include line, but not the actual contorted and horribly broken logic you've rendered unto your text editor, you may not be a good programmer.

Re: A Waste of Time

2009-06-08 10:51 • by Zapp Brannigan (unregistered)
267788 in reply to 267786
Mr B:
And hopefully, by the time the next February 29 rolls around, someone else will be there to fix it.


Yes, yet again, complain about something, do NOTHING about it, and leave it to someone else. Which is exactly why code like this exists.

a) someone weak creates it
b) someone with NO BALLS WHATSOEVER finds it
c) said person with NO SPINE reports to hilarious internet site, taking about as much time and energy as it would to probably FIX the original problem
d) someone like me says "that's why there are so many WTFs, it's because the shit developers are the only ones with spines".

:(
Ah Leeeeerooooy Jennnnnkinnnnnns!

Re: A Waste of Time

2009-06-08 10:52 • by kastein
267789 in reply to 267763
buja:
*puts c++ beard on*

- no include guards, which is especially great when using so many #defines

- expects that someone puts a global "using namespace std;" before including time_help.h (or spreads specific using directives like "using std::cout;" etc. around)

- no "inline" linkage, so multiple source-files #including time_help.h will result in multiple definition errors

- mixing floating point with integer calculations without proper cast and relying on compiler specific behaviour

- "#define SECpYEAR 31536000"
some lines later
"#define YEAR time(NULL)/31536000+1970"
--> yay for still using magic numbers
--> double yay for for not putting the definition in round braces

Oh that code is so great. But regarding that it's not even 2^8 lines long, why not just rewrite instead of fixing?

You're trying too hard. PS it's a code snippet, include guards are boring and thus not included ;)

... seriously though, a lack of parentheses around a macro is just BEGGING for trouble. For extra fun, make macros with unbalanced braces, parentheses, and doublequotes in them... and perhaps throw in a return and/or semicolon occasionally.

Re: A Waste of Time

2009-06-08 10:58 • by buji (unregistered)
267790 in reply to 267763
buja:
*puts c++ beard on*

- no include guards, which is especially great when using so many #defines

- expects that someone puts a global "using namespace std;" before including time_help.h (or spreads specific using directives like "using std::cout;" etc. around)

- no "inline" linkage, so multiple source-files #including time_help.h will result in multiple definition errors

- mixing floating point with integer calculations without proper cast and relying on compiler specific behaviour

- "#define SECpYEAR 31536000"
some lines later
"#define YEAR time(NULL)/31536000+1970"
--> yay for still using magic numbers
--> double yay for for not putting the definition in round braces

Oh that code is so great. But regarding that it's not even 2^8 lines long, why not just rewrite instead of fixing?



*C++ beard on again*

Oh I have forgotten those:

- C <time.h> instead of C++ <ctime>

- expects someone to #include <iostream> etc.

And as for my initial comment about that time_help.h expects someone to put using declarations before #inclusion: This might be a false assumption. It could also that it just expects someone to write code like

namespace std {
#include "time_help.h"
}

and because of that maybe, the author has #included time.h instead of ctime, because basically ctime is time.h inside namespace std.

Re: A Waste of Time

2009-06-08 10:59 • by Still Not Tim (unregistered)
267791 in reply to 267787
The_Assimilator:
*more

Actually I think this says it all:

#include <time.h> // Includes base header time.h

Protip: if you feel the need to comment an #include line, but not the actual contorted and horribly broken logic you've rendered unto your text editor, you may not be a good programmer.


I would suggest a comment on an include line indicates that line - among others - was copy-pasted from some form of "beginner's tutorial"...

Re: A Waste of Time

2009-06-08 10:59 • by buji (unregistered)
267792 in reply to 267789
kastein:

You're trying too hard. PS it's a code snippet, include guards are boring and thus not included ;)


But the full file has been linked over in the initial post.

Re: A Waste of Time

2009-06-08 11:01 • by iToad (unregistered)
If this is a sample of the code, then the application is probably more of a train wreck instead of a car crash.

Re: A Waste of Time

2009-06-08 11:07 • by GettinSadda
267794 in reply to 267786
Mr B:
a) someone weak creates it
b) someone with NO BALLS WHATSOEVER finds it
c) said person with NO SPINE reports to hilarious internet site, taking about as much time and energy as it would to probably FIX the original problem
d) someone like me says "that's why there are so many WTFs, it's because the shit developers are the only ones with spines".
e) ???
f) Profit!!!!!!!!!111111ONEUNOELEVENTYONE
FTFY

/me runs for cover

Re: A Waste of Time

2009-06-08 11:09 • by UpNDown (unregistered)
Cool, uncopyrighted code! So I can take this and use it for my job!

Just have to fix that August bug and add some parentheses around the define expressions and we're ready to go.

Thanks, DWTF!

Re: A Waste of Time

2009-06-08 11:13 • by java.lang.Chris;
267796 in reply to 267763
buja:
*puts c++ beard on*


It's probably C - single line comments with // are no longer C++ only and it's including time.h rather than ctime.

buja:
- no include guards, which is especially great when using so many #defines


The preprocessor in all the compiler suites I'm aware of cache includes and don't parse them twice. Some people would argue that include guards are just noise (personally I do use them, but that may be more out of habit).

buja:
- expects that someone puts a global "using namespace std;" before including time_help.h (or spreads specific using directives like "using std::cout;" etc. around)


Again, this might be C rather than C++.

buja:
- no "inline" linkage, so multiple source-files #including time_help.h will result in multiple definition errors


I'll let my compiler inline for me where it thinks it's suitable. Most attempts to game the optimising passes of a decent compiler lose.

buja:
- mixing floating point with integer calculations without proper cast and relying on compiler specific behaviour

- "#define SECpYEAR 31536000"
some lines later
"#define YEAR time(NULL)/31536000+1970"
--> yay for still using magic numbers
--> double yay for for not putting the definition in round braces

Oh that code is so great. But regarding that it's not even 2^8 lines long, why not just rewrite instead of fixing?


Those are definite WTFs, and as you say it looks eminently fixable. It's probably Alex fictionalising things as usual - the submitter probably did fix this.

Re: A Waste of Time

2009-06-08 11:13 • by DeLos
267797 in reply to 267770
SR:
Coward:
I see the bug! Sugust has 31 days not 30!:
else if ( days <= FEBLENGTH+31+31+30+31+30+31+30)

{
return 8;
}


Sugust only has 4.5 days ;o)


Lousy Smarch weather!

Re: A Waste of Time

2009-06-08 11:17 • by chicago fan (unregistered)
267798 in reply to 267771
snoofle:
*musical interlude*
Does anybody really know what time it is?


It is 25 or 6 to 4.

Re: A Waste of Time

2009-06-08 11:18 • by GettinSadda
267799 in reply to 267796
java.lang.Chris;:
buja:
*puts c++ beard on*
It's probably C - single line comments with // are no longer C++ only and it's including time.h rather than ctime.
time_help.h:
void printdate_function(int format)
{
if ( format == 0 )
cout<<YEAR<<"/"<<MONTH<<"/"<<DAYINMONTH;
else if ( format == 1 )

My! C has changed since I last used it!

Re: A Waste of Time

2009-06-08 11:20 • by java.lang.Chris;
267800 in reply to 267799
GettinSadda:
java.lang.Chris;:
buja:
*puts c++ beard on*
It's probably C - single line comments with // are no longer C++ only and it's including time.h rather than ctime.
time_help.h:
void printdate_function(int format)
{
if ( format == 0 )
cout<<YEAR<<"/"<<MONTH<<"/"<<DAYINMONTH;
else if ( format == 1 )

My! C has changed since I last used it!


Gah. I hadn't clicked on the link, and was going on what was included in the article body. Apologies.

Re: A Waste of Time

2009-06-08 11:21 • by Ethan Qix (unregistered)
267801 in reply to 267785
The_Assimilator:
SCB:
dpm:
Mark V Shaney:
Most in-house time libraries I've seen were broken. Including the ones I wrote myself.

A fun one was detecting the numbers of days between two dates by asking the OS-provided library the number of seconds since a fixed date, and dividing the difference by 86400.

Worked like a charm... until daylight savings kicked in...
In 1986 (roughly) I had to write date/time routines. I finished coding and ran the unit tests, only to see them fail consistently. Turned out that the code was fine; my mistake was using the test data in "Inside Macintosh" (at the time, the best source I could find) which had "May 12 1984" as a Wednesday. I still think very bad thoughts about whoever wrote that . . .

Probably because 05/12/1984 (5 December) IS a Wednesday.


Someone got pwned by date formats...


Heh. MMDDYY dates FTW. Why the hell did someone create this retarded, mixed-up format to begin with ? :p

*dodges tomatoes*

Re: A Waste of Time

2009-06-08 11:21 • by dpm
267802 in reply to 267782
SCB:
dpm:
Turned out that the code was fine; my mistake was using the test data in "Inside Macintosh" (at the time, the best source I could find) which had "May 12 1984" as a Wednesday. I still think very bad thoughts about whoever wrote that . . .

Probably because 05/12/1984 (5 December) IS a Wednesday.
That's a coincidence, yes, but the book explicitly said "May 12 1984 is a Wednesday" in their examples of date/time coding.

Re: A Waste of Time

2009-06-08 11:29 • by Micah (unregistered)
267803 in reply to 267796
java.lang.Chris;:
buja:
*puts c++ beard on*


It's probably C - single line comments with // are no longer C++ only and it's including time.h rather than ctime.


You must've missed the call to cout then.... Or I missed when cout was added to C.


Re: A Waste of Time

2009-06-08 11:30 • by Grimoire
267804 in reply to 267790
buji:
buja:
*puts c++ beard on*

- no include guards, which is especially great when using so many #defines

- expects that someone puts a global "using namespace std;" before including time_help.h (or spreads specific using directives like "using std::cout;" etc. around)

- no "inline" linkage, so multiple source-files #including time_help.h will result in multiple definition errors

- mixing floating point with integer calculations without proper cast and relying on compiler specific behaviour

- "#define SECpYEAR 31536000"
some lines later
"#define YEAR time(NULL)/31536000+1970"
--> yay for still using magic numbers
--> double yay for for not putting the definition in round braces

Oh that code is so great. But regarding that it's not even 2^8 lines long, why not just rewrite instead of fixing?



*C++ beard on again*

Oh I have forgotten those:

- C <time.h> instead of C++ <ctime>

- expects someone to #include <iostream> etc.

And as for my initial comment about that time_help.h expects someone to put using declarations before #inclusion: This might be a false assumption. It could also that it just expects someone to write code like

namespace std {
#include "time_help.h"
}

and because of that maybe, the author has #included time.h instead of ctime, because basically ctime is time.h inside namespace std.


A few more to add for you:
- dateformat and YEARSCOPE should be declared extern to avoid multi definition errors during link
- using magic numbers instead of enumerations
- outputting to cout instead of taking in a stream to output to
- using all caps to indicate macro defines AND global variables...sometimes
- #include <time.h> // Includes base header time.h
Yes, I know what this line does. So does every C/C++ developer on the planet. Please don't put a comment telling me what it does.
- expecting NULL to be defined
- appending _function to every function name.

And that is without even considering the actually algorithms in use. Probably plenty more. Ugh, this is indeed a disaster!

Re: A Waste of Time

2009-06-08 11:41 • by Yazeran (unregistered)
267806 in reply to 267802
dpm:
SCB:
dpm:
Turned out that the code was fine; my mistake was using the test data in "Inside Macintosh" (at the time, the best source I could find) which had "May 12 1984" as a Wednesday. I still think very bad thoughts about whoever wrote that . . .

Probably because 05/12/1984 (5 December) IS a Wednesday.
That's a coincidence, yes, but the book explicitly said "May 12 1984 is a Wednesday" in their examples of date/time coding.

Well yes, but it may be that the original manuscript said 05/12/84 and the proof-readers/editors changed it to the more user friendly "May 12'th" before publication.... date format WTF indeed!

Yours Yazeran

Plan: to go to Mars one day with a hammer

Re: A Waste of Time

2009-06-08 11:43 • by pitchingchris
267807 in reply to 267765
Man of Steel:

Managertip: The return on investment of fixing the bug now is zero.


Anything that prevents a future issue and loss of time/money/resources can be seen as a ROI in my book. The bug can be fixed in a couple hours, and would pay dividends as well as avoiding the case where they use "certain" parts of it that worked. They can fix the bug and hopefully make all parts work correctly in less than a day.

Re: A Waste of Time

2009-06-08 11:48 • by Green (unregistered)
267808 in reply to 267804
Grimoire :
- expecting NULL to be defined

NULL is defined in ctime.h

Re: A Waste of Time

2009-06-08 11:58 • by Grimoire
267811 in reply to 267808
Green:
Grimoire :
- expecting NULL to be defined

NULL is defined in ctime.h

I think you mean time.h, and yes, it appears to be defined there. Never knew that. Thanks.

Re: A Waste of Time

2009-06-08 12:00 • by MadtM (unregistered)
267812 in reply to 267788
[quote user="Zapp Brannigan"][quote]Ah Leeeeerooooy Jennnnnkinnnnnns![/quote]

Thank You! I always speculated on the name of that Design Pattern.
« PrevPage 1 | Page 2 | Page 3Next »

Add Comment