Gray's Matter
Justice Gray - North America's favorite metrosexual software consultant

I Wish These People Updated More Than Once a Year

Starscream and Calgary Code Camp
I wish I was getting to Calgary Code Camp this way

Everyone who is everyone in the Alberta developer community is going to Calgary Code Camp on April 28th.  Everyone except one person, that is - my grandmother.  She was originally hoping to come out and "have a talking to" the "confused young man" who continues to send her flowers and lipstick-covered photographs of himself.  Now that she is out of commission for a while, I guess it is up to me to tell Jeff Perrin in person that she is married and just wants to be friends. 

While my grandmother won't be there, two other industry luminaries will: Donald Belcham and D'Arcy Lussier.  So we'll use these two and their imaginary travel plans as a tribute post to Granny, and at the same time cover off yet another of Scott Hanselman's potential interview questions, this one being:

"What is strong-typing versus weak-typing? Which is preferred? Why?"

We join Donald Belcham at the Edmonton International Airport, awaiting his flight to Calgary.  Since Donald looks like an incredibly shady character, the airport security force orders Donald to fill out a declaration form indicating every single piece of luggage he has, along with its type; otherwise, Donald will not be allowed on the flight.  Donald fills out the form thus:

Drink: 55 cans of Sanpellegrino Limonata
Videotape: Star Wars - Return of The Jedi
Alcohol: 20 L of scotch

sanpellegrino_limonata.jpg
I've seen the inside of Donald's fridge - this situation isn't that unrealistic


As Donald's baggage is inspected, all of his items are checked one by one.  Finding a suspicious item, the airport security stops Donald to tell him that they have found a plush Han Solo doll that he has failed to declare; they cannot allow him entry onto the plane unless he makes another declaration.  Donald refills the form, adding

Marital aid: Han Solo plush doll

to the list.  He tries to go through the airport screening again.  They stop him once more to explain that a Han Solo plush doll is actually a toy and *not* a marital aid, and thus cannot be declared in that fashion.  Donald insists that the Han Solo doll can actually be both.  The airport security tell him that it is illegal to use the Han Solo plush doll as a marital aid on his trip.  He will have to declare it as a toy if he wants to take it on the plane - and if he tries to use it as a marital aid he will be arrested.  After some passionate arguing and crying in the fetal position, Donald's final declaration form looks like this:

Drink: 55 cans of Sanpellegrino Limonata
Videotape: Star Wars - Return of The Jedi
Alcohol: 20 L of scotch
Toy: Han Solo plush doll

Everything now checks out.  Donald Belcham is coming to Calgary, and its women (and livestock) will never be the same.

With Donald safely aboard to sexually harass unsuspecting flight attendants everywhere, let's check in with D'Arcy Lussier.  Unlike Donald, D'Arcy lives in Winnipeg where the concept of modern commercial flight is a science fiction fantasy.  He heads over to farm subcolony #72 to get in the bed of the pick-up truck that will cart him off to Calgary.  Before getting on the "flight", D'Arcy also has to provide a list of his luggage, which is as follows:

Mexican wrestler mask
Picture of John Bristowe
Saved By The Bell, Seasons 3&4

Saved By The Bell Seasons 3 and 4 - featuring the Zack Attack
The DVD that features the Zack Attack, D'Arcy's favorite band

Note here that D'Arcy has not declared any type for these objects.  This is because the Winnipeg Travel Fiduciaries consist of a stuffed moose, a dead squirrel and a sign saying "BE BACK LATER 4 REALZ 1996".   Despite their varying types, D'Arcy plans to use *all* of these items as marital aids, despite the illegality of those acts in Alberta.  As far as the Winnipeg Travel Fiduciaries are concerned, D'Arcy can use his luggage however he likes; however they will not be responsible for protecting him if there is any inappropriate item usage while in Alberta.

The difference between these two situations is coincidentally similar to the difference between strong typing and weak typing!

Donald's airport experience exemplifies strong typing.  In a strong typing situation, the type of all of your variables (Donald's luggage items) must be declared prior to compile time (boarding the aircraft).  If any variables are left undeclared, the compiler (airport security) will flag an error; in our analogy above, this is represented by Donald being denied passage on the aircraft when he did not declare the type of his Han Solo plush doll.  The compiler will also fail if you attempt to use an object in an inappropriate way (when Donald attempted to declare it as a marital aid, when it's really just a toy).  Both of these failures are shown below using C#:

class MaritalAid
{        
}

class Toy
{
}

class HanSoloDoll : Toy
{
  public void PlayWith()
  {
    Console.WriteLine("WHEEE");
  }
}

HanSoloDoll hanDoll = new HanSoloDoll();
// the line below will fail as this object hasn't been declared - compiler doesn't know what type it is.
undeclaredHanDoll.PlayWith() ;
// the line below will fail as a Han Solo doll cannot be used as a marital aid!
MaritalAid m = hanDoll;

D'Arcy's case is an example of weak typing.  In weakly typed languages (Javascript and Ruby, for instance), the compiler doesn't actually care what type your variables are, or what you're trying to do with them - it assumes that you know what you're doing when you're making variable assignments.  The price of this freedom: you'll experience run-time errors if you try to do something with a variable that you shouldn't be.  Using a similar example in Ruby, the following code will compile, but will give run-time errors:

class MaritalAid
   def Use
      puts "Censored"
   end
end

class Toy
   def PlayWith
      puts "Wheeeee"
   end
end

class HanSoloDoll < Toy
end

hanDoll = HanSoloDoll.new();
maritalAid = hanDoll;
maritalAid.PlayWith();  // produces "Wheeeee"

//  this is a run-time failure: Ruby treats this as a basic object, which has no PlayWith method
undeclaredHanDoll.PlayWith();

// another run-time failure - there's no "Use" on the HanSoloDoll or Toy classes, which this evaluates to
maritalAid.Use(); 

Which one of these is better?  I don't believe one is actually better than the other; I think it is a matter of personal preference.  Weak typing is certainly easier on the original developer (easier to compile, at any rate), but it can be much harder on the maintaining developer, since poor development can lead to very convoluted code.  Sure, you can write bad code in *any* language (goodness knows my career is evidence of this fact), but weakly typed languages make it a little bit easier.   This is why test-driven development and unit testing become even *more* important in weakly-typed languages, but that's a topic for another post entirely.   

Do any of you out there have a preference?  If so, what is it and why?  [The Igloo Coder has posted his thoughts on the issue here.]

Previous entries in the "My grandmother and" series:

Sunday, 22 April 2007 #