Imperative programming v.s. Declarative programming

Imperative programming (指令式程式設計)

In computer science, imperative programming is a programming paradigm that uses statements that change a program’s state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program consists of commands for the computer to perform. Imperative programming focuses on describing how a program operates. (from Wiki)
簡單來說Imperative programming就是用一連串的指令或statements來改變程式的state。所以Imperative programming著重在「how a program operates」。

因為Imperative programming會改變程式的state,所以也有可能改變到function外的state,進而產生side effect。在專案較大時,debug的難度也大大增加 (p.s. 因為難以追蹤在什麼時候、哪一行code或function改變某個state)。

Declarative programming (宣告式程式設計)

Declarative programming is a programming paradigm—a style of building the structure and elements of computer programs—that expresses the logic of a computation without describing its control flow (from Wiki)
簡單來說Declarative programming就是一種建構程式結構的設計法,著重在「what to do」,而非「how to do」。

Those of database query languages (e.g., SQL, XQuery), regular expressions, logic programming, functional programming, and configuration management systems.

%d 位部落客按了讚: