- 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
if(state == "AL") return 0x414C; if(state == "AK") return 0x414B; if(state == "AZ") return 0x415A;
Admin
Ding.
Use an enum for the states and then you get an index for free if you ever need one, which you shouldn't.
Ever.
Admin
And then someone searched for Tennessee, and got Montana instead.
Might want to pad your state codes...
Admin
Admin
Drastically. You should create an enum and use that as a key into a map, the value of that key being the data array (or whatever data structure). Storing all your data as arrays is so last millennium. Ignorant pricks and stupid cunts do that sort of thing all the time.
Admin
.Net does this for you, with a .ToString() property of the enum.
Admin
Admin
Java enums have the handy valueOf() method for this purpose.
Admin
Admin
With this method, what happens when he looks for Louisiana, abbreviation LA?
Admin
It's your fault because if I'm using your function, I expect that it would be secure enough to work. Would you want the developers of a 3rd party library to have your attitude?
TRWTF is writing code that is not resilient to odd inputs okok
Admin
Another "too smart for your own good solution" - the code fails miserably with input such as "LA", "KA", "ZA", "ZARCA", etc. I am sure you know that trying to be clever is no substitute for good validation.
Admin
Heh.
And when both "AL" and "LA" return 0, you'll have some fun =)
Admin
This looks to me like yet another of those "store everything as a number" idiocies.
Joe: "Why is state converted to an integer?" Bob [with pitying look]: "Because you don't waste space storing strings in the database. Integers are much more efficient. Duh!"
Admin
Defining the method inside the class is probably preferable to defining it elsewhere. It looks like C# to me, and Visual Studio (and most editors) will let you fold blocks of code if they're getting in your way (albeit, I prefer to unfold everything myself). Or you can use a decent editor (e.g., Vim) and skip right over it. :)
I'm not American (i.e., U.S. resident) so forgive me if I'm missing something, but I assumed the 51 was an unnamed state due to the fact that apparently Puerto Rico is included in the list of states. So it's not necessarily an error. Just an implicit state. Maybe.
(Surely I'm too lazy to read through 3 pages of stupid comments to look for this already being said)
Admin
Defining the method inside the class is probably preferable to defining it elsewhere. It looks like C# to me, and Visual Studio (and most editors) will let you fold blocks of code if they're getting in your way (albeit, I prefer to unfold everything myself). Or you can use a decent editor (e.g., Vim) and skip right over it. :)
I'm not American (i.e., U.S. resident) so forgive me if I'm missing something, but I assumed the 51 was an unnamed state due to the fact that apparently Puerto Rico is included in the list of states. So it's not necessarily an error. Just an implicit state. Maybe.
(Surely I'm too lazy to read through 3 pages of stupid comments to look for this already being said)
Admin
Sweet, double post. I'm pretty sure I only activated the button once... Thanks, TDWTF. >_>
Admin
Nope,
AL == LA == 0
Admin
Another interesting thing is that Visual Studio's Code Analyzer recommends that any Enum have a value at the very beginning for options that don't fit:
And I also like how easy it is to go back and forth:
Admin
In C#, it's more or less the same (if a bit nicer, and much less torturous):
Note: Enum.Parse or Enum.TryParse to convert a string to an Enum in any .NET language. n00bs. ;)
Admin
It's the future (and also a tangent here). :)
Admin
Except for education budget, I thought the same thing. If you wanted to be backwards compatible with some ancient system, you might do something just like this.
Admin
My favorite part is how calling it with an invalid state will return index 52. Of course.
Admin
No, this is future compatibility! When a new state is added, no modifications required!
Admin
Admin
bene there, done that
Admin
I think that the The The record Heartland with the chorus "This is the 51st state of the USA" might have been released before NMA's 51st State. I heard the The The one before I heard the NMA one anyway.
Admin
Storing the states as contiguous numbers should mean you can iterate through them. It's a bit of a WTF in C++ that you cannot properly iterate through an enum wtihout converting to an int and back again. Not sure about other languages.
Assuming you are not short of space but want something a bit faster, either use a table of 676 entries (26*26) or if you can afford it (and probably can) 65536 which will swiftly turn your two-letter code into a number. Within this table you can default info to null and have the data there for those that exist.
What I want to know is how the USA will manage things when they declare their 677th state.
Admin
Ugh. Readability aside, you sure there is no overlapping acronym (e.g. "LA" in "ALAK...LA...")? For wants of being clever, sometimes we do nasty stuff...
Admin
I wouldn't mind if the UK did become the 51st or 52nd or whatever state of the USA if that meant we would all automatically have the rights to work in the USA, where all the cool development takes place.
Admin
It isn't the valid C++ syntax for private.
Admin
That won't work.
There is a state on the list with abbreviation 'LA'.
In your code, this would be index 0.
There might be more such examples.
Admin
Admin
It's fun to see that even the MSDN team has to jump through hoops to loop through enumeration values: http://msdn.microsoft.com/en-us/library/system.enum.getvalues.aspx
Admin
Admin
states: ..., MN, MO,... string: "...MNMO..."
Found "NM" at index 22 ((int)(45/2) = (int)22.5 = 22)
Correct "NM" state at string index 58, state index 29.
OOPS!
Admin
Admin
Admin
Admin
And it shouldn't be static. Because there is a "this" - the class that stores your lookup table.
Yes, it's only changed 37 times...
Admin
What if we are looking for AZ in
WAZAAZ
wrong index returned.
Admin
Iirc, the USPS web site lists about 73 postal abbreviations.
I once had an altercation with a QA manager who maintained that DC was not a valid input for the state field. Because it's a "district" not a "state" facepalm
Admin
That's pretty cool!
Admin
So LA == 1.5?
Try:
return index('AL,AK,AZ,...', $state) / 3;
(Only what happens if $state isn't in the list?)
Admin
Admin
http://youtu.be/-FRm3VPhseI
Admin
Actually, there is a good reason for this method to be static: it's a mathematically pure function, that is to say that it has no side effects and its return value depends only on its parameters and nothing else. It's bad design for such functions not to be static. For an example of this principle in practice, see for example java.lang.Math. (As a corollary, since mathematically pure functions don't influence global state, static does not imply global state. The reason for this is that Java has no way to declare a method side effect free, so any method, static or not, may or may not influence global state.)
Admin
The USPS says there are actually 65 of those two-letter codes, not 50.
https://www.usps.com/send/official-abbreviations.htm
Admin
...And prior to the Treaty of Nerchinsk, the Chinese empire only had one division: The Celestial Dynasty, "All under heaven". Under this system, Mongolia, Tibet, England etc are all historically part of China, just less civilized than the centre.
Admin