• bvs23bkv33 (unregistered)


  • (nodebb)

    The problem of course is that it's a short switch. Anybody with a modicum of self respect prefers a long one.

  • Zach (unregistered)


  • (nodebb)

    That part is hilarious, reinventing the array using a dictionary, and reinventing a dictionary using arrays. Just great!!

  • (nodebb) in reply to bvs23bkv33

    Oh, the horrors of the Cobol of my youth (Cobol-77). Too bad the line breaks did not stay in your code sample.

  • Aaaaaaa (unregistered)

    Short switch, short bus... must be some connection there.

  • (nodebb)

    All of this would be unnecessary if the developer had done just a little research: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay

  • Gumpy_Gus (unregistered)

    Could be worse. I once worked on an app that a certain university paid $7 million real 1995 dollars for. It could support about three simultaneous users (needed to support at least 1000). I happened to notice that whenever they wanted to display a calendar, which was on every page, mostly, the would do SQL queries to get the names of the days of the weeks. (Just in case the names changed in the middle of an on-line session). I just assumed the rest of the app was equally foobarred.

  • snoofle (unregistered) in reply to Gumpy_Gus

    I know how you meant it, but I worked on an internal app that ran at numerous international locations. It had the ability to dynamically switch currencies, date formats and so on between locales. That included on-screen text being presented in the language of the locale. So yeah, it was actually possible for the names of the days of the week/months/etc to change while the application was running.

    The previous developer did something like that.

    I replaced it with overloaded classes: interface DaysOfWeek { public String getDay(int dayOfMonth); }, class DaysOfWeekEnglish implements DaysOfWeek {...}, class DaysOfWeekFrench {...}, etc, and simply replaced all the implementations on-the-fly on a locale-switch.

  • (nodebb)

    I'd rather fight than switch. [bonus points to anyone who can ID the cancer stick brand that used that line]

  • WTFGuy (unregistered)

    IANA JavaScript guy ...

    shortswitch in and of itself might make sense for probing dictionaries where the keys aren't restricted to a known contiguous range of integers. Depending on how Javascript responds to searching for a non-existent key this might be the simplest way at the call site to locate the matching value or determine there's a key miss. We're not in the business of mindlessly wasting CPU cycles, but the truth is that developer brain bytes and ability to read the code fluently during maintenance are far far more important to the business than are CPU cycles.

  • Fernando (unregistered) in reply to WTFGuy

    Yep, you're not. In the case of a key miss, $company.misc.shortSwitch doesn't return anything at all, which means it returns null. $company.time.dayNumToDayName claims to return a string; in this case it too returns null, but there's no signal to the developer reading this code that this is possible.

    That's the same thing that would happen if using a standard Javascript object as a dictionary. That would use a standard idiom with which every JS programmer is familiar and would be substantially faster than this coded iteration.

  • Jim Tonic (unregistered)

    The value in a lookup table doesn't get 'directly accessed'. It's not some magic bag that just instantly yields the thing you're looking up, it has to 'look up' the key. I think JS is smart enough to implement this like a hash table which is usually O(1) in the average case. Still better than shortSwitch's linear search, which has O(n) in the average case.

    Anyway, a simple array index for week names always has O(1), so that's definetely the way to go.

  • o11c (unregistered)

    If you're dynamically creating the object you're using for the lookup - which you often have to for dynamic languages - you're O(n) already.

  • Jinks (unregistered)

    Look on the bright side: Refactoring that code and getting a big speedup is gonna be easy.

  • WTFGuy (unregistered)

    @ Fernando: Thanks for the quickie JavaScript lesson.

    Given that, yup the code is pure WTF all the way down.

  • (nodebb) in reply to o11c

    Dynamically creating the object being used to do the lookup is O(m) (i.e., linear in the length of the key string) rather than O(n) (linear in the number of things being compared against). The difference can be profound.

    Or you could be using integers as keys, which are pretty cheap but nothing like as cheap as a direct array indexing…

  • somebody (unregistered) in reply to Jinks

    Yeah, with this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

    Well, that's what I would have said before I looked up on caniuse and it gave 89.75%. So looks like an array is the way to go (or an object to keep it readable, most JS engines probably optimize those well enough anyway)

  • siciac (unregistered)

    Python has no switch because a dict doesn't do what you think it does. The PEP explains why it doesn't work and why they concluded it offered no real benefit over if-elif chains.


  • pa (unregistered)

    The real WTF is Sunday being the first day of the week instead of Monday.

  • Some Ed (unregistered) in reply to Gumpy_Gus

    I doubt it's the same app, but I recall being asked to fix an app like that, where the web pages were all issuing database queries to get the days of the week. Simply changing the code to use the programming language's own date routines rather than the database's date routines gave most of the speedup they needed.

    To be fair, yes, the rest of the application was of similar uality. (Technically, my keyboard does have issues, but it can do a 'q'. On the other hand, it can't do a letter 'Z'. But the uality joke works a lot better, since 'q' is a more common letter. Btw, Zach, thanks for letting me cut and paste a letter from your name. That was very helpful.)

Leave a comment on “Switching Daily”

Log In or post as a guest

Replying to comment #:

« Return to Article