Friday, September 13, 2013

Coding 101: Typing

Next lecture in my Coding 101 series.  Looks like September is another one of those coding based months.  Anyways, this post is about typing.

What do I mean by typing?  Surely I don't mean the act of typing on a keyboard.  While an interesting topic, see Dvorak, this post is not about that.  This post is however about programming languages, more precisely the type safety of a language.

In particular this post is about the difference and examples of statically typed and dynamically typed languages.

Let us start with statically typed languages.  These languages do not allow comparison between differing types and check methods for input variable types on every function invocation.  This actually looks different depending on the language.  Most often you will see statically typed languages like C or Java where you have to declare types before all of your variables.  In this case you might see something like this:

int a = 10;
int b = 20;
int c = a + b;
int d = 3.14;

It is apparent by the first keyword 'int' that these variables are meant to be integers.  In a statically typed language, the last line 'int d = 3.14;' would cause a type error as 3.14 is not actually an integer.  For an example of function invocation, take the function print(string text).  If you ever try to put any other type but a string, the function will type error.  For function invocations, you might overload the function to take different input types, but that's a different topic altogether.  There are exceptions to the declare your type before the variable in statically typed languages, see Haskell, but for the most part, C/Java style static typing is more common.

Conversely, you have dynamically typed languages.  In these languages, types are not checked and outputs can be quite amorphous in type.  Let us take this example in JavaScript, which is not related to Java:

var a = 4;
var b = 4.5;
var c = "hello";
var d = a + b + c;

In JavaScript, the result of d is the string "8.5hello" (note: quotes denote a value is a string in most languages, others might use single quotes too like JavaScript, PHP, and SQL).  That seems bizarre if you think about it.  What's going on?  var d is the addition of a, b, and c and none of their types are defined.  In JavaScript, disparate types are added together left to right as in most languages and are 'type casted' as necessary.  Let's walk through the example a bit.  First you have 4 + 4.5 which is of course 8.5.  That just seems like simple math.  The next step is the interesting one.  Since "hello" is a string and 8.5 is a numeric type they should definitely type error.  Unfortunately, or maybe fortunately, JavaScript in these situations will always 'type cast' non-strings to strings if either operand is a string.  What this means is that 8.5 + "hello" is equivalent to "8.5" + "hello" where + is appending in JavaScript.  This ends up with the result of "8.5hello" for d.  Crazy stuff eh?

I won't say whether static typing is a good thing or not, that's all your opinion and I don't want to cloud it too much with my preferences.  However, I do hope you found this interesting.  If you want to try out the JavaScript example to see that I'm not lying go here.  (There will be a popup with the value of d, that's what the alert() function in JavaScript does)  I hope you enjoyed this post and until next time, take it easy.

--CsMiREK

No comments:

Post a Comment