Static vs Dynamic Typing / Compiled vs Interpreted difference!

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. I’ll begin with some basic terms and gently lead you to enlightenment.

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 = 2
num = '3' // ERROR

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值