Despite the fact that Ruby on Rails has been around since 2004, I'm still surprised and dismayed at how many "Amateurs" (or
non-Passionate Coders as Stephen McMahon recently blogged about) are trying to get into Ruby on Rails.
Much like what Stephen encountered, I seem to be rescuing a lot of Rails Projects recently, with Ruby code made to "look like" an older, more established language.
One recent code snippet I came across was written by a nameless Ruby on Rails programmer in Hawaii. While it's not as bad as what's found in
The Daily WTF (a fine site I highly recommend to any serious Programmer), this is still not the kind of code that Honolulu's Ruby on Rails Coders should be propagating, much less tolerating.
The author of the following snippet seems to have no grasp on the merits of using Collections, as a lengthy hunk of code was custom-written for an e-Commerce site:
case address.zip
when "96701"
local_surcharge = 15
when "96734"
local_surcharge = 15
when "96744"
local_surcharge = 15
when "96782"
local_surcharge = 15
when "96797"
local_surcharge = 20
when "96792"
local_surcharge = 25
# ... all the rest of Oahu's Zip Codes hardcoded with values here...
end
if local_surcharge
total_charges = total_charges + local_surcharge
end
Alrighty, go ahead and cue that
Sam Kinison clip (NSFW) for me, please:
"Oh! Oh! Auuuugh!!!"
Ow, my eyes! Such unsightly code! I thought this was supposed to be Ruby, not Visual Basic or COBOL! I'll bet that even an experienced JavaScript coder would've tried to write the above lookup as a JSON Object or something. Blecch!
If you're writing Ruby code like the above, and consider it perfectly acceptable code, please get out of the game NOW. You're doing nobody any favors by writing such poorly-styled code. In fact, I've fired (dismissed) a few expensive, $300/Hour consultants in Las Vegas for writing unsightly code like that. Thank goodness I was the Technical Lead back then.
If you're writing code like the above through inexperience, though, you might want to invest in some reference materials for your personal library. I suggest The Ruby Way, and The Rails Way as starting points.
Anyway, here's a slightly better way to implement the above, using a Hash Lookup in Ruby:
zip_lookup = {
"96701"=>15,
"96734"=>15,
"96744"=>15,
"96782"=>15,
"96797"=>20,
"96792"=>25,
# ... all the rest of Oahu's Zip Codes hardcoded with values here...
}
# Add the lookup value, or $0.00 if not in lookup hash
total_charges += (zip_lookup[address.zip] || 0)
This is better style for the intended operation, in my opinion, as it's much clearer that only one thing is supposed to happen in the code snippet: determine and apply a surcharge (if any) for specific zip codes. The only acceptable reason to use the original "Big-Ugly Case Statement" would be if several Cases can't easily be grouped into a common, reusable pattern.
Aim for simplicity when possible -- you'll thank yourself 6 months later when you need to go back and update your own code.
Of course, to be a real Rails Rock-Star and have your Inbox flooded with Job Offers -- quite possibly from me -- the above really should have been done as a new Rails Model. At least a "script/generate scaffold ZipSurchargeLookup code:string surcharge:integer". That way, the above is about as simple to implement in Ruby code as this:
zip_lookup = ZipSurchargeLookup.find_by_code(address.zip)
total_charges += zip_lookup.surcharge unless zip_lookup.nil?
Furthermore, by doing the lookup as a Database Table, the ZipCode surcharge can be maintained by the eCommerce site's administrator via Web GUI, rather than maintained by a Programmer spelunking around in Ruby code.
You don't want to be stuck doing menial "Code Maintenance" tasks like updating hard-coded Zip Code Surcharges in low-level Ruby Code, do you? Because if you do, you probably shouldn't be coding in Ruby on Rails, or coding in anything else for that matter.
You need to be a member of TechHui to add comments!
Join TechHui