Many key factors for developing a great programming language include how it manages its memory space. This memory space largely depends on empty memory boxes called variables that one creates to store all sorts of data.
Early languages like C, C++, and Java are statically typed languages. By statically typed we mean, hard coding, stating the type of every element, variable during their declaration itself.
Though it always felt like a problem, it wasn’t dealt with until dynamic typing came into existence. In 1958, LISP was the first dynamically typed language. After nearly 40 years, in 1991, Python was released, became the most successful dynamically typed language.
Let’s understand how variables are stored? And importantly, how are their types labeled inside a memory space?
In languages like C, C++, and Java, we need to specify the data type of the variable at the time of declaration.
Let’s take an example.
String message = 'Hello';
In Java, a string type object is created with the value “Hello”. Additionally, it creates a variable ‘message’ of string type. Then, it points/references to the object ‘Hello’ inside the memory space.
If we now decide to create another datatype, say integer, with identity ‘message’, it won’t work. It will raise an error stating “incorrect datatype”. C, C++, and Java are rigid in their manner of creating variables.
Let’s write the same code in Python.
message = ‘Hello’
In Python, a string type object is created with the value “Hello”. Additionally, it creates a variable ‘message’ which points/references to the object ‘Hello’ inside the memory space. But, the data type of ‘message’ is not specified until runtime.
In the graphical representation above, the message had no specified type. It is pointing to a string type object. Now, if we decide to name another variable of the same name ‘message’ but of a different data type, we can. For example:
message = ‘Hello’
message = 100
Since python is a dynamically typed language, the variable message will now refer/point to the last data type it is associated with. This final association will be done at run time.
Let’s define what is dynamically typed language is with whatever we have learned so far?
“A dynamically typed language does not require declaring the data type of the variable at the time of declaration rather it will automatically associate the data type to that variable at runtime.”
PHP, Prolog, Python, Ruby, Smalltalk, and Tcl are a few dynamically typed languages.
Advantages of Dynamically Typed Language
- Simple language: Dynamically typed languages are simple in writing and easily understandable.
- Shorter compile time due to shorter code.
- Cleaner code: Small codes due to fewer/shorter declarations.
- Easy comprehension: Easy readability and cluster-free due to cleaner code.
- Variables can be easily passed to different modules without defining data type.
- Source code will look pretty much like pseudo codes as the language is simple.
- Not much distinction between writing methods for static and dynamic typing.
- Testing and debugging become easy as coding is short and repeatable.
- Higher reusability due to less complex or heavily-defined structures/types and it is easier to mock-up replacements.
Disadvantages of Dynamically Typed Language
- Run-time type errors.
- Can be very difficult or even practically impossible to achieve the same level of correctness and requires vastly more testing.
- No compiler-verified documentation.
- Poor performance (usually at run-time but sometimes at compile time instead, e.g. Stalin Scheme) and unpredictable performance due to dependence upon sophisticated optimizations.
Frequently asked questions about Dynamically Typed Language
Q) Does dynamic typing implies weak typing?
A) No, dynamic typing does not imply weak typing. Dynamic/static typing is not related to weak/strong typing.
Dynamic typing: Declaring variable type is not necessary and it will be generated on the fly during run time.
Static typing: Declaring variable type is mandatory before usage, else it will result in a compile-time error.
Strong typing: Once a variable’s data type is declared it cannot be changed. You can explicitly cast the data type though.
Weak typing: Variables are not of a specific data type. However, it doesn’t mean that variables are not “bound” to a specific data type. In weakly typed languages, once a block of memory is associated with an object it can be reinterpreted as a different type of object.
Q) Is dynamic type safe?
A) Most higher-level languages, including dynamically typed languages, are type safe because any attempt to use a type incorrectly is guaranteed to cause an error (compile-time or run-time) in them.