Gray's Matter
Justice Gray - North America's favorite metrosexual software consultant
E-mail Justice
Subscribe to this feed
Subscribe to comments
Perennially Popular Posts
How to write a presentation abstract
My grandmother, globalization, and what's wrong with DateTime.Parse
A letter to my Worldvision sponsor child
Development Methodologies for the 21st Century
My grandmother & the difference between strong typing and weak typing
How I am becoming a better developer, part 1 of infinity
Facebook RSS Feeds: How to Write Your Own With C#
The Ten Keys To Happiness
My grandmother and the Global Assembly Cache (And Star Wars)
My grandmother and the difference between processes and threads
How to make a meeting request
Why you should never be an independent consultant
The DasBlog 1.9 AJAX Autosave True Story, Part 2
Other "metro" developers
Ben Scheirman
Bil Simser
D'Arcy Lussier
David Woods
Donald Belcham
Donald Jessop
George Clingerman
James Kovacs
Jason Hunt
Jason Row
Jean-Paul Boodhoo
Jeffrey Palermo
Joey deVilla
John Bristowe
Jonas Avellana
Kyle Baley
Mack Male
Martin Hinselwood
Michael McClenaghan
Mike Duncan
Mike Friesen
Mo Khan
Otyce
RoboSapient
Russell Ball
Scott Hanselman
Scott Reynolds
Steven Rockarts
Tech Embassy
Terry Thibodeau
Tom Opgenorth
Vladimir Levin
The Justice Gray Fan Club
Amazing Mrs. Loquacious
Dr. Teeth
Facedown
Janey Lynne
Maxime Lamy
Ridley Thunder
Stan Woo
Vanilla Con
Wobbly*Bits
I Wish These People Updated More Than Once a Year
Anand Narayan
Jeff Perrin
<< The law of attractiveness and system preference
Beyond Code, The Totally Hetero Review >>
My grandmother and the difference between strong-typing and weak-typing
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
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
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:
the difference between processes and threads
the difference between HTTP GET and HTTP POST
the Global Assembly Cache (and Star Wars)
globalization, and what's wrong with DateTime.Parse
Sunday, April 22, 2007
Comments [2]
HanselmanQuestions
|
Technical
|
Tracked by:
"My grandmother and the difference between processes and threads" (Gray's Matter...
[Trackback]
http://igloocoder.com/archive/2007/04/22/1135.aspx
[Pingback]
5/11/2007 10:09:50 PM (Mountain Daylight Time, UTC-06:00)
Great Presentations!
I learned more useful infos from your site. Thanks much!
I see. Thanks anyway :)
翻译公司
|
fanAT NOSPAMgmail dot com
10/31/2007 7:14:27 AM (Mountain Daylight Time, UTC-06:00)
I love that movie. It's the best movie I've seen. Saved by the bell and Friends are the best. Great presentation. Congratulation.
arizona golf tours
Name
E-mail
Home page
Remember Me
Comment (HTML not allowed)
Enter the code shown (prevents robots):
Justice Gray
: a seething cauldron of rampaging masculinity. A fighter! A brother! Your
secret lover
! He's also a software development consultant and the Vice President of the
Edmonton .NET user group
. He also greatly enjoys speaking about himself in the 3rd person.
Gray's Matter Archives
Recent Posts
Controversy clarifications!
The most controversial interview of ALL TIME
Finally! The *winner* of the "What Justice Gray Means to Me" contest
The mystery of D'Arcy Lussier's ASP.NET MVP is solved...
Well, now that's pretty horrifying
MVPs on MVC: "No, we don't have any idea either"
Why I am happy to not be at the MVP Summit
Donald Belcham's cry for help: MS MVPs, your attention is needed
ALT.NET Pursefighting revealed
Test, please ignore!
Change yourself, change the world: a great week 4
Post Categories
BookReviews
Conferences
DasBlog
EDMUG
Goals
HanselmanQuestions
HotDeveloperCorner
ILiveToInform
Leadership
Mac
OtherGrayAreas
Personal
Productivity
Ruby
Suresh
Technical
Transformers
Vancouver
Wii
<
July 2008
>
Sun
Mon
Tue
Wed
Thu
Fri
Sat
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
Sign In