paint-brush
I Finally Understand Static vs. Dynamic Typing and You Will Too!by@jballin
144,057 reads
144,057 reads

I Finally Understand Static vs. Dynamic Typing and You Will Too!

by JBallinDecember 7th, 2017
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

When I decide I want to understand something, I become obsessed until I finally do. My latest attempt? <strong>Static vs. Dynamic Typing</strong>.
featured image - I Finally Understand Static vs. Dynamic Typing and You Will Too!
JBallin HackerNoon profile picture

When I decide I want to understand something, I become obsessed until I finally do. My latest attempt? Static vs. Dynamic Typing.

Stack Overflow’s answers were confusing, long, and even contradictory. Turns out these terms are commonly misunderstood, so it makes sense that my search would prove difficult. I continued reading whatever I could find but nothing satisfied my hunger for something approachable and concise.

Compiled vs. Interpreted

“When source code is translated”

  • Source Code: Original code (usually typed by a human into a computer)
  • Translation: Converting source code into something a computer can read (i.e. machine code)
  • Run-Time: Period when program is executing commands (after compilation, if compiled)
  • Compiled: Code translated before run-time
  • Interpreted: Code translated on the fly, during execution

Typing

“When types are checked”

"3" + 5 will raise a type error in strongly typed languages, such as Python and Go, because they don't allow for "type coercion": the ability for a value to change type implicitly in certain contexts (e.g. merging two types using +). Weakly typed languages, such as JavaScript, won't throw a type error (result: '35').

  • Static: Types checked before run-time
  • Dynamic: Types checked on the fly, during execution

The definitions of “Static & Compiled” and “Dynamic & Interpreted” are quite similar…but remember it’s “when types are checked” vs. “when source code is translated”.

Type-checking has nothing to do with the language being compiled or interpreted! You need to separate these terms conceptually.

Python Example

Dynamic, Interpreted

def foo(a):    if a > 0:        print 'Hi'    else:        print "3" + 5

foo(2)

Because Python is both interpreted and dynamically typed, it only translates and type-checks code it’s executing on. The else block never executes, so "3" + 5 is never even looked at!

What if it was statically typed?

A type error would be thrown before the code is even run. It still performs type-checking before run-time even though it is interpreted.

What if it was compiled?

The else block would be translated/looked at before run-time, but because it's dynamically typed it wouldn't throw an error! Dynamically typed languages don't check types until execution, and that line never executes.

Go Example

Static, Compiled

package main

import ("fmt")

func foo(a int) {  if (a > 0) {      fmt.Println("Hi")  } else {      fmt.Println("3" + 5)  }}

func main() {  foo(2)}

The types are checked before running (static) and the type error is immediately caught! The types would still be checked before run-time if it was interpreted, having the same result. If it was dynamic, it wouldn’t throw any errors even though the code would be looked at during compilation.

Performance

A compiled language will have better performance at run-time if it’s statically typed because the knowledge of types allows for machine code optimization.

Statically typed languages have better performance at run-time intrinsically due to not needing to check types dynamically while executing (it checks before running).

Similarly, compiled languages are faster at run time as the code has already been translated instead of needing to “interpret”/translate it on the fly.

Note that both compiled and statically typed languages will have a delay before running for translation and type-checking, respectively.

More Differences

Static typing catches errors early, instead of finding them during execution (especially useful for long programs). It’s more “strict” in that it won’t allow for type errors anywhere in your program and often prevents variables from changing types, which further defends against unintended errors.

num = 2num = '3' // ERROR

Dynamic typing is more flexible (which some appreciate) but allows for variables to change types (sometimes creating unexpected errors).

Did that clear things up for you? Let me know in the comments!