in reply to Module Pondering
Masem gave a good answer and I want to extend it by pointing out some other reasons for not using SELECT * FROM foo.
I was puzzling through some old code trying to figure out what was going on. The programmer had used fetchrow_hashref (with the SELECT *) to grab a bunch of data that controls client customization. Later on in the code, I saw that he was doing some more selects to grab display information. I used Data::Dumper to dump the results of his first SELECT and lo, he already had the information! It turns out that we had a poorly normalized database and the data was stored in two different places. The programmer didn't realize this, but the original SELECT * hid from him exactly what fields he was grabbing. The moral? Specifying field names is self-documenting if the fields are named correctly.
Tell me what type of data the following returns:
SELECT * FROM customer WHERE customer_id = 7
You could make some good guesses, but unless you go to the database or print out the return data, you're not going to know. Making self-documenting code is incredibly important and should never be overlooked. Not forcing yourself to jump through hoops to look up data is a good example of the Perl virtue "Laziness".
Another reason for naming the fields you want is the idea that your code should do as little as possible. If you have 30 fields in a table, but only need two, why grab all 30? Yet another good example of "Laziness".
Lastly, ignoring the performance issue, we all know one of the worst problems with hashes:
$total_orders += $salesman{ orders_recieved };
In the above example, 'received' is spelled wrong. If you're not careful in your code, that undef field can evaluate as zero and your total orders are incorrect. That's always been a problem with hashes and you'll find many modules that attempt to rectify this (and this is part of the reason the ill-fated "pseudo-hash" was created).
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
|
---|