- 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
public static List<string> GetMonths(int month) { List<string> monthList = new List<string>(); DateTime tempDate = new DateTime(2010, month, 1); DateTime endDate = tempDate.AddMonths(12); while (tempDate < endDate) { monthList.Add(tempDate.ToString("MMMM")); tempDate = tempDate.AddMonths(1); } return monthList; }
Admin
Admin
TRWTF is the subject. What does the code have to do with SQL?
The code itself is a rather minor WTF. The technique is clumbsy and using mod 13 instead of mod 12 and then adjusting is just plain weird.
Admin
No, I think Alex got us on this one. The WTF is the punchline:
(I'm not the first to notice this. It just took a while for it to sink in). There is not an "un-managed" resource leak here. It's C#. It garbage collects. DUH!!! This WTF, and the c-bitmap WTF from 2 days ago...aren't.And I bet Alex knows that. And I bet he gets a LOT of these. So I think he wanted to share, and sit back and watch the fireworks with a crap-eatin grin...
You got us Alex....now can we get back to TRWTF?
Admin
Like I said, Alex copy-pasted the wrong code sample.
Admin
I agree, TRWTF is not recognizing what the function does, nor that it's probably more readable and clear than taking a precomposed array and generating a new one using some weird array wraparound checks.
This seems like a perfectly nice and legible approach.
Except!
No support for localization!?? In this globalized world, WTF!!!
Admin
Is it possible that the quality of coders is generally improving over time as code reviews and open source projects force review and rework of bad code, so that there are actually fewer instances of egregious WTF-worthy code as time goes on?
Admin
Philip thought
So the WTF is that Philip doesn't realize that he does not know a bit about C#.
Admin
WTF!?! It uses a 13 month list, modulo 13, it's broken for negative numbers, and it's a nice and legible approach?
The nice and legible approach is to use the standard algorithm: modulo 12 AT THE TOP OF THE LOOP.
In the middle of the loop you put a case statement or an array or an object or whatever floats your boat.
It's a passible first attempt for a beginner programmer. That doesn't make it nice or legible. It makes it a dodgy kludge to get month Zero.
Admin
(In Java... Not terribly efficient due to mod. Whatever.)
Admin
I don't know this "C#" you youngsters speak of, but its clear that the code returns an array of month names with the 0-th element set to the requested month. Entirely useless, and probably damaging. The same result could be achieved with the client code using
$month_names[$_month + $i + (-12*int(($_month+$i)/12))];
with $i as the index and $_month as the requested month.
The second part of the story I don't understand, maybe someone can enlighten me. I've often committed changes to dozens of files, where I changed the name of a single function or class to better reflect its duty. This results in a "large commit" where the actual change is in fact very small.
Admin
I cannot possibly count the number of WTFs in the above CSOD.
Admin
Admin
Admin
Admin
http://en.wikipedia.org/wiki/Undecember#Computing[
Admin
This is perfectly normal on an embedded platform that perhaps doesn't have a file system.
Admin
There are no unmamaged resources in that code snippet so I don't see what the last line of the article is talking about. Also, EVE Online. No wait, embedded systems, filesystem, ah shit.
Admin
TRWTF is that someone thought this was a WTF!
Admin
Congratulations! You're the first one to find the WTF!
I was focused on the last sentence "... un-managed resources were disposed of properly ..." since I couldn't see how that related to the article. Turns out it was just a decoy to hide the real WTF which was cleverly hidden right there in the code. Congrats to finding it!
You're the proud winner of todays "Find the WTF" contest.
Admin
What if the user wants to be able to advance the month by a number of months, say if a customer prepays for a number of months of support, and you want to set the expiry date.
If you want to advance 8 months, with this list, you would just press 'down' 8 times.
If you put thie months in the correct order, and start with 'June', you either have to think, and press up 4 times, or press down 6 times, then up 12 times, then down the remaining 2 times.
Without knowing the purpose to which the function was used, we can't say whether the use was a WTF or not. But, apart from the minor wrapping algorithm, it's not really a TDWTF-worthy WTF.
Admin
New bad coders are produced faster then old bad coders get reformed or removed from the pool. It's just that there are no more interesting ways do screw up that are new to the site.
Admin
Admin
ROTFLMAO! Congrats! You win the thread!
Admin
Admin
In Irish embedded development girls are definitely not WTF.
Admin
From my understanding it is a managed object. The Garbage Collector can dispose of it (free it) when it is no longer referenced.
Admin
TRWTF is that somebody thinks that LINQ code is preferable. It's not. It's much more difficult to see what's happening there than even in the original.
Code is supposed to be maintainable, not "Look at how awesome I am at LINQ! I'm so awesome that Junior programmers will NEVER be able to read my code...NEVER!" Bwah, ha, ha, ha, ha (twist mustache).
Admin
This is not the same function: yours don't miss a month when you pass a number higher than 12.
Admin
Admin
Isn't the only real problem this code has is that it can be WRONG?
If you pass in anything outside 0-12 you lose a month due to the switch statement coming before the modulo.
i.e. GetMonths(13) only gives you a list from January-Novemeber. Missing December.
Admin
You still missed it.
As someone else pointed out, for some things you actually do need an array.
There's almost certainly a standard library for the language of your choice that already has the array you want, in the language of the user's choice. So you instead use the standard library call. If you need all twelve months, you make an array of 12 elements, and then populate the elements by looping through the months, modulo 12, and calling the standard library each time. It's not only faster than the given code sample, but it's I18N compliant.
Most of the people here are not trying to fix the problem, but are reveling in it. You're spoiling their fun. That's ok, so'm I.
Admin
I'm very confused. Isn't all that's require just
Admin
Admin
FTFY
Admin
Here! Here! (or is it Hear! Hear!?)
--Former build nazi in hell because I can't find the real issues among the hundreds of deprecated function warnings.
Admin
The real WTF is I don't see the words "fiscal year" anywhere in all these comments (ok, I didn't read all of them, but come on ...). Doesn't anyone work for, you know, a company?
This looks like something that may be helpful for translating any fiscal year back to calendar year.
Admin
This made me laugh.
Convention? To not modify parameters.
Admin
nearly anybody used the chance to create a generic method, so here we go:
var laMonthNames = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames.Take(12).ToList();
// month are 0-index based here, so 6 means July foreach (string lsMonth in laMonthNames.EnumerateRotated(6)) { Console.WriteLine("Month: " + lsMonth); }
// the target is to enumerate, so return an Enumerator public static IEnumerable<T> EnumerateRotated<T>(this IList<T> list, int startFrom) { if (list == null) { throw new ArgumentNullException("list"); }
}