The Yin and Yang of Typing: Dynamic vs. Static

The Yin and Yang of Typing: Dynamic vs. Static

When I was researching about what the heck is dispatching, and overloading, I realized that I should write a blog post about typed languages. Static and Dynamic typing are two different characteristics of programming language itself. I consumed a lot of content from ChatGPT, Stackoverflow, and Youtube about this topic and here's my curation xD

First, let me clear your mind about typing.

Typing is just type checking in programming language. It's important because there are pros and cons we will talk about in the next sections. Type checking means programming language itself checking if the types are correct.

Dynamic Typing: The Yin

So the dynamic typing means that programming language will check your variables types at the runtime. Means that you can run your code with a lot of errors and bugs within itself. Btw, if you don't know what is the runtime vs compiler time;

Runtime: it means your program executed and the time after execution

Compile time: means your program's source code is translated into machine code

You can compile dynamic languages even if they contain errors that will prevent script from running probably. (with compiler implementation)

Addition to that, if you don't know when your code translates into machine or when your program will executed; here is a diagram for you! 👇

After learning which one is will happen first and which one is later, let's continue with dynamic one, Yin!

Dynamic languages are flexible and save your time and space when writing code. Most scripting languages are dynamically-typed since there's no static type checking for them. Scala can be used scripting lang and also it's statically-typed.

  • Scripting language doesn't mean interpreted language. There are a lot of projects for turning your interpreted language into compiled one, and also the opposite one.

Addition to that, dynamically-typed mean you can change your variables types'., because dynamic typing is associated with the value it assumes rather than the variable itself.

You will understand better after explanation of Yang. 🤔

Static Typing: The Yang

On the other hand, static typing means that language performs type checking at compile time. It means that static language cannot be compiled if the script contains error. And also it means that it requires you to declare variable types before assigning. Main advantage is; all kind of checking can be done by the compiler and a lot of bugs, issues are caught at the early stage.

  • Compilation doesn't mean statically-typed
There are also hybrid languages, like TypeScript, which are dynamically-typed languages that include optional static type checking. In summary, while static typing is often associated with compiled languages, there are dynamically-typed languages that can also be compiled.

In static typing language, variables types are static, meaning once you set a variable to a type, you cannot change it, because typing assoiated with the variable rather than the value it refers to.

A Tale of Two Methods + coding example

These two methods has their own advantages and disadvantages, so let's not thinking about which one is best for the programming world and just think about it; which one is best for your project!

# example of dynamic typing in Python
x = 42  # x is an integer
x = "Hello, World!"  # now x is a string, type is changed
print(x)  # prints "Hello, World!"

// example of static typing in Rust
fn main() {
    let a: i32 = 5;  // a declared to 5 (32-bit signed integer)
    println!("{}", a);  // prints 5
    // a = "hello";  // This line would cause a compile-time error

Dynamic typed language: is flexible about changing types and giving the programmer fast development with bunch of bugs

Static typed language: the main advantage is the checking of all types are getting done at compilation phase and you can easily catch all trivial bugs, but cannot change type of variable after. Development will be slow but also robust for the project, and at the and it's faster than dynamic-typed language

Also at the end, it's just a design choice!