Sunday, April 13, 2014

Dynamic and Static Typing

It happened to me more than once, that I heard people talking about something referred to as "scripting languages" and the fact that those kind of languages have no types. In this post, I want to clarify this subject in more detail. The term "scripting language" doesn't really exist in Computer Science. Therefore, two different type systems for programming languages do exist: compiled (static) and interpreted (dynamic). With static typed languages, the type checking takes place during compilation, whereas dynamic typed languages do the type checking at runtime, by the interpreter that executes the program. Popular representatives for statically typed (and compiled) languages are for example C++ and Java. Python, Perl and Ruby are languages which are dynamically typed and executed by an Interpreter.

But how is a software developer confronted with dynamic and static typing?

Example No.1 (Scala):
scala> var i = 42
i: Int = 42

Scala is a statically typed programming language, but sometimes it might look like its dynamically typed, because it allows the developer to write less boilerplate code. After assigning the integer value 42 to the variable i, the Scala compiler is able to recognize that i is of type Int, which is done through type inference. The compiler "guesses" the type of the variable by determining the type of the expression on the right hand side of the assignment.

Lets look what happens, if we try to assign a string to i:

scala> i = "Hello World!"
<console>:8: error: type mismatch;
 found   : String("Hello World!")
 required: Int
       i = "Hello World!"


Since the variable i is of type Int, we cannot assign it any value that is different from the type of integer -  due to the fact of static typing. A special case is Polymorphism, where a "super-type", can hold values (references) of all "sub-types".

Example No.2 (Python):
>>> i = 42
>>> type(i)
 <type 'int'>

In the second example, I did the same assignment as before, but using a dynamically typed programming language - in this case: Python. To invalidate the argument, that (scripting) languages like Python have no types, look at line three. Every variable in Python has a type, that can be determined at any given point in time during execution by using the type function.

Lets look what happens, if we try to assign a non-integer value to i:

>>> i = "Hello World!"
>>> type(i)
<type 'str'>


As we can see, the type of variable i has changed from integer to string. That is due to the fact, that Python is a dynamically typed programming language, where the type of a variable can change over time.

Both, dynamic and static typed programming languages have pros and cons. Dynamic typed languages are a good choice for rapid prototyping, because sometimes types stay in the way, when developers want to be creative and think about a solution for a problem. On the other side, since the absence of a compiler, the developer has to make sure that for example, a method that awaits an integer as argument, is not called given a string - which would lead most likely to a runtime exception, depending on the methods implementation.

Although, dynamically typed languages easy rapid prototyping, they are often slower than compiled languages. Furthermore, refactoring is more difficult when using dynamic typed languages, because syntax errors (wrong (text-)replacements) are not discovered by a compiler, but have to be found by self-written unit tests. Therefore, a really high test coverage of the whole source code is necessary.

References:
DynamicTyping
Type System
Type Systems for Programming Languages

No comments:

Post a Comment