News for hackers. Stuff that matters.
Re:People in your organization that grow legacy code by Kiro on Monday February 24, 2014 @09:06AM (#7290763)
Ok, I'm an idiot but how would you rewrite getDayOfWeek?
Re:People in your organization that grow legacy code by mseebach on Monday February 24, 2014 @09:33AM (#7290938)
1: Should have used a library function

2: not 0-indexed (there can be good reasons for this, but the context of the function should at least suggest the reason)

3: Doesn't handle errors - should throw IllegalArgumentException, and if should do it in the default handler of the switch. If you suddenly see "???" in the UI, it's not easy to track down to this.

4: If for some valid reason this can't be a library function, it should be an enum, and it should be used throughout the code.

    enum DayOfWeek {
       MONDAY(1, "Monday"), TUESDAY(2, "Tuesday") ...

       public int id;
       public String displayName;  // these should be private and have getters according to most style-guides.

       public DayOfWeek(int id, String displayName) {
           this.id = id;
           this.displayName = displayName;
       }

       public static DayOfWeek fromId(int id) {
           for (DayOfWeek d : values()) {
               if (d.id == id) return d;
           }
           throw new IllegalArgumentException("Illegal DayOfWeek id passed: " + id);
       }
    }
Re:People in your organization that grow legacy code by ygra on Monday February 24, 2014 @09:37AM (#7290966)
FWIW, the default Java API to get weekday strings is also 1-based (DateFormatSymbols.getWeekdays(); and Calendar.SUNDAY is 1).
Re:People in your organization that grow legacy code by anton_gogolev on Monday February 24, 2014 @09:51AM (#7291048)
Yeah, with Java DateTime API being royally fubarred [1].

[1]: http://stackoverflow.com/q/1969442/60188

Re:People in your organization that grow legacy code by melignus on Monday February 24, 2014 @10:41AM (#7291342)
... every DateTime API ...

FTFY

Re:People in your organization that grow legacy code by a3voices on Monday February 24, 2014 @10:21AM (#7291228)
In iOS, days aren't 0 indexed either by default. Sunday starts at one.
Re:People in your organization that grow legacy code by Xophmeister on Monday February 24, 2014 @09:20AM (#7290846)
I think the joke is the `getDOWNum` call: `getDayOfWeek` takes a string as its argument, so presumably this is already something like "Monday", thus rendering the function redundant (i.e., equivalent to the identity). However, there's no implication of this anywhere in the code; the input string could be "2014-02-24", for example.

Maybe that's a bit of a long shot. Perhaps there's some standard library function -- I don't know Java, but the author seems to assume the reader does throughout the article -- to turn dates/whatever into day number. Maybe his joke is therefore, "OMFG! What a noob!"

In short: I don't get it, either...

Re:People in your organization that grow legacy code by MAGZine on Monday February 24, 2014 @09:37AM (#7290961)
That could be it. My first glance lead me to think it could all be replaced with an enum rather than 7 switch statements (or, better yet, a library function).

"data" is ambiguous, too. I think that perhaps there are a few errors to demonstrate his point.

Re:People in your organization that grow legacy code by ygra on Monday February 24, 2014 @09:15AM (#7290813)

    calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US)
I guess.

Not that it's necessarily nicer, but generally I'd say duplicating functionality of the standard libraries or frameworks should be avoided if possible.

Re:People in your organization that grow legacy code by msluyter on Monday February 24, 2014 @09:34AM (#7290950)
Well, this gets you the day of the week for whatever the current time is. The example in the article didn't do that exactly. It took a string of some sort (in what format, exactly, isn't clear to me) and mapped it to another string.

Assuming that the input string is a formatted date of some sort, then I think this would work, using JodaTime (minus exception handling):

    public String getDayOfWeek(String s) {
        DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy");
        DateTime dt = formatter.parseDateTime(s);
        return dt.dayOfWeek().getAsText();
    }
Re:People in your organization that grow legacy code by jrabone on Monday February 24, 2014 @09:50AM (#7291040)

  String[] names = { "UNUSED", "Monday", "Tuesday", ... }
  try { 
    return names[i];
  } catch(ArrayIndexOutOfBoundsException uhoh) {
    return "???";
  }
It's so easy...

Look, at least it's not a pile of @Inject DayOfWeekLookupAdapter dayOfWeekLookupAdapter with attendant 100kB Spring XML config, of which the unit tests will have their very own special cut and pasted copy. No, I'm not bitter at all.

Re:People in your organization that grow legacy code by awda on Monday February 24, 2014 @09:36AM (#7290957)
Ok, problem number one: Without looking at the code, what day is number 3?
Re:People in your organization that grow legacy code by dsego on Monday February 24, 2014 @09:15AM (#7290812)
I don't get it either.