Dynamically Typed and Statically Typed languages

It's a tough debate


Allan Sendagi

3 years ago | 3 min read

There is no shortage of programming languages — Python, Ruby, Java, C++ — you name it. The diagram above is a good way to categories them.

So how do these programming languages differ?

From the diagram above, we see that Javascript is a dynamically typed programming language — so is Python, Ruby, PHP…We also see the C, C++, Java, Scala are statically typed languages.

So what does this mean?

For Javascript, the expression above works just fine. Notice that we don't have to specify what type var a is going to be.

However, in a statically typed language such as C++, if we wanted the same result, we would have to write our code differently.

Notice that with a statically typed language, we have to specify in advance what type var a is going to be — an integer in this case.

Here, we explicitly declare the variables before using them. In a dynamically typed language, we are not bound to this constraint.

Javascript, for example, will recognize 50 as an integer type right away. The type checking for dynamically typed languages is done during run time.

While the user is browsing through the website, Javascript is running and being compiled in the background using the runtime or just in time compilation.

This is why we are able to use dynamically typed languages and assign anything to a variable and things don't break. You might get errors during run time while the user is browsing the website, but because our just in time compilation, we don't have to worry about this.

Now, this all sounds good. But it isn’t always ideal. It might cause problems as well.

Different people have different opinions as to whether Statically typed languages are best to use or dynamically typed languages. It's a tough debate.

The best way to think about this is to look at the pros and cons of each or at least where one shines over the other.

Pros of a Statically typed language

  1. Documentation.

This above example demonstrates what a statically typed language could do. If a new developer came along and ran the sum function with anything other than a number, for example:

This will automatically error out before we put the code to the browser or in production. Something is evidently wrong here because the program expects a number and you are giving it a string.

In that sense, statically typed languages are self-documenting. You see right away the kind of parameter it expects and avoid making an error even if the function was named badly or if it was a complicated function.

2. Auto-completion in IDE’s. This is nice when you are developing. Because of the self-documenting nature of the language, it helps with auto-completion in IDE’s. You can download plugins that show you right away even before you run anything that for example — this should be a number.

3. Fewer bugs in production.
Fewer bugs will make it to production. If something fails at compile-time, we catch it early before we even send it to a browser.

Cons of a Statically typed language

  1. An added layer of complexity.
    We have just made our code a little harder to read. We are just adding another layer of complexity and this can have a learning curve.

2. Why not just write better tests?
A lot of people get excited about statically typed languages and forget about good unit tests.
Before you have static typing especially in a language like javascript, why not make sure you have good unit tests.
It’s easy to just assume that because you have static typing, you are not going to get any bugs. That simply isn't true.

3. Slower developing process.
This is because now you have added another step to the code creation process. Because while coding along, there is another check to see that you are not making any type-errors. This slows down the process and how fast you can create, run and ship code to production.

It is because of all those reasons Javascript became so popular. With a dynamically typed language, you spend less time debugging syntax and semantic errors. Most of your debugging time is spent purely on logic which is more interesting.

To conclude:
Statically typed languages usually prevent bugs and stop errors from happening. Dynamic typing allows you to be more flexible and write software faster.
Typescript allows us to make Javascript behave like a statically typed language. It adds types to Javascript. It makes Javascript safer.


Created by

Allan Sendagi

Technology will save homo sapiens from extinction







Related Articles