- 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
What if they add more states?
Admin
Happy now? You heathens can pass in our own list of states, or accept the default from God's Country, or add states as needed.
Admin
Weird. It's almost like there should be something called ENUMs for this exact use case...
Admin
Another weirdness: The abbreviations use the alphabetical order of the full name of the states. Thus AR comes after AZ, IA comes after IN, etc.
Admin
I wonder if the values are in some database somewhere, and this was an attempt to save db interactions. Originally, when someone had been building up the dbo.States table, they forgot Mississippi until the end, and that just is reflected here.
Or, the states are ordered by the amount they spend on education or something where Mississippi finishes last.
Admin
Chicago, LINIAKS
Admin
Mississippi is at the bottom because they rank last in almost every category. In Alabama they have a saying "Thank God for Mississippi!"
Admin
Admin
Not good, get_state_index("LA") would return 0. A bunch of invalid strings would also return a non-error value.
Admin
Baker Island is 81.
http://www.itl.nist.gov/fipspubs/fip5-2.htm
(FWIW, I was using FIPS codes by the late '70s.)
Admin
Doesn't work, as pointed out, because they run into each other so LA will be read as split between what you meant to be two tokens.
However use a separator
Admin
Yes but I don't think you can automatically "unstringify" enums without writing your own code / table to do so. It would make a great language feature if it were done for you (as well as one to stringify them back).
So "AR" would convert to the enum AR.
Admin
Bad idea ... try 'LA' and you may see why ;-)
regards Ole
Hint: 'LA' should return 17 and not 1 (or 0?).
Admin
Except that it would return 0 for 'LA' since that is at index 1 in your string
Admin
Admin
That doesn't necessarily work, depending on the letter occurrence... consider:
return index('MILAIL', 'IL') / 2;
Granted, they aren't in alphabetical order in my example, but this is just to illustrate the possible issue with this method. It would return 0 (1 / 2), not 2 (4 / 2), for Illinois.
Admin
Or, hell, x = ['AL', 'AK', 'AZ',...].index(state); if (x == -1) x = 51;
Admin
Cromwell's one or the 80s rock band?
Admin
-Harrow.
Admin
I hasten to point out that even if they created an instance of the State class, they still couldn't call "get_state_index": it's a private method.
Admin
As whacked out as this list is, it doesn't quite match the fubar found in several US federal agencies.
Last time I worked with the DEA databases, they were still using NB for Nebraska (the rest of the US switched from NB to NE back in 1969).
The Department of Interior also lists a number of tribal areas in addition to states, so the coal mining office uses things like "Navaho - New Mexico" and "Navaho - Arizona" to try to prevent the screw ups where the tribes were not getting their share of the royalties (read Hillerman's book "Sinister Pig" for some more explanations of the situation).
Admin
With this solution, Lousiana's value is 0.5?
Admin
Admin
You need blanks on both ends.
Admin
The great state of DC is actually a district (District of Columbia).
As per Wikipedia (http://en.wikipedia.org/wiki/Washington,_D.C.), purveyor of all true information on the internet, "As permitted by the U.S. Constitution, the District is under the exclusive jurisdiction of the United States Congress and is therefore not a part of any U.S. state."
Admin
Who ever said MI was Mississippi, anyway? MI is Michigan. MS is Mississippi, like the article says.
Admin
Admin
public string StateProvince;
If you explicitly need to reference a state or province in the UI, you're probably doing something wrong. At least that's the smell I get from this type of code.
Dealing with massive amounts of data coming from various sources, I've had to deal with everything, and standardizing it is hard, but wouldn't require this type of code.
The second scenario is a drop down/textbox combo with clean-ish input from a user, but that still wouldn't require hard-coding values in the UI code.
Admin
If it's Java,
Admin
No wait, let me change that:
Admin
But it isn't a number, because you don't do math on it. It just happens to consist entirely of short strings from the set [0-9]. That just happen to be in consecutive numerical order.
Admin
I'm glad I'm not the only one thinking this.
Admin
It isn't, it's C#. You can tell because string is all lowercase.
Admin
It could be C++... I can't see anything there that would be invalid in C++, and the use of underscores instead of camel case points towards a more Unixy programmer.
Admin
I hope most of these people responding don't read program requirements like they read comments. It seems like 9 out of 9 comments are saying the same reason why Larry's funny solution doesn't work and the other half are proposing other broken solutions.
Admin
Admin
Admin
Admin
What? 2^6 is 64, so a binary search does it in 6 comparisons maximum, 5 average.
But I'm surprised that nobody has noticed TRWTF: representing the states as numbers. If you're not going to do arithmetic on them, they are not numbers and you shouldn't use numbers to represent them. Shouldn't that be obvious to every TDWTF reader?
Admin
It's returning an index, presumably to some array of data for each "state". Is that so wrong?
Admin
return $A eq'AL'?$X++:$A eq'AK'?$X++:$A eq'AZ'?$X++:$A eq'AR'?$X++:$A eq'CA'?$X++ ...;
Admin
That seems like a WTF all its own. Why do we have a set of standard two-letter state/territory abbreviations, AND ALSO a set of standard two-digit codes for the same list? Why do we need two primary keys for the same list?
Also, I'd quibble with your reference to "all the territories that could someday become states". How do you know that new states or territories might not be added someday in the future, or that existing territories might not be split or combined in whatever politics goes on in becoming states? And where are Tranquility Base and Ceres Colony on the list of potential future states?
Admin
Admin
As the example doesn't show us where this number is used, it's hard to say. If, say, they're using this function to convert a state abbreviation into a number so they can use it to index into an array in which they are keeping data about each state, that could be perfectly reasonable.
Admin
Thanks for bringing it up - I was thinking "Jefferson" or "Jackson" - whatever some folks are planning in the Pac NW.
/captcha: dolor: pain
Admin
So someone misspells Kansas as KA and your function returns 1.5 as the index?
Admin
I'm not too disturbed by the if/then return/else structure. As others have said, it's only 51 tests max and no overhead, probably not all that much slower than a hash lookup or a binary search. If I get really bored maybe I'll run a test both ways and get some empirical data.
But 51 as the error value annoys the heck out of me. The presence of Puerto Rico indicates it's not strictly states. What happens if they decide they need, say, DC and Guam? Then we get 0 - 50 = valid, 51 = error, 52 - 53 = valid. Yuck. Or the programmer will decide that he should insert DC and Guam at 51 and 52 and make the error value now 53. And then he'll miss one call when he changes this, and mysteriously all values for DC will be treated as errors, but just in one function.
Admin
This isn't any worse than the sites that hard-code a massive list of cities/states/countries into a <select> tag's options. I see that all the time. It makes me wonder if someone was paid per line, just like all these super-epic cascading ifs and massive switches do (granted, massive switches are sometimes the only option, sort of like running into those speed barrels or ramps because your brakes are out).
Admin
I generally object to hard-coding lists, and I would almost certainly have put this in a database. But as lists go, this one is pretty stable. I think the last time we added a new state was 1959. It's not like this is highly dynamic. If the US annexes Vancouver or Isfahan as the 51st state, I'm sure we can take the time to update the table, recompile, and redeploy.
Admin
Get of my lawn you youngling state.