imperative | declarative

The differences in programming styles

Posted by Eirik on 14 Mar, 2024

Imperative

  • HOW you do something Imperative programming is a paradigm describing HOW the program should do something by explicitly specifying each instruction (or statement) step by step, which mutate the program’s state. Functional programming 是 Declarative Paradigm 的代表,邏輯為用較為抽象的程式碼,理解程式碼想要達到怎樣的目標,Function 之間不會互相共用 state 狀態。
優點:易實作

Declarative

  • WHAT you do Declarative programming is a paradigm describing WHAT the program does, without explicitly specifying its control flow. Imperative 代表 OOP 比較容易寫出狀態互相依賴的程式碼。
優點:易讀

Comparison

BASIS FP OOP
Data Uses immutable data Uses mutable data
Model Follow a declarative programming model Follow an imperative programming model
Execution The statements can be executed in any order The statements should be executed in particular order
Iteration Recursion is used for iterative data Loops are used for iterative data.
Element The basic elements are Variables and Functions The basic elements are objects and methods.
Use Often be used when there are few things with more operations. Often be used when there are many things with few operations