Skip to content

Instantly share code, notes, and snippets.

@darrencauthon
Created September 11, 2015 14:17
Show Gist options
  • Save darrencauthon/8b938a60baf962129e87 to your computer and use it in GitHub Desktop.
Save darrencauthon/8b938a60baf962129e87 to your computer and use it in GitHub Desktop.
Comment on "How I Came To Love TypeScript"
http://developer.telerik.com/featured/how-i-came-to-love-typescript
You wrote "My favorite thing about all of this is the fact that I know, without a
shadow of a doubt, that when I run this, it’s going to work."
Are you really that sure? I think you may be reading too deeply into the IDE's method checks.
Take your last three lines of code:
let mainPage = <page.Page>args.object;
let txtGreeting = <TextField>mainPage.getViewById('txtGreeting');
txtGreeting.text = "Hello Index";
You are certain this works? Alarm bells are going off in my head. Casting an unknown, untyped object to a concrete type is like an "if" statement. The "if" is always.... is this object actually a type, or not? I don't know how TypeScript handles your casts, but I bet it's either going to immediately throw a runtime error when the cast fails, or it's going to return null and let the next line of code fail.
Don't believe me? Change the "txtGreeting" id on your view to "greeting". Or change it to "txtGreeting " because of an accidental typo later. Or change X or Y or Z... changes *over there* can affect changes *over here*.
Of course, you know this, we all know this, we just don't speak of it... but why do we talk with such certainty about the infallibility of code that is just as susceptible to failure with-or-without your types? Look what you said again:
"My favorite thing about all of this is the fact that I know, without a shadow of a doubt, that when I run this, it’s going to work."
Without. A. Shadow. Of. A. Doubt.
Maybe your IDE can reach deeper. Maybe it can catch a rename of a property in the code and yell at you. But we all know -- no matter how tight we squeeze our code into types and interfaces and IDE plugins and bells and whistles -- the runtime errors won't stop. So why is this myth of static typing = reliable code keep getting perpetuated with such wild claims?
I think the only way to know that code works is to run it. Run it with lots of inputs and test that the right outputs come out. *That* is certainty... but only certainty for the inputs we offered to our tests! That's as good as it gets, and we can go really far with that.
So my suggestion, I guess, is that more focus be put on testing our code reliably, and less focus on Intellisense.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment