Thursday, 13 November 2008

Hash Wrangling

Someone once asked me "just what is a Symbol in Ruby". I was stumbling around the web and found this: http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol

The way I personally think of it is as an immutable string that has a hashed internal representation, which probably encompasses definitions 4 from the web page above. Having coded to the Ruby C API, I also think of them in terms of them in terms of its hashed internal representation, and the C typedef ID (definitions 5 and 13).

But this also reminds of something I've been thinking about for a while with respect to hashes. Let's call it hash-wrangling:

wrangle |ˈraŋg(ə)l|
noun: a dispute or argument, typically one that is long and complicated.
verb:
  1. [ intrans. ] have such a dispute or argument : [as n. ] ( wrangling)weeks of political wrangling. See note at quarrel .
  2. [ trans. ] round up, herd, or take charge of (livestock) : the horses were wrangled early.
  3. another term for wangle .
ORIGIN late Middle English : compare with Low German wrangeln, frequentative of wrangen ‘to struggle’ ; related to wring .

Hashes are a great programming construct that I first encountered in awk's associative arrays. As a former C programmer, hashes were great because they provide tremendous power in freeing you from the mundane simplicity of C's data structures, and let you think about the relationships between different data. Gone are all thoughts of B-trees, search algorithms, etc and you're left with just the hash key and its related value.

The problem I see with hashes however is that it is far too easy to get the key wrong. It's quite easy to misspell a key, and have the resulting value stored in the wrong place, or fetched from the wrong place. The interpreter doesn't care what the hash keys are, and will just return a null when the value is fetched from the wrong place.

I've sometimes considered creating forms of enumerated types for the hash keys, as a convenient way to verify that only the correct keys are being used for a given hash. But this gets very long winded very quickly. And not only is it easy to get the key wrong, but with a hash using the "unknown" dataype (needed to allow nested hashes), it's also easy to get the type of the value wrong.

I think the only solution for this is to write test scripts that exercise every line of code. This sort of approach only goes so far however, and doesn't necessarily catch those cases where the hash keys is generated at runtime. You end up having to write lots of code that validates the inbound hash keys.

This is where an Object Oriented language like Ruby can help a lot because you can build the validation into the class definition itself. The class is basically a hash structure that you've defined in your code up front, and that is validated every time you access it. A hash is then relegated to only those values that might have been received from outside, but not yet passed to the class for validation.

This is not to say that the structure of the class and its data should become static. There is tremendous power in the way a dynamic language like Ruby has all classes open for the addition of new methods all the time. So, even if your class validates the data through its definition, thus staying DRYer in your approach to data structures, you can easily add more capabilities later.

Wednesday, 21 November 2007

Apple Quality?

I have been ruminating lately over whether or not to upgrade my Apple hardware products to more recent versions.  But I am starting to have concerns over the quality of recent Apple hardware products.  Let me explain...

I have a third generation "classic" iPod and still use it all the time.  I travel quite a lot, so it's great for all that dead-time waiting in airport lounges, etc.  As a piece of industrial design it's fantastic.  I love the way I can operate it using the same hand I'm holding it in.  Part of this comes from the select button in the middle of the click wheel which is slightly convex, and when pressed gives a nice and positive click sensation when you press it.  

However more recent iPods however have a very flat or even slightly concave select button, and when you press it, there is no click sensation when it is pressed.

I know I'm being picky, but this is actually issue that is holding me back from an upgrade.  Why did they change it?  I'm guessing that it was made flat/concave to prevent the button from being accidentally pressed if it is placed face down on a table (not an issue for me because I use an iSkin)?

I'm probably going to upgrade fairly soon anyway, because my current unit is starting to have hard disk issues.  Occasionally I find that it won't start up.  The disk spins up but then makes a distinct click and spins down and up again.  Eventually I get a "sad iPod" icon and a web link telling me to go to Apple's iPod support page.  To recover the situation, I have to perform a bit of "percussive maintenance".

I do love my current iPod, and will be sad when it really dies.  Apple have been adding features to tempt me to buy a new one.  I recently played with both an iPhone and an iPod Touch and I have to agree with everyone raving about these products' touch-based interfaces.  It was just so natural.  So I'll probably buy one of those.  But I've also noticed that the same concave button is there on the iPhone and the iPod Touch as the home button, and again the buttons on these units lack that positive feedback when pressed.

The other Apple product that I own is a beloved PowerBook G4, 15", 1.67GHz.  I've had this baby in my life for a couple of years now and I feel truly enriched by it.  At work I use a Dell Latitude D610 - a machine that I truly despise.  I resent its presence in my life and the amount of time I spend waiting for it.  Two things I like:
  • The quality of the PowerBook's screen is lovely and natural, as compared to the Dell's screen which, when viewed even slightly off-axis, makes my eyes bleed.  
  • The sound from the PowerBook, whether from the built-in speakers, or from the headphone jack is great.  With the Dell, the speakers are poor and, instead of the Sennheiser headphones I use with the PowerBook, I have to use lower quality (iPod!) headphones to avoid having my brains sucked out by the bus-noise you get from its headphone jack.
Now that Leopard is out, I feel it's time to upgrade so I've been looking at specs and prices and analysing what I'm going to use it for, reading reviews and just generally enjoying the shopping experience. :)

But I've come across some articles saying that the screen quality isn't as good as my current PB and that there is some high-pitched noise or other digital interference when you listen using the headphone jack.  What's happening here?  Perhaps the headphone noise is down to he chipset that Intel provide to support their Core 2 Duo processor, but that doesn't explain the screen dropping in quality.

So I put it to you, dear reader, that Apple hardware quality is decreasing over time.  Perhaps it's just part of being competitive - after all, their products seem to be better value than ever.  But I feel there is still an expectation of quality that comes with every piece of Apple hardware.

Comments?