sub-title

Also check Orama's Quora and Orama's GitHub
I shall not claim to know so much, but only that I learn new things everyday

Friday, 6 May 2022

Static vs Dynamic Typing in Programming

Introduction

While reading on online forums, I realize that as developers do their day-to-day software development or programming, sometimes they are unaware of some of the underlying  fundamental principles that rule those languages. This has also happened to me personally on many occasions. The good news is that it becomes easier to understand these things when you have some practical programming experience. Again – as I always say – The taste of the pudding is in the eating.

One of the many such fundamentals is called “typing”. Let me try to decode “typing” in this post, hoping that it will help someone out there.

For starters, a programming language may be statically or dynamically typed. This is important.

Familiarity with data types in general is required for proper understanding of this post. By data types, I am talking about things like int, float, string, Boolean, date, etc, depending on the particular language. It is important that a program properly handles the assignment of data to variables – i.e. the right data must be assigned for the right type of variable.

As the name suggests, “typing” refers to the way data types are handled. At some point in the execution of a program or application, the type of data being used must be checked for conformity – and this is called type-checking.


A Refresh of Compiled vs Interpreted

Let us refresh a little bit regarding compiled vs interpreted language at this point before we proceed.  

In a compiled language (or rather program), a program is translated (compiled) into machine code before being executed – meaning that the machine code is the one that is used at runtime. To run or test the program, you have to “build” it first, and this may be an overhead to the developer. Examples are C, C++, Erlang, Haskell, Rust, and Go.

In an interpreted language (or rather program), the program is executed while still in source code (before any translation). That overhead makes an interpreted program slower, but the benefit is that there is no additional need to translate before testing. They are also platform-independent, i.e. they can run on any OS. Makes sense? Examples are PHP, Ruby, Python, and JavaScript.

Now, I am sure if you have used a mix of compiled and interpreted languages before, then it is very easy to grasp the above distinction between the two.


Type-checking (Static and Dynamic Typing)


Back to type-checking. If type-checking is done at compile-time, then it means the language is statically-typed. If done at runtime, then the language is dynamically-typed.

Statically-typed languages specify the data type before assignment, e.g. in Java of C#

int age;
age = 10;

If you make the following assignment:

age = “10”;

then an error will pop up at compile-time because of the type mismatch since age was declared as an int, but you are assigning a string "10".

Dynamically-typed languages do not specify the data type. They simply assign data, e.g. in Python or Groovy

age = 10


Advantages of Static Typing (also disadvantages of Dynamic Typing)
  • the program runs faster since type-checking and optimization have already been done at compile-time
  • errors may not arise at runtime since they were already identified at compile-time. In other words, it is risk-averse, or less prone to errors because any mismatch between a data value and variable type is caught at compile-time. The programmer is forced to correct errors before runtime.

Disadvantages of Static Typing (also advantages of Dynamic Typing)
  • coding takes more effort (you have to specify the type)
  • the developer doesn’t have much freedom or flexibility. They are constrained by data types. This factor seems to make it harder for learners to use statically-typed languages.

Final Word on Related Concepts


The concepts of Weakly-typed and Strongly-typed languages are usually confused with Dynamic and Static typing. They need not be confused. Let me try to clarify here.

Depending on the language, the type-checker may have some liberty while checking types. For instance if a programmer issues the following line of code:

age = 20 + true;

Surely? Adding 20 to true? Well, there may be a good reason for that, but only the developer can tell us.

The handing of the above statement will vary depending on whether a language is weakly-typed or strongly-typed.

Weakly-typed languages offer a lot of freedom with variable-type compatibility. They are more liberal, and can make their own interpretation instead of throwing an error. If the above statement was issued in Javascript, then the type-checker may assume that true means 1, and therefore would return 21. It would not throw an error because Javascript is weakly-typed.

Strongly-typed languages, such as Python, are very strict with variable-type compatibility, and they would throw an error on encountering such statements.

As can be seen above, weakly-typed languages can automatically convert variables to other data types to allow an operation to succeed. In the above example, the boolean value true is implicitly converted to the number 1 in Javascript.

So much for a day.

No comments:

Post a Comment