Dynamic vs. Static Typing

TL;DR: I’m sharing my view on Static vs. Dynamic typing after watching the Programming Languages Course @ Coursera. In the table below the key highlights:

Static typing Dynamic Typing
The object’s types are checked by the type checking system before the program is run The program is run without any type validation, it’s the developer’s burden to code the checking for when the program is executing (runtime)
At every part of the program the type of the objects used are known, so one’s assured that the right type of objects are being passed in the right places The type of the objects are known only at runtime. So if a wrong object type is being used, it will only show up during execution as a runtime exception.
The structure of the program is somewhat rigid, since a type must be defined for everything that is used Dynamic typing languages are more flexible, since you do not need to respect the type system
Changes in the program’s types structure are flagged by the type checking system and must all be corrected before running the program. Propagating changes in the type structure is responsibility of the developer, but subparts can be run before all the changes are implemented.

A few times I’ve tried understanding Dynamic vs. Static, but I never quite grasped it. I believe I didn’t get it because the mental model I was using wasn’t in line with what the writer had on it’s own, so we weren’t communicating on the same abstraction level.

It was during the Programming Language Course @ Coursera (a course that I strongly recommend if you don’t have a formal CS degree like me), that the professor, Dan, explained it in a way that finally made sense to me. In case there are other people wandering around, struggling to understand it, here’s my take on the subject:

  • Static Typing: it means that the types are/must be defined before the program executes (in a “static” environment). This done automatically by the type checking system, in which the object’s types are known at every line of the code. Therefore, before the program is even executed, there are no worries that there is a different than expected object being passed around. For example, if there is an object Foo being passed to a function, where an Array was expected, the type checking would fail before the program even start running. On the other hand in …

  • Dynamic Typing: there are no checks before the program is run. Any object type can be passed anywhere, provided that the syntax of the language is respected. For example, the if-then-elses parts must be in the correct order, but the language doesn’t care if there is a non-bool in the “if” part, or if the return types from the then/else branches are different. This means that in the code, objects of different types can be passed around without any restriction or error. The burden of type checking is passed to the developer.

    In case there are objects being passed with different types than expected, like in the previous example, it will be only at runtime that an exception will be thrown and the developer will be aware of the error. Since there’s no type checking system to watch over one’s shoulder, usually there are helper functions, like “is-of-type-X?” or similar, that will do the checking while the program is running (hence “dynamic” typing).

Advertisements