- 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
The code populates a 24-element array of integers with "month" numbers, spanning a two-year period from July to June and starting at 7 (July) of the first year. It can be replaced with one line of code as follows:
int[] months = { 7, 8, 9, 10, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6 };
Admin
my eyes! the goggles! they do nothing!
Admin
ohhh come on, no way that is in a real app, it seems more like an excersive on a test that says the output is __________
Admin
Hmm, let me guess. There must be an explanation...
You know... Your one line code uses magic numbers, so it looks very amateurish. Everyone knows that using magic numbers is deprecated. The original code is free from that problem.
Admin
I only wish ;-).
Only real-world, production code need apply here. Exceptions are certainly made, but they're duly noted in the post.
Admin
It's real. I found in some code on a real project.
Admin
And if you still want to do it in code:
for (int i = 0; i != 24; ++i) { months[i] = 1 + ((i + 6) % 12); }
(Not tested at all. Of course.)
Admin
Actually, no the original code has several acane magic numbers. The original code is completely free from the problems of clarity, simplicity and efficiency, though.
I hope this reply was meant to be sarcastic because that kind of one-dimensional adherence to misunderstood advice is a huge problem in the software industry. I've seen some of the worst code written in the name of structured programming, design patterns, etc.
Admin
You mean you guys don't get paid by the kloc? [:D]
Admin
I'm sorry, but populating an array in your one-liner doesn't scale.
The equivalent code would be:
int[] months = new int[24]; for (int i = 0; i < months.length; i++) { months[i] = (i + 6) % 12; }
Of course, rather than using a lookup table for such a simple operation, it would be better to do
int getFriggedMonthNumber(int nonFriggedMonthNumber) { return (nonFriggedMonthNumber + 6) % 12; }
replacing variable names to reflect WTF the code is supposed to actually be for, of course...
Admin
Oh god thats so fucking stupid.
Admin
The above has the added bonus - if you consider it that way - of giving you a 2 year array of months starting at any given month with only trivial modifications... But then, we all know (esp. after reading this blog for a few months) that Math Is Hardtm
Admin
In Perl, this can be
@months = (7..12, 1..12, 1..6);
Admin
With such frequent use of foul language like "cnt", its no wonder there are so few women in this field
Admin
I completely agree. The extra-special lucky bonus wtf here is that a lookup table was created when an inline function or a macro would do.
Lookup tables are more for string lookups (for easly portability of applications to other languages, and to correct spelling) and speed-critical shortcuts in assembly programs (ie, replacing floating-point sin/cosine with a 256-degree pre-calculated integer lookup so you can use unsigned bytes as degrees, multiply an integer fraction, then shiftdown)
Admin
..and today in pre-school we will be teaching the alphabet starting from M working outwardly by alternating left-right characters until reaching A and Z. WTFHIT?
Admin
LOL! [:D] Nice Simpson's quote.
A agree....just wow.
Admin
Actually talking about school, they often have applications where calendar year starts in July or August, as this is the normal start of a school year. I'm working in a school board and we actually have to display a full year calendar that way.
Admin
<FONT face=Arial>That's pretty special!</FONT>
<FONT face=Arial>However, a better way of doing it would be to put those values in a database, then pull them out as needed with some nice Javascript.</FONT>
Admin
Missed the quoting for my school calendar year post right above.
Admin
There is nothing quite like engaging a college professor during a classroom exercise as to the current value of 'cont'. Especially when his poor English prevented me from gracefully retracting the question. Yup, some things will just scar a man. I have never named a variable 'cnt' or 'cont' since.
Admin
Surely then the general form as a loop is much simpler to come up with
m = 24 # number of months
s = 7 # starting month
list = array [1..m] # output
for i = 1 to m
if s > 12 then s = 1
list[i] = s
s = s + 1
end for
Admin
(Big sigh)... Wow, definately must be paid by the line of code. Unless he knows something we don't like the order of the months may change. [:P]
Admin
Trivial, and hilarious at the same time -- but am I really the first one to point out that the "proper" one liner solution proposed does not match the description above ?
int[] months = { 7, 8, 9, 10, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6 };
Missing an 11 there eh ?
I can only assume it's a typo because there are only 23 elements. shrug
Y'all are fools for missing that. :-)
Admin
I worked at a company that used the variable "NDK8R" for a loop counter everywhere. It took me a long time to figure out that it stood for "indicator".
Nothing like confusing people for no reason.
Admin
Except that this is Java or C# where there's no such thing as inline functions or macros.
I very minor code enhancement, though, replace:
months[i] = cnt;
cnt = cnt + 1;
i = i + 1;
with
months[i++] = cnt++;
(God, how I hate the x = x + ... syntax. I'll never use a language that doesn't have +=)
Admin
The WYSIWYG text editor this forum uses is the FreeTextBox. However, there's another one just like it, called the "FCK editor" (name from the author's initials)
Admin
Reminds me of a joke told by Freddy Prinze (Sr.) way back in the '70's. His Puerto Rician-born mother was a very big fan of Columbo, and wanted to meet Peter Falk. When Freddy's TV show took off, he was able to arrange it, but he caution her, "Don't go wild and embrass me, and whatever you do, don't call him 'Columbo' --- he hates it when people do that". When Freddy brought her into the room with Falk, she immediately shouts, "Hey Columbo! I LOVE You!". Later, Freddy chastised her, "Why did you call him 'Columbo'? I told you not to". She replied "With my accent, I'm gonna take a chance with his last name?" (*)
(*) The story does lose a bit without Prinze doing his mother's accent.
Admin
A couple of the comments in reply to this had off-by-one errors (which I also call Obi-Wan errors -- "these are not the integers you're looking for"), but never mind that.
I'm just wondering: is there a language in common use that allows automagic caching of immutable values? So you'd write something link this:
immutable int months(int i)
{
return 1 + (i+6)%12;
}
... and it would compile as something like this:
int _months(int i)
{
static int _result[];
if (!defined _result[i]) {
_result[i] = 1 + (i+6)%12;
}
return _result[i];
}
That would be an interesting opportunity for optimisation.
Admin
Functional languages (e.g. Haskell) usually evaluate function calls at most once. Although the implementation is nothing like your pseudo-code.
Admin
That's called memoization. As another poster mentioned, it's used a lot in functional programming, but it does occasionally turn up elsewhere. If you google for memoization you should find more than you ever wanted to know about it.
Admin
With Perl it's easy:
<FONT face="Courier New"> use Memoize;
memoize('slow_function');
slow_function(arguments); # Is faster than it was before</FONT>
<FONT face=Arial>It works by comparing the arguments passed to a table of arguments it already got before, and only calling the function for arguments it has not seen yet. It wraps the function at runtime by changing the symbol table, so the rest of the code does not have to be changed.</FONT>
Admin
http://validator.w3.org/check?verbose=1&uri=http%3A//www.thedailywtf.com/
Admin
Ahem... normal if you are in the US, maybe, but everywhere else in the world (AFAIK) normally has their calendar start at the beginning of the year... schools included. Please be aware of US regionalisms when making sweeping statements.
Admin
school year here in the uk is september-july (august is the middle of the summer holiday)
Admin
Yeah, you tell 'em, Taryn! It's a big world, and you can't expect people on a web forum like this to all know each other and know what they're all talking about! Why, the chances against that are astronomical!
(Hey Taryn, weren't we just talking about how easy it is to identify some people just by their names? Spooky, huh? See you on the Shambles!)
Admin
Ah, that was it - memoization! I knew I'd seen it somewhere. Really wish I had it in Delphi sometimes.
Admin
Not necessarily. I work for an edu in the UK and our calendar starts from August. I have a bunch of inline SQL functions and other goodies for working out that week 1, for me, is actually week 22 or 23 on the calendar.
Admin
That's nothing. At one place I worked, all dictionaries were prefixed with "dic". Using a dictionary to track data inserted into tables was always fun.
Admin
There's another gotcha in this, assuming the code is Java - if the rest of the code uses the months to relate to an instance of java.util.Calendar it will have strange results, because the MONTH field in Calendar is zero-based.
Admin
This reminds me of my old COBOL days in Decatur, Illinois. To track rail cars, the code had a flag that said whether or not a car was in the town of Decatur:
01 In-Decatur-Indicator pic x(01).
Sorry for the COBOL post, I haven't used it in 13 years...
Admin
Now, sometimes the simple math confuses me, but is that months array supposed to be 30 elements?
Admin
Nope. 24. The first loop executes 6 times, the second 12 times, the third 6 times.
Admin
not sure if you're being sarcastic, but the first loop starts at 7.
Admin
In this woman's classroom, "cnt" is definitely pronounced "count". And programs I write for public consumption have it spelled out in full, although the unnecessary letters grieve my lazy programmer's mind.
Admin
Hahaha, that's hilarious, but that's probably because I live in Champaign right now :) I try to piss off the locals by pronouncing it all French like "Décateur".
Admin
Hey all, new here waves someone posted a one for loop to do this, but assuming you wanted to stay with a while loop, this does the trick as well:
int[] months = new int[24];
int x = 7;
int z = 0;
while ( z != 24 )
{
months[z] = x;
if(x == 12)
{
x = 0;
};
x = x + 1;
z = z + 1;
}
Admin
I never realized women teachers visited this forum. I must say I usualy pronounce above variable namt as 'See-En-Tee' or Knut, never as the word inferred from above posts. Then again, I usually just use i for counting...
Drak
Admin
I hope you were joking :P
Admin
in the off-topic-but-relevant-to-suspicious-naming department, it's always been a source of amusement how many people get caught out by legitimate but illicit-sounding domains.
For example, a long time ago I was investigating some illicit web browsing that some HR department had picked up. Having not progressed past a script that simply searched for keywords, the poor guy was in the firing line for his habit of browsing to a dubious site.
The URL?
www.expertsexchange.com