• Pippo (unregistered)

    if (state='WTF') return FRIST

  • (cs)

    I'm always glad that here in Holland (The Netherlands for you, geeks...) we only have twelve provinces to code for.

  • Stev (unregistered)

    Oh man...the state of that code...

  • Owain (unregistered)

    Never mind Guam, how about DC?

  • ME2 (unregistered)

    Ok, it's a long list of ifs and you could refactor it to a couple of lines of code but other than that, what's so wtf-worthy about it?

  • cousteau (unregistered)

    REAL programmers don't use lists and indexOf() (or hashmaps)

  • Ol' Bob (unregistered) in reply to ME2
    ME2:
    Ok, it's a long list of ifs and you could refactor it to a couple of lines of code but other than that, what's so wtf-worthy about it?

    It's a long list of ifs and you could refactor it to a couple of lines of code - and it's just so wrong.

    (CAPTCHA: capio - a device used on a guitar to change the key of a song without actually learning how to play the chords. "Those chords are too hard - I'll just capio up 2 frets")

  • Grammarian developer (unregistered)
    ... and call it's .get_state_index() method.
    It should be "its" not "it's".
  • Muzer (unregistered) in reply to ME2
    ME2:
    Ok, it's a long list of ifs and you could refactor it to a couple of lines of code but other than that, what's so wtf-worthy about it?

    Well, apart from the obvious:

    • It's not static, meaning you have to create a copy of the class just to call this function, which has no bearing on the class's own variables.
    • From the looks of the syntax, it's defined inside the class definition... imagine scrolling past that lot.
    • It returns 51 on error instead of a slightly more obvious error code (like -1) - consider writing another function, especially if you're not American (sorry, we don't know how many states your crazy country has) - what was it, 41? 51? You'd have to go back and check.
  • ME2 (unregistered) in reply to Muzer
    Muzer:
    * It's not static, meaning you have to create a copy of the class just to call this function, which has no bearing on the class's own variables. * From the looks of the syntax, it's defined *inside* the class definition... imagine scrolling past that lot. * It returns 51 on error instead of a slightly more obvious error code (like -1) - consider writing another function, especially if you're not American (sorry, we don't know how many states your crazy country has) - what was it, 41? 51? You'd have to go back and check.

    That makes sense.

  • AP² (unregistered) in reply to Ol' Bob

    You'd still need to build the dictionary/map, so the code wouldn't be that shorter.

    I think TRWTF is returning 51 when the value isn't found.

  • Nemo (unregistered) in reply to Muzer

    And the even more obvious: it's private function. So you can create objects all you want, but you still cannot access the function.

  • (cs)

    District of Columbia is also not represented, of course, but the WTF is using linear search and not some kind of map or other efficient lookup.

    (Or constant time in table of just 676 entries of course, converting to upper-case if you desire and if it isn't a letter it isn't found).

    I would use 0 for not-found and 1-N for the found ones, or -1 for not found possibly.

    Either way, you do still have to build the table, albeit you might decide not to "hard code" it and read it from config, just in case the USA declares a new state at some point before your next release.

  • (cs) in reply to Cbuttius
    Cbuttius:
    District of Columbia is also not represented, of course, but the WTF is using linear search and not some kind of map or other efficient lookup.

    (Or constant time in table of just 676 entries of course, converting to upper-case if you desire and if it isn't a letter it isn't found).

    I would use 0 for not-found and 1-N for the found ones, or -1 for not found possibly.

    Either way, you do still have to build the table, albeit you might decide not to "hard code" it and read it from config, just in case the USA declares a new state at some point before your next release.

    This is pretty mild compared to some of the state selectors you've featured in the last few years. You've published some absolute screamers.
  • abigo (unregistered) in reply to Nemo
    AP²:
    I think TRWTF is returning 51 when the value isn't found.
    Nemo:
    And the even more obvious: it's private function. So you can create objects all you want, but you still cannot access the function.

    See the obvious: This is a private function of this class, which means it serves for some well defined purpose. This class knows that 51 is an error indicator. And the numbers do not have any meaning outside the class or even for inheriting classes.

    Not so obvious: Is this really intended or just done mistakenly?

  • (cs) in reply to Muzer

    [quote user="Muzer Well, apart from the obvious:

    • It's not static, meaning you have to create a copy of the class just to call this function, which has no bearing on the class's own variables.

    And it shouldn't be static. Because there is a "this" - the class that stores your lookup table.

    * From the looks of the syntax, it's defined *inside* the class definition... imagine scrolling past that lot. /quote] Unfortunately a feature of languages that don't have header files. However it should be in its own class. You might have a static (dreaded singleton) instance of that class.
    * It returns 51 on error instead of a slightly more obvious error code (like -1) - consider writing another function, especially if you're not American (sorry, we don't know how many states your crazy country has) - what was it, 41? 51? You'd have to go back and check.

    Not found isn't necessarily an error, and they are enumerations. Using an enum would probably be tidier, and a more efficient algorithm for string text to enum. 51 is a perfectly good number of "not found" as an enumeration albeit I would possibly use 0, however it is highly unlikely this list is ever going to change.

    The software is presumably written for internal use and not for international usage so countries that don't have 2 letter states or have a different number of them are irrelevant. Coding for this scenario would be a waste of developer time unless you intend to sell the software.

  • (cs)

    I presume the "WTF" grade here represents "Worse Than an F"?

  • (cs) in reply to Cbuttius
    Cbuttius:
    District of Columbia is also not represented, of course, but the WTF is using linear search and not some kind of map or other efficient lookup.
    This solution is not (much) slower than looking it up in some table. It's only 51 comparisons, whereas an efficient searcher would do, say 7, but there is no overhead. I think you have to run a huuuuuge amount of lookups to get a bit of a difference.
    Either way, you do still have to build the table, albeit you might decide not to "hard code" it and read it from config, just in case the USA declares a new state at some point before your next release.
    A, yes. Perfect. Of course, you'd still have to adapt your code for the index of the new state...
  • (cs)

    Smaller WTF: Not using the the FIPS state codes. That list already has reserved numbers for all of the territories that could someday become states.

  • (cs)

    This is still ugly but better. Not sure if the syntax is correct for this language but you get the point

    enum StateCodeEnum { AL, AK, AZ, AR, ..., StateNotFound };
    private StateCodeEnum get_state_index(string state) 
    {
       if( state.size() < 2 ) 
         return StateNotFound;
    
       switch (state[0])
       {
          case 'A':
            switch( state[1] )
            {
               case 'L': return AL;
               case 'K': return AK;
               case 'Z': return AZ;
               default: return StateNotFound;
           }
            break;
    
          case 'C':
             ...
     
          default:
             return StateNotFound;
       }
    }
    

    Ugly but constant time and uses an enum rather than magic numbers.

    I'd rather something like this in C++ but not sure how feasible it would be in other languages

    private:
      enum StateCodeEnum = { ... };
      const size_t dim = 'Z'+1-'A';
      StateCodeEnumTable[dim][dim] = { StateNotFound }; 
      StateCode StateCodeEnum get_state_index(string const& state)
      {
          // verify length is 2 and both characters are letters
          return StateCodeEnumTable[state[0]-'A'][state[1]-'A'];
      }
    
  • refoveo (unregistered) in reply to Muzer
    Muzer:
    ME2:
    Ok, it's a long list of ifs and you could refactor it to a couple of lines of code but other than that, what's so wtf-worthy about it?

    Well, apart from the obvious:

    • It's not static, meaning you have to create a copy of the class just to call this function, which has no bearing on the class's own variables.
    • From the looks of the syntax, it's defined inside the class definition... imagine scrolling past that lot.
    • It returns 51 on error instead of a slightly more obvious error code (like -1) - consider writing another function, especially if you're not American (sorry, we don't know how many states your crazy country has) - what was it, 41? 51? You'd have to go back and check.

    Surely 51 represents the UK, the 51st State of America.

    plaga

  • (cs) in reply to refoveo
    refoveo:
    Muzer:
    ME2:
    Ok, it's a long list of ifs and you could refactor it to a couple of lines of code but other than that, what's so wtf-worthy about it?

    Well, apart from the obvious:

    • It's not static, meaning you have to create a copy of the class just to call this function, which has no bearing on the class's own variables.
    • From the looks of the syntax, it's defined inside the class definition... imagine scrolling past that lot.
    • It returns 51 on error instead of a slightly more obvious error code (like -1) - consider writing another function, especially if you're not American (sorry, we don't know how many states your crazy country has) - what was it, 41? 51? You'd have to go back and check.

    Surely 51 represents the UK, the 51st State of America.

    plaga

    52nd. Canada is the 51st

  • Jake (unregistered)

    In the old days, this would be inefficient and banned for performance reasons. And it duplicates all those "return"s when it could just "go to" a single return at the end.

    But with prices falling on modern hardware, and the astronomical programmer salaries, readability is more important than performance. You should be able to see at a glance what it says. And this says you need a new programmer.

  • Mike (unregistered) in reply to refoveo
    refoveo:
    Surely 51 represents the UK, the 51st State of America.

    plaga

    My first thought, the real WTF is that the default value is for Great Britain

  • Winston (unregistered) in reply to refoveo
    refoveo:
    Surely 51 represents the UK, the 51st State of America.
    Not true! The UK has cameras everywhere to spy on its citizens. They love the police state and constantly want more! Why the book 1984 was originally written as a criticism of where the UK was headed... and it got everything right except for the date.

    The US, on the other hand...

    Never mind.

  • (cs) in reply to JiP
    JiP:
    [...] in Holland (The Netherlands for you, geeks...) [...]
    What's so geeky about it? I'm not a 'Hollander'. In fact, I've found southern Europe more similar in many ways to the south of the Netherlands than the west of the Netherlands is.

    The west of the Netherlands is the part where 'hospitality' means that you get one (1) dry biscuit (not a cookie) with your cup of coffee.

  • Muzer (unregistered) in reply to Cbuttius

    [quote user="Cbuttius"][quote user="Muzer] Well, apart from the obvious:

    • It's not static, meaning you have to create a copy of the class just to call this function, which has no bearing on the class's own variables. [/quote]

    And it shouldn't be static. Because there is a "this" - the class that stores your lookup table. [/quote] There isn't, though, not in THAT code. I don't really think it should be static, just that the code should save the result into some var (though I don't know what the rest of the state class is like). [quote] [quote]

    • From the looks of the syntax, it's defined inside the class definition... imagine scrolling past that lot. [/quote] Unfortunately a feature of languages that don't have header files. However it should be in its own class. You might have a static (dreaded singleton) instance of that class. [quote]
    • It returns 51 on error instead of a slightly more obvious error code (like -1) - consider writing another function, especially if you're not American (sorry, we don't know how many states your crazy country has) - what was it, 41? 51? You'd have to go back and check.[/quote]

    Not found isn't necessarily an error, and they are enumerations. Using an enum would probably be tidier, and a more efficient algorithm for string text to enum. 51 is a perfectly good number of "not found" as an enumeration albeit I would possibly use 0, however it is highly unlikely this list is ever going to change.

    The software is presumably written for internal use and not for international usage so countries that don't have 2 letter states or have a different number of them are irrelevant. Coding for this scenario would be a waste of developer time unless you intend to sell the software.

    [/quote] Yes, of course it would be perfectly fine - if it actually used an enum. Currently, you just have to remember when writing any function that uses it that 51 = not found. Not good.

    I never mentioned anything about international use.

  • Captcha:consequat (unregistered) in reply to Jake
    Jake:
    And it duplicates all those "return"s when it could just "go to" a single return at the end.

    Won't the end result be exactly the same (unless the compiler is very dumb)?

  • John (unregistered) in reply to Nemo

    I've seen some kludge like this where the "developer" used a property getter to return the value based upon another property (e.g. get {return get_state_index(this.StateAbbreviation)}) - you just hope that they set the referenced field correctly.

  • My name (unregistered) in reply to cousteau
    cousteau:
    REAL programmers don't use lists and indexOf() (or hashmaps)

    This problem is a perfect match for the FOR_CASE paradigm.

  • Singleton or Someting (unregistered)

    This WTF isn't a real WTF. Not in the way it is presented anyway. The method is private, so either it is for some debugging purpose or it is called via a public wrapper, that could just happen to be static.

    ie:

    public class State {
        private State instance = new State();
    
        private int getState(String state) {
            if("NY".equals(state)) { return 1; }
            else if("CA".equals(state)) { return 2; }
            else { return 3; }
        }
    
        public static int stateToInt(String state) {
            return instance.getState(state);
        }
    }
    
  • Jake (unregistered) in reply to Captcha:consequat
    Captcha:consequat:
    Jake:
    And it duplicates all those "return"s when it could just "go to" a single return at the end.
    Won't the end result be exactly the same (unless the compiler is very dumb)?
    Well in the old days we wrote our own compilers, for efficiency, because you could skip the opcodes and features you didn't intend to use. And if you know better than to code all those wasteful returns in the first place, you don't need to write a compiler smart enough to fix your sloppiness for you.

    (Truth: Atari BASIC used two-byte branch instructions instead of three-byte return instructions, to branch to a return point that was shared by several subroutines. Now that's geeky! And even with that, they came within about seven bytes of running out of the 8K available.)

  • (cs) in reply to refoveo
    refoveo:
    Surely 51 represents the UK, the 51st State of America.
    No, it's zero-indexed, so 50 represents Puerto Rico, which the programmer of this considered to be the 51st state.
  • Craig (unregistered)

    Surely instead of returning an error value that isn't much different from a regular value, it should have done

    throw new NotAssimilatedYetException()

  • (cs)

    I don't like all those magic numbers.

    private int get_state_index(string state) {
        returnValue = 0;
        if(state == "AL")
            return returnvalue;
        returnValue++;
        if(state == "AK")
            return returnValue;
        returnValue++;
        if(state == "AZ")
            ... etc.
    :
    :
    :
        returnValue++;
        if(state == "PR")
            return returnValue;
                
        returnValue++;
        return returnValue;
    }
    
  • Gizzy (unregistered)

    When did it become fashionable to say "ho" instead of "ha"???

  • Larry (unregistered)

    return index('ALAKAZARCA...', $state) / 2;

  • Barry (unregistered) in reply to Gizzy
    Gizzy:
    When did it become fashionable to say "ho" instead of "ha"???
    Because a ho is more fun than laughing.
  • BLs (unregistered)

    Including Puerto Rico isn't all that strange... in the banking industry it's been a common theme in most of my jobs. If you want strange consider my first finance company that had two divisions, domestic and international. Not all that unusual unless you consider that Hawaii was part of the international division yet Canada was part of the domestic division.

  • (cs) in reply to Larry
    Larry:
    return index('ALAKAZARCA...', $state) / 2;
    $state = 'LA';
  • proofer (unregistered)

    Everyone missed the obvious. MI (listed twice) is not the abbreviation for Mississippi. So, 51 isn't error, so - quickly - which state is ACTUALLY missing?

  • Larry (unregistered) in reply to pjt33
    pjt33:
    Larry:
    return index('ALAKAZARCA...', $state) / 2;
    $state = 'LA';
    return int(index('ALAKAZARCA...', $state) / 2);
  • Perry (unregistered) in reply to pjt33
    pjt33:
    Larry:
    return index('ALAKAZARCA...', $state) / 2;
    $state = 'LA';
    Los Angeles is a city, not a state. But don't tell them that.

    (Actually, nobody will admit to being from L.A. The closer you get to the center of town, the more obscure subdivisions there are so people can say "I'm from Flagpole Heights" or whatever instead of admitting to being from L.A.)

  • (cs)

    I'm not American but I think LA is Louisiana?

  • (cs) in reply to Cbuttius
    Cbuttius:
    I'm not American but I think LA is Louisiana?
    We've got a fur-ner who knows more about America than the Americans. Not surprising, I guess, considering one of them doesn't see how his code returns 1.5 or 1 for the index of LA.
  • Tomatoman (unregistered) in reply to JiP
    JiP:
    I'm always glad that here in Holland (The Netherlands for you, geeks...) we only have twelve provinces to code for.
    Many US websites require a ‘state’ of ‘province’ field to be filled in whenever a European address is entered. That’s a real WTF, because most European addresses don’t include a province at all. And for those of you who are still unaware: European zip codes aren’t necessarily a five-digit numeric code. Many countries rely on a zip code system that includes letters.
  • (cs) in reply to Sutherlands
    Sutherlands:
    one of them doesn't see how his code returns 1.5 or 1 for the index of LA.
    It doesn't return either of those - it returns 0 (or possibly 0.5 if it's capable of returning a floating point number).
  • Meme (unregistered)

    More over Why convert to an arbitrary index in the first place?

  • (cs) in reply to proofer
    proofer:
    Everyone missed the obvious. MI (listed twice) is not the abbreviation for Mississippi. So, 51 isn't error, so - quickly - which state is ACTUALLY missing?

    The second "MI" is actually "MT" (Montana). I had to bump up the text size to see the difference.

  • (cs)
    but why is Mississippi (MS) floating out of order at the bottom of the list?

    Must be The Weather Channel. During the Hurricane Isaac coverage, they wouldn't say "Mississippi," they'd call it "the land mass between New Orleans and Mobile"--at least until tens of thousands of Mississippians started making fun of them on Facebook and Twitter. Then they decided Miss. was a state after all.

Leave a comment on “Pick-a-State”

Log In or post as a guest

Replying to comment #:

« Return to Article