- 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
This was obviously done to make the code portable across platforms. This is a crucial requirement of an enterprise system.
Admin
Not Frist.
Admin
...or to anticipate for when we add an 8th day of the week.
Admin
This is not portable in other languages/countries where the first day of the week may not be sunday..
Admin
Admin
Ummm... WOW! I believe what we have here is a new IdiotSavant<Idiot>();
Admin
Safe to assume DRX and OBL "commonly" have performance issues?
Admin
I especially love how it creates a new hashtable each time. Actually I'm surprised someone who would write this is able to use a hashtable in the first place.
Admin
Obviously, this code was offshored to Turkmenistan:
http://en.wikipedia.org/wiki/Renaming_of_Turkmen_months_and_days_of_week,_2002
Admin
I suspect that this individual must have been looking at source code and attempting to discern its' purpose. Apparently, this individual did not understand what they were doing, or perhaps the hash table might have been replaced with an equally rediculous coding structure... you know, like a REAL ENUM!?
Admin
TRWTF is the hashtable. That data should be stored in xml.
Admin
nah, a sql database with a table for each day, containing only 1 row and 2 columns. then they can Select top 1 * From {0} with {0} = Today.
Admin
Where's Blernsday?
Admin
I have wanted to do something like this but never felt right doing it.
Essentially the thinking goes like this: I need an enum, enum Values { Yes, No, Maybe, NotSure }; ok done.
Now I need to display the names of the enums for the user. Values.GetName() ok Done... err no wait, the "NotSure" should be two words not one. now how do I do that? Hash table (which this is), dictionary? Oh the conundrum here.
Then I go back and go with string resources and load up that resource file via the enum for the proper value. And that is how you get dynamically changing text for different languages, just swap reaource files.
Admin
The real WTF is the comments... they feel the need to explain what they are doing (even though an 12 year old can figure that out), but not why they are doing it.
I guess we will never know.
Admin
Well DUHH! Obviously they are trying to memorize the days of the week! Can you think of a better way than writing arbitrary code to represent what you are attempting to learn?
Admin
This is how: http://stackoverflow.com/questions/4367723/get-enum-from-description-attribute
Google before you do anything, will you?
Admin
Nice, now how do you get different ones for English, Spanish, Russian, Pirate Day and Klingon.
This works well if you don't need to worry about internationalization.
Admin
I'm going to have a go at rewriting this function in one line...wish me luck!!! (I may or may not need it)
C#:
Java:
...I have great faith in the C# one, since I use C# on a day-to-day basis. Java, not so much, I could have botched that...
CAPTCHA: abico - Seriously, the guy who wrote the code in the WTF should use one to crunch numbers, not a computer.
Admin
The best part is that the author actually did DateTime.Now.DayOfWeek; in two parts, but he had the number he wanted.
To even make this kluge work, he had to understand that today.DayOfWeek was a number from 0 to 6...so why didn't he just use the number?
Paid by the line?
Admin
The real WTF is using a hash table when everyone knows a list is faster for collections of 10 or less, right?
Admin
But assuming he didn't know that he could just cast from enum to int, as the original submitter pointed out ( (int) DateTime.Now.DayOfWeek ), why would he choose to load up a hash table every time this is called instead of just using a switch statement? Egads, even a nested if statement would have made more sense.
Bonus points for using String.Format instead of ToString() though. I really liked that.
Admin
Easy, just s/([a-z])([A-Z])/\1 \2/g.
Admin
The real fail is using C#.
Admin
TRWTF is calling ToString() on a string.
Admin
It's not a string, it's an object. The HashTable is not generic.
Admin
Does C# have arrays?
Admin
It's an object, but it's still a string. You just need a cast, not a ToString() call.
Admin
Using (int)DayOfWeek.Sunday is a terrible idea. The underlying values are not guaranteed to be 0..6 starting with Sunday, the aren't even guaranteed to be consecutive, or the same across different versions of .NET!
Relying on the underlying integer values of an Enum is asking for trouble.
That said, I would have written this as switch (DayOfWeek) { ... }instead of this awful mess.
Admin
This sounds like a reasonable guarantee to me:
"The DayOfWeek enumeration represents the day of the week in calendars that have seven days per week. The value of the constants in this enumeration ranges from DayOfWeek.Sunday to DayOfWeek.Saturday. If cast to an integer, its value ranges from zero (which indicates DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday)."
Granted, there is the caveat about only working on calendars that have seven days per week. But I think it's a reasonable assumption to assume that there is no requirement here to handle such calendars.
Admin
But suppose the code is ported to a platform that does not support date/time primitives. Better to wrap it in a function call, then. That way you only have one thing to change.
Admin
It fascinates me that so many cultures have a seven-day week, especially since there is no seven-day trigger for it in Nature (as far as I am aware.) The 28-ish day month has a couple of likely causes, and the year is also easily explainable. But the week? Where did that come from?
Admin
7*4 == 28 is one possible link, but as Wiki points out, that is fairly week (yeah, pun intended, sorry).
Since it is referenced right in the first chapter of the Bible, the idea must have been around and firmly entrenched for ages, long before people started committing ancient myths to written documents.
Curious stuff indeed.
Admin
For key, value pairs collections under 10 items it is recommended to use Dictionary.
Admin
He/she wants a string with the name of day of the week, not an integer:
using System; using System.Globalization; DateTime.Now.ToString("dddd", CultureInfo.CurrentCulture); // Or your favourite culture.
CAPTCHA: facilisi == easy peasy.
Admin
The real WTF is Java.
Admin
No, it's actually the int as a string. For example, I'm writing this on Friday, and if I run the code, the return value of that method is the string "5".
So what this is actualy doing is:
Admin
string dayOfWeekStr = String.Format("{0}", today.DayOfWeek);
This is the best use of String.Format() I've ever seen !
Admin
On modern superscalar processors, the break even point for linear searches is higher than 10. One test showed it was between 64 and 128.
Admin
Y U No(I had to do it) know that 7days week is derrived from 7 days moon phases?
Admin
Admin
No, 7 days in the week derives from creation, when G-d created the world in 6 days then rested on the 7th.
Subsequently when the Israelites left Egypt and were given the Manna, they were told about the Sabbath, on which no manna would fall.
In addition there were more rules placed upon them for this day such that publicly going out to gather wood on this day was a capital offence.
As this tradition has been kept ever since, and given that there are names for each day in a 7-day cycle, the Sabbath always falls on the same named day, and that happens to be the one that is named "Saturday". Thus it is the 7th day in a 1-based calendar, but in a 0-based weekday index it should probably be numbered 0 not 6.
Admin
Admin
TRWTF is that the week starts on Monday :-p
Admin
Number of systems I've worked on that had overcomplicated property file systems for getting display strings: many.
Number of systems I've worked on that were ever actually used in any language other than English: 0.
Be agile.
Admin
In Genesis, it says "one day". Then after that says "a second day", "a third day" and these follow the days.
After 6 it says "The sixth day" not "a sixth day".
and then goes on to introduce the Sabbath...
If you don't believe in creation you have no particular reason to decide on which day of the week is first as it has no logic. But if you do believe in it, then the day biblically referred to as the first day is Sunday.
Of course we know these weren't solar days because the solar system was not created until the 4th day, however G-d allocated Saturday to represent the Sabbath and thus Sunday to represent the first day.
Admin
The only thing you actually achieve with this is to break localization. Which is all good if you want to force Sunday as the first day on people...
Admin
♫ Day enum and me wan' go home. ♪
Admin
I suspect that it's twofold: when various tribes began farming, the regular work becomes very cyclical and time off every seven days is just about right. Various revolutionaries have experimented with longer and shorter weeks, and it never lasts.
And seven itself is an appealing number to religious mystics just figuring out math; because the number is mystical proves that the deity is actually granting the time off, which also makes the deity far more appealing.
Admin
Using a number to represent the day of the week though is normally only an internal thing because numbers work better than names for this purpose when it comes to computing.
I mentioned that as someone who keeps the Sabbath on a Saturday, I think Saturday should be day 0 so Sunday is day 1. So those who like Monday to be day 1 are happy because it is, while Sunday is day 0, as there isn't a day 7 anyway.
It does make some sort of sense to keep Saturday and Sunday together at one of the two ends, and certainly to keep Monday to Friday as a continuous range.
International issues are more likely to happen in working out what is the "next working day" or "trading day" assuming that in every country they work Mondays to Fridays but that would be in the business logic.