DEV Community

Andres Court
Andres Court

Posted on

Dynamic vs Strictly typed languages

In this article I'm going to discuss about dynamic typed languages and strictly typed languages, giving some of the adavantages and disadvantages of each and
which is my preference, but first lets start with some definitions:

  • Dynamic Typed Languages: This languages have the characteristic that it will assign a type to each variables at runtime based on whatever the data value is at a specific moment.
    • Examples of this kind of languages are Javascript Python PHP
  • Strictly Typed Languages: This languages have the characteristic that you define what type will have each variable the moment you write the code.
    • Examples of this kind of languages are Go C/C++ Rust
Dynamic Languages Strict Languages
Javascript Go
Python C/C++
PHP Rust

Advantages of Static Typed Languages

Protection from Runtime Errors

This is the main benefit of a static typed language since the type of each variable will be checked by a compiler, who will ensure that whatever value that a variable7
holds must be of the type in which the variable is declared. Lets see an example in Go:

package main

func main() {
    var x int

    x = 10   // This will be allowed by Go since 10 is an integer
    x = -10  // This will be allowed by Go since -10 is an integer
    x = 10.3 // The compiler will not allow this since is a float number
}
Enter fullscreen mode Exit fullscreen mode

This kind of languages add some boilerplate that is defining what a variable should hold, reducing the code we need to add for validation purposes.

Developer Experience

Having a strictly typed language allows the LSP (Language Serve Protocol) in your editor to know what a function, variable, etc expects. With this it can give the
developer hints, and code completion which makes easier to code.

Compiled

Usually strictly typed languages are compiled, this means it will have to pass through a program called "Compiler" that will translate the code written in that
language to machine code, making it generally faster than interpreted languages, continuing with a Go examples, this is how you build and execute your code in Go.

go build -o app main.go
Enter fullscreen mode Exit fullscreen mode

The last command will compile the main.go and create the app executable file

./app
Enter fullscreen mode Exit fullscreen mode

With this command we will be able to run the application built.

Besides this, I will not continue with the discussion about compiled vs interpreted languages, that will be topic for a future post.

Advantages of Dynamic Typed Languages

Less Boilerplate

Generally speaking dynamic languages are mor succint than the statically ones. Type definitions add to the verbosity of the syntax.

Consuming Data Sources

When consuming information from external data sources, there is no need to add aditional code, sometimes we will create a data structure to make it easier to parse that
information, but it is not required

Beginer Friendly

All of this advantages makes a dynamically typed language easier for learning to program with, usually a great entry point for new developers.

Conclusions

Even though each kind of languages have its use cases, for me the advantage of only validating the data on entry makes the great difference between each, and that is
being addressed by the dynamic typed languages communities and organizations eg

  • Javascript you define types either using JSDoc or Typescript
  • Python is introduced Type Hints in its version 3.5

Top comments (2)

Collapse
 
jdmedlock profile image
Jim Medlock

Nice article @alcb1310! I agree and I keep hoping (beyond all hope) that one day we might see support for stronger typing in native JS.

Collapse
 
alcb1310 profile image
Andres Court

Thanks @jdmedlock! Hopefully one day we will, but I really don't see that being even possible due to all of the current code