- 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
months[z] = x % 12 is off-by-one
Perhaps months[z] = (x -1) % 12 + 1 is what you were after? But gross... if () statement might be easier to read.
Admin
Oracle PL/SQL knows a deterministic keyword; this is especially usefull if a function is used in a sql-statement, like
select somecolumn from a, b where mypkg.myfunction(a.somecolumn) = b.anothercolumn;
Admin
I wanted to keep in the 'theme' of the original code ;)
Admin
#define MONTHS_PER_YEAR 12
// [snip]
// All numbers are 0-indexed
int AddMonths(const int nStartMonth, const int ncMonths)
{
return( (nStartMonth + ncMonths) % MONTHS_PER_YEAR );
}
And, voila! No magic numbers, no OB1 errors (as far as I can tell), and it's even const-correct.
Although chances are your OS/language may already have functions to do this for you.
Admin
I'm actually in Canada. Close enough to US, but not it. I think it's probably most nothern hemisphere's countries which have a cold winter time between October and April that have the "August to July" calendar for schools. I could easily admit equator and southern hemisphere's countries doing differently. To goal here is to enjoy the warmest time of the year in vacation, which is June-September in most northern hemisphere's countries.
Admin
In J, this can be:
6|.24$1+i.12
The APL would be similar: 6{shift}24{shape}1+{range}12
-The Centaur
Admin
Actually, pretty much anywhere in the world, corporations have their "fiscal year" begin just about anywhere EXCEPT January 1st. And I very much doubt that schools having their calender start at the beginning of the SCHOOL YEAR is in any way US-specific.
Admin
<FONT face=Arial>Any <FONT face="Courier New">for</FONT> loop can be translated into a <FONT face="Courier New">while</FONT> loop: move the declaration out in front, put the increment statement at the end of your loop, leave the condition where it is, and replace "for" with "while".</FONT>
<FONT face=Arial>That transformation is pretty pointless in itself (a deterministic iteration from x to y makes more sense as a <FONT face="Courier New">for</FONT> loop). The only other difference I can see is that you've abandoned the modulo function in preference to that <FONT face="Courier New">if</FONT> statement.</FONT>
Admin
My only point for redoing it as a while loop was to make the assertation that if you were going to do it this way (considering that was the original author's state of mind), there was /still/ a much more simpler intuitive way of doing it. In other words, even if the original author was convinced it had to be done in a while loop with an if statement, it could still have been done in one loop instead of three.
Admin
Most people here seem to be missing the point of the original complaint.
Someone said that their school had a "Calendar year" starting in August.
"Calandar Years" start in January. Period.
If you have a 12-month period starting on any month beside January, you need to use a different adjective with it.
Like "School Year"
Or "Fiscal Year"
Or "Year of Linving Dangerously"
Admin
Up and AT THEM!
Admin
Interesting that people pointed out the one-liner's problems. I submitted the wtf with the explanation for what it did, but I didn't go into the context at all. The first thing to note is that this was for a financial application where the budget is biennal running from July to June, hence the 7. The other, more interesting and yet more WTF!?! thing to know is that this code turned out to only generate the numbers in order to populate a static label in a GUI component. When I finally figured out the context, I just went to the GUI and put in the numbers in the form, and deleted the static array completely.
I did ask, there was some discussion about a desire to dynamically generate the months, but nothing else required it and it was merely a "nice to have" anyway.
Admin
The address to this website has changed to http://www.experts-exchange.com [:D]
Admin
ahem...last time i checked school starts around september and lasts untill about june.
world wide.
Please take note that the US also starts their calandar 'at the begining of the year' which you have so blatently pointed out to us.8-)
Thanks for your helpful information.
Admin
<FONT face=Arial>I guess your definition of "world wide" doesn't include continents such as Australia.</FONT>
Admin
yeah. thats a WTF in itself
Admin
Wow, I hope to god I am never in the position of hiring many of you. (Ahh, ok, you're not as bad as some of my Indian colleagues...)
With regards to:
months[z]=x%12
Thinking this was nice code was a mistake I made in my first 3 months of Java programming. As it is Java, it doesn't really matter, but % is an expensive divide operation. If (x == 13) x=1 will pretty much always be faster than a divide.
Thankfully I had good tutors that explained why % is a fairly unneeded and wasteful in this situation. (Any why in Java it doesn't really matter so much, as it really depends on the implementation of the JVM.)
Swannie
(BTW:
void monthNumbers ( int[] months, int monthsLength, int start ) {
for (int monthNumber = start , int arrayIndex = 0; arrayIndex < monthsLength; monthsNumber++ , arrayIndex++ ) {
if (monthNumber == 13 ) {
monthNumber = 1;
}
months [ arrayIndex ] = monthNumber;
}
}
might not be 100% correct, but you get the idea)
Admin
Wow, I hope to god I am never in the position of hiring many of you. (Ahh, ok, you're not as bad as some of my Indian colleagues...)
With regards to:
months[z]=x%12
Thinking this was nice code was a mistake I made in my first 3 months of Java programming. As it is Java, it doesn't really matter, but % is an expensive divide operation. If (x == 13) x=1 will pretty much always be faster than a divide.
Thankfully I had good tutors that explained why % is a fairly unneeded and wasteful in this situation. (Any why in Java it doesn't really matter so much, as it really depends on the implementation of the JVM.)
Swannie
(BTW:
void monthNumbers ( int[] months, int monthsLength, int start ) {
for (int monthNumber = start , int arrayIndex = 0; arrayIndex < monthsLength; monthsNumber++ , arrayIndex++ ) {
if (monthNumber == 13 ) {
monthNumber = 1;
}
months [ arrayIndex ] = monthNumber;
}
}
might not be 100% correct, but you get the idea)
Admin
Let me be the first to do it in python (not that anyone cares anymore)
Admin
OK, I know I'm a bit late, but this is something that reminds me of an old computer game called "The Incredible Machine", maybe it's is developed by the very same company!!!!
Admin
maybe that is because Australia isn't a continent?
Admin
I really wonder what use has that!
I mean, what's wrong with using a plain calendar?
Maybe it's just me, but if I was to code a school year management application, I would rely on a timeline rather than a custom calendar :P
I can understand computing the first school day, that actually makes sense, but then just use that on a plain calendar - why doing something else?
Am I missing anything?
Andrew
Admin
Not sure I agree with compressing the operation into a single line. In terms of readability and ease of maintenance, I'd prefer to write out the three operations separately (though I agree with using ++ syntax). Especially as either way, the program will still be executing the same instructions, so there's no gain in efficiency.
months[i] = cnt; ++cnt; ++i;
Also, pretty sure Australia is a continent...