Comment On Denumerating the DayOfWeekEnum

"When I first found this custom 'enum'," writes Garrett Hopp," I had assumed that my predecessor had re-implemented the DayOfWeek Enumeration. I suppose that wouldn't be that big of a WTF as discovering the built-in DayOfWeek does involve using a search engine... which many developers seem to struggle with." [expand full text]
« PrevPage 1 | Page 2Next »

Re: Denumerating the DayOfWeekEnum

2012-07-26 14:36 • by geoffrey (unregistered)
This was obviously done to make the code portable across platforms. This is a crucial requirement of an enterprise system.

Re: Denumerating the DayOfWeekEnum

2012-07-26 14:42 • by Chuck (unregistered)
Not Frist.

Re: Denumerating the DayOfWeekEnum

2012-07-26 14:52 • by snoofle
...or to anticipate for when we add an 8th day of the week.

Re: Denumerating the DayOfWeekEnum

2012-07-26 15:01 • by paquetted
This is not portable in other languages/countries where the first day of the week may not be sunday..

Re: Denumerating the DayOfWeekEnum

2012-07-26 15:03 • by Chronomium (unregistered)
(int) DateTime.Now.DayOfWeek
Given that the pairs in the hash table are things like ("Sunday", "0"), it's more like:
((int) DateTime.Now.DayOfWeek).ToString()
...which has to be re-int'd anyway given that you probably want to use the result as a number.

Re: Denumerating the DayOfWeekEnum

2012-07-26 15:08 • by Some Jerk (unregistered)
Ummm... WOW! I believe what we have here is a
new IdiotSavant<Idiot>();

Re: Denumerating the DayOfWeekEnum

2012-07-26 15:11 • by C-Derb (unregistered)
Safe to assume DRX and OBL "commonly" have performance issues?

Re: Denumerating the DayOfWeekEnum

2012-07-26 15:12 • by M (unregistered)
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.

Re: Denumerating the DayOfWeekEnum

2012-07-26 15:14 • by wonk (unregistered)
Obviously, this code was offshored to Turkmenistan:

http://en.wikipedia.org/wiki/Renaming_of_Turkmen_months_and_days_of_week,_2002

Re: Denumerating the DayOfWeekEnum

2012-07-26 15:16 • by Some Jerk (unregistered)
385756 in reply to 385754
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!?

Re: Denumerating the DayOfWeekEnum

2012-07-26 15:17 • by C-Derb (unregistered)
TRWTF is the hashtable. That data should be stored in xml.

Re: Denumerating the DayOfWeekEnum

2012-07-26 15:21 • by Some Jerk (unregistered)
385758 in reply to 385757
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.

Re: Denumerating the DayOfWeekEnum

2012-07-26 15:24 • by Leo (unregistered)
Where's Blernsday?

Re: Denumerating the DayOfWeekEnum

2012-07-26 15:31 • by KattMan
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.

Re: Denumerating the DayOfWeekEnum

2012-07-26 15:33 • by DaveShaw (unregistered)
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.

Re: Denumerating the DayOfWeekEnum

2012-07-26 15:49 • by Some Jerk (unregistered)
385763 in reply to 385762
DaveShaw:
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.


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?

Re: Denumerating the DayOfWeekEnum

2012-07-26 15:51 • by abico (unregistered)
385764 in reply to 385761
KattMan:
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.



This is how:
http://stackoverflow.com/questions/4367723/get-enum-from-description-attribute

Google before you do anything, will you?

Re: Denumerating the DayOfWeekEnum

2012-07-26 16:01 • by KattMan
385765 in reply to 385764
abico:


This is how:
http://stackoverflow.com/questions/4367723/get-enum-from-description-attribute

Google before you do anything, will you?


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.

Re: Denumerating the DayOfWeekEnum

2012-07-26 16:04 • by AGray (unregistered)
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#:
public static string getDayEnum()

{
return DateTime.Now.DayOfWeek.ToString();
}


Java:

import java.util.Date;

public static string getDayEnum()
{
return new Calendar.DAY_OF_WEEK.toString();
}


...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.

Re: Denumerating the DayOfWeekEnum

2012-07-26 16:19 • by Coyne
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?

Re: Denumerating the DayOfWeekEnum

2012-07-26 16:22 • by Botia (unregistered)
The real WTF is using a hash table when everyone knows a list is faster for collections of 10 or less, right?

Re: Denumerating the DayOfWeekEnum

2012-07-26 16:39 • by C-Derb (unregistered)
385769 in reply to 385767
Coyne:
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?


Agreed.

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.

Re: Denumerating the DayOfWeekEnum

2012-07-26 17:23 • by Meep (unregistered)
385770 in reply to 385761
KattMan:
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?


Easy, just s/([a-z])([A-Z])/\1 \2/g.

Re: Denumerating the DayOfWeekEnum

2012-07-26 17:30 • by Spewin Coffee (unregistered)
The real fail is using C#.

Re: Denumerating the DayOfWeekEnum

2012-07-26 17:51 • by Rob (unregistered)
TRWTF is calling ToString() on a string.

Re: Denumerating the DayOfWeekEnum

2012-07-26 18:34 • by James (unregistered)
385774 in reply to 385772
It's not a string, it's an object. The HashTable is not generic.

Re: Denumerating the DayOfWeekEnum

2012-07-26 18:55 • by Bill (unregistered)
Does C# have arrays?

Re: Denumerating the DayOfWeekEnum

2012-07-26 18:58 • by Rob (unregistered)
385776 in reply to 385774
James:
It's not a string, it's an object. The HashTable is not generic.


It's an object, but it's still a string. You just need a cast, not a ToString() call.

Re: Denumerating the DayOfWeekEnum

2012-07-26 20:47 • by Mathlete (unregistered)
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.

Re: Denumerating the DayOfWeekEnum

2012-07-26 21:06 • by NitPiC# (unregistered)
385778 in reply to 385777
Mathlete:
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.

Are you sure about that?

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.

Re: Denumerating the DayOfWeekEnum

2012-07-26 22:04 • by geoffrey (unregistered)
385779 in reply to 385767
Coyne:
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?



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.

Re: Denumerating the DayOfWeekEnum

2012-07-26 23:00 • by Maurits
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?

Re: Denumerating the DayOfWeekEnum

2012-07-27 00:37 • by NitPiC# (unregistered)
385781 in reply to 385780
Maurits:
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?

Yeah, that is a bit curious.
I assume you've already read the wiki on the subject

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.

Re: Denumerating the DayOfWeekEnum

2012-07-27 00:47 • by Radu V. (unregistered)
For key, value pairs collections under 10 items it is recommended to use Dictionary.

Re: Denumerating the DayOfWeekEnum

2012-07-27 01:50 • by Errr.... (unregistered)
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.

Re: Denumerating the DayOfWeekEnum

2012-07-27 04:04 • by PHP Vigilante (unregistered)
The real WTF is Java.

Re: Denumerating the DayOfWeekEnum

2012-07-27 04:12 • by Jason (unregistered)
385786 in reply to 385783
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:
((int)DateTime.Now.DayOfWeek).ToString();

Re: Denumerating the DayOfWeekEnum

2012-07-27 04:13 • by Kryptus (unregistered)
string dayOfWeekStr = String.Format("{0}", today.DayOfWeek);

This is the best use of String.Format() I've ever seen !

Re: Denumerating the DayOfWeekEnum

2012-07-27 04:15 • by barleyguy (unregistered)
385788 in reply to 385782
On modern superscalar processors, the break even point for linear searches is higher than 10. One test showed it was between 64 and 128.

Re: Denumerating the DayOfWeekEnum

2012-07-27 04:21 • by wtf (unregistered)
Y U No(I had to do it) know that 7days week is derrived from 7 days moon phases?

Re: Denumerating the DayOfWeekEnum

2012-07-27 04:45 • by English Man
385791 in reply to 385785
PHP Vigilante:
The real WTF is Java.
It's a good job he didn't use it then.

Re: Denumerating the DayOfWeekEnum

2012-07-27 05:19 • by Cbuttius (unregistered)
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.

Re: Denumerating the DayOfWeekEnum

2012-07-27 06:00 • by A. Nonymous (unregistered)
385794 in reply to 385785
PHP Vigilante:
The real WTF is not knowing Java.

TFTFY

Re: Denumerating the DayOfWeekEnum

2012-07-27 06:14 • by Luc (unregistered)
TRWTF is that the week starts on Monday :-p

Re: Denumerating the DayOfWeekEnum

2012-07-27 06:22 • by lmm (unregistered)
385796 in reply to 385765
KattMan:

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.


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.

Re: Denumerating the DayOfWeekEnum

2012-07-27 06:35 • by Cbuttius (unregistered)
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.

Re: Denumerating the DayOfWeekEnum

2012-07-27 06:52 • by Sunday is the last day (unregistered)
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...

Re: Denumerating the DayOfWeekEnum

2012-07-27 07:04 • by shoofle (unregistered)
♫ Day enum and me wan' go home. ♪

Re: Denumerating the DayOfWeekEnum

2012-07-27 07:15 • by Meep (unregistered)
385800 in reply to 385781
NitPiC#:
Maurits:
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?

Yeah, that is a bit curious.
I assume you've already read the wiki on the subject

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.


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.

Re: Denumerating the DayOfWeekEnum

2012-07-27 07:19 • by Cbuttius (unregistered)
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.

« PrevPage 1 | Page 2Next »

Add Comment