- 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
As long as state abreviations are unique they would be excellent candidates for a primary key. I don't see the point to add an extra column just for that. I use it for country abreviations too.
Hardcoding states or countries is never a good idea tho! :) I hope that the US don't add or lose any states in the future.
The code looks like perl, but in that case @data[0] should be $data[0]... !?
Wow... my first FIST ever! :D
Admin
What. The. Fuck.
Admin
I think that this is designed to display a customized list of states. If you're contacting about a product available only in Texas and Utah, only those will be shown. So there is a flag for each state to indicate whether it should be shown or not.
I agree that this is strange way of doing it but at least it's readable. And having readable Perl code is already a good start.
(I remember a job where one of the first sentences of the Perl developer I was replacing was "I put all the code of the application in a single file because I didn't know how to use modules." Of course, it was printing html all over the code...
Admin
It's pulling Database-like data from a file instead of a database! SMART! After all, who would want a database when you can have a nice portable file, am I right, am I left?
Admin
Cmon, he's using flags. Programmers love those, right? And its (sortof) binary data so it must be really efficient, right? :/
Admin
"As long as ..." is the key. Since customers are intrinsically evil, and business logic is naturally in flux, state abreviations WILL change to make them non-unique, and it WILL be popular to use AA instead of AL for Alabama at some point in the future.
The trick it to not think about your data at all, when you decide on your primary key. The PK is not your data, and your data is not your PK.
Admin
The real WTF is that @data[0] works in Perl. I learned something today.
perl -e '@x=qw(foo bar); print @x[0]'
Admin
Yeah it's Perl and the @ probably should be a $ since he's trying pull a scalar, but Perl is flexible and it probably works. Lousy way to code.
Using the postal abbreviation is fine if your app is always and only going to deal with US states/territories. If your app is international, using the international country codes (US, DE, GB, etc) are fine since those are going to be unique. However you may not want a table for each countries states/provinces/terrirtories/regions/etc so a general purpose table would want a numeric identifier for each row since you never know if China may have a OH region.
If you use an Integer it is most likely to only be an extra 4 bytes per row. Remember how much trouble we had with mainframes and Y2K because somebody wanted to save 2 bytes? (ok storage cost alot more back then than it does now).
I'd never advocate going hogwild in future proofing your app, but if it's simple and easy to implement you might as well do it.
Admin
He probably doesn't use warnings; in his Perl code. Otherwise he would get:
Scalar value @data[0] better written as $data[0] at filename line 5.
Admin
It's really weird the way some guys spend their time.
Two points to consider:
1 - What's the problem about using an auto generated PK? We're talking about 50 states only. It is because of space??? Art?? Religion? Philosophy?...
2 - Why is so hard to do things the way normal people do instead of proving your intelectual superiority at each line of code you write?
Really weird...
Admin
@data[1] returns an array with a single item (the value of $data[1]). We all know that in scalar context, arrays have the value of their length.
Admin
The @array[0] syntax is valid because it's how you get array slices in Perl. Technically it's returning a list of 1 value from the given array, rather than returning one scalar value with the $array[0] syntax. It's perfectly valid, but could lead to unexpected results.
@array = (5);
$array[0] == 5; # true $array[0] == 1; # false @array[0] == 5; # false @array[0] == 1; # true
Admin
State abbrs can be fine for primary keys, but if they ever change (I know I know, but Canada did this with the abbrs of their provinces) you'd have a mess.
Admin
Ugh. I don't get these people. If I have to type out a list of the states (let alone the surrounding code) even once, I would stop at about Arkansas and say, "There has got to be a better way."
Admin
Nicely stated. Another good reason to use an integer PK is efficiency with portability. A very intelligent database can build a good index on a two-letter abbreviations. Even a dumb database can build a great index on integers. And you never know when you'll need to change your database vendor.
Admin
Wow...
That's it. Just Wow.........
Admin
Actually, you do not even have to look as far as China. If you mix US states and country codes, you end up with Canada and California both having CA as an abbreviation.
Admin
So use country code and state code? USCA and CACA.
Admin
Odds are he wrote a program to generate the code, rather than typing in all fifty state names several times. There are times when using a program to generate a program makes sense, but it should always set off alarm bells in your head. You should at least consider modifying the program that makes the program into being the program as that often makes a lot more sense.
This is definitely one of those cases.
We're pretty unlikely to need to add more states, but look at the complexity of auditing that code. What if one state got left out of one of the places all the states are listed? Would you catch it? What if one of the state flags was mis-abbreviated in one place? Would you catch it?
Admin
The real WTF is not including APO states for us military folk.
APO (City) States: AE (Armed Forces Europe) AP (Armed Forces Pacific) AA (Armed Forces Americas)
So annoying to find the right product... but the merchant doesnt ship to deployed military.
Admin
But that would probably mean... all of them are always... oh God... oh GOD!
Admin
I only see one state: confusion.
Admin
Admin
God, no! You've begun thinking creatively about your primary key!
I'm not even being sarcastic when I say that you should never have to think about your PK. Remember why you have a PK: It's to index data and to uniquely identify a row in a table. Why would you want to be able to perform concatenations on it? Do you have to split up your PK and have the shattered parts contain semantics? It's the dawn of dev hell.
Admin
The fun thing is that, based on the rest of that code, the file its reading things from looks something like:
Contact Us Form|||||||||||||||||||||||||||||||||||||||||||||FILE_NOT_FOUND|||||||||||||
Okay, so I lied. I didn't count all the |s and FILE_NOT_FOUND could be anything and only its POSITION in that data marks which state you are. I love how they put the string "Contact Us Form" into two different variables, too.
That said, except for this part, at least the Perl is reasonably readable, especially for a WTF.
Admin
Actually, in Perl a list is different than an array and hence your logic is not valid. A list will return the last value of the list in scalar context. (An array, however, will indeed return the size of the array, as you stated) An array slice evaluates to a list, not an array. Hence, the table you posted is in actuality:
@array = (5);
$array[0] == 5; # true $array[0] == 1; # false @array[0] == 5; # true @array[0] == 1; # false
As it should be.. however, using the array slice notation confers a 'list context' on both the array subscript and on any expression being assigned to the slice which could cause unexpected results. (Not to mention the fact that using an array slice in the place of a scalar array access doesn't have the visual cue that you're working with a single value and hence leads to confusion, such as you have experienced above) :)
Matt
Admin
I guess I should also add... TRWTFs are:
(And of course, the WTF-ed-ness of the entire algorithm to begin with, and the aforementioned use of slice notation)
Admin
This reminds me of a code snippet I came across on my last job. I had to maintain a somewhat large Delphi-written ERP. The real WTF was that, even though it was internal development, nobody could tell me exactly what were the requirements for that part of the system. To top it off, its original coder was long gone.
Most of those units were ok, but one day my eyes fell upon something like this:
Not believing my eyes, I checked with my supervisor and he suggested I should leave it like that, but it was up to me to decide. I did some research, talked to users and checked the code, and it became clear that the users could choose any checkboxes except all of them at once or neither at all. A Karnaugh map analysis confirmed it (not entirely, though. One condition appeared twice whilst another wasn't verified)
A few minutes later, the following IF replaced the former:
Too bad I didn't know this site back then. It would be a nice CodeSOD
Admin
Ahhhh...my eyes...they burn...ahhhh
Admin
The real trick is to use the iso standards (3166 and 3166,2 iirc)... then its even compatible with other implementations of the iso standard... horrifying eh?
captcha - I like as a dba = inhibeo ;)
Admin
Use an array of strings, e.g. state = ("Alabama", ... , "Wyoming");
But it's nice to use useless loop unrolling.
Admin
errr.. is it just me or does it appear that there's a bracket missing?
ORIGINAL: if ((arr[0] = arr[1]) and (arr[1] = arr[2]) and (arr[2] = arr[3]) then begin
CORRECTED?? if ((arr[0] = arr[1]) and (arr[1] = arr[2]) and (arr[2] = arr[3])) then begin
idk to be honest .. bt yeah .. =]
Admin