Clean Architecture - Starting with the Bricks: Programming Paradigms
前導
- 這章像在介紹軟體界的「誓約與制約」,提到了三種典範語言為程式碼增加了一些規則,而這些限制可以讓軟體品質更高
- Structuring Programming
- Object-Oriented Programming
- Functional Programming
 
- 可以由上述的限制延伸到整個系統的三個專注點
- function
- separation of components
- data management
 
Structuring Programming
- 增加限制在 Program counter 的控制
- imposing discipline on direct transfer of control
 
- 使用 if, else, while取代goto- 為何要廢除 goto看這篇
 
- 為何要廢除 
- 雖然現今的主流語言已經沒有 goto但還是有像 exception 和 break 等等破壞流程的語法
- Edsger Wybe Dijkstra 想要用數學學科中的**歐幾里得定理**找到一個公式可以代表軟體的品質
- 所有的程式都是由 sequence, selection, and iteration 組成
- sequence, selection 透過枚舉法證明
- iteration 透過歸納法證明
 
- 第 5 點的發現看似有機會找到公式解,但最終失敗了,於是開始採取科學的方式證明軟體品質
- 數學可以證明對的
- 科學是普遍性的假說,如黑天鵝,所以存在可否證性(falsifiability)
 
- 測試無法代表程式正確,僅能透過程式發現錯誤
- Structuring Programming 的價值來自於可以由多個可否證性的 unit(function) 所組成
- 鼓勵用 module 或是 function 切分系統 (functional decomposition)
補充資料
Object-Oriented Programming
- 增加限制在 function call(from pointer)
- imposing discipline on indirect transfer of control
 
- 有兩位大神發現可以把 function call stack frame 放在 heap memory 中
- stack memory vs heap memory
- 
stack memory is a fixed of size memory int val = 0; // compiler can know the size of the programming
- 
在 heap memory 中的空間不隨著 function return 而釋放,所以需要在用完後需要呼叫 free() 或是由 GC 處理,heap memory 是整個系統共用的 int* ary = new int[n] // cannot know the size during compilation
 
- 
 
- stack memory vs heap memory
- 什麼是 OO?
- combine data and function
- f(o)→- o.f()
 
- 模擬現實世界
- encapsulation, inheritance, and polymorphism
- 上述都對於作者來說都不是好的答案
 
- combine data and function
- Encapsulation
- 
並非是 OO 的專有概念 struct Point {int x;int y;} // C 語言也可以封裝
- 
OO 的封裝並不完美 - C++
- the header file includes variables (the user doesn’t need to know)
 
- Java/ C#
- definition and implementation are in the same file
 
 
- C++
 
- 
- Inheritance
- The goal of Inheritance is the re-usability of already implemented code by grouping common functionality of different classes in so-called base classes —- from How To Implement Inheritance and Polymorphism in C?
- 在 C 裡也可以手動完成類似的概念(透過轉型)
 
- Polymorphism
- In Polymorphism, we declare an Interface and implement the details in entities of different types —- from How To Implement Inheritance and Polymorphism in C?
- STDIN and STDOUT 早已包含 Polymorphism 的概念
- 
每個 Unix 上的裝置都要實作 5 個 functions,這些 functions 都包含在 FILE Structure - open
- close
- read
- write
- seek
 // device definition #include "file.h" void open(char* name, int mode) { /*...*/ } void close() { /*...*/ } int read() { /*...*/ } void write(char c) { /*...*/ } void seek(long index, int mode) { /*...*/ } struct FILE console = { open, close, read, write, seek } // somewhere extern struct FILE* STDIN; STDIN = exist_device int getchar() { return STDIN->read(); }
- 
透過上述方法可以將每個 device 當成是一個 plugin,這種作法稱為 Plugin architecture 
 
- 
- C++ 透過 Virtual method table 找對應的 method 位置
- 在 OO 之前,polymorphism 的達成是需要去 init pointer 的,假如忘記 init 而直接用 pointer call function 是很危險的
- OO 導入 interface 讓 polymorphism 更安全
- initialization
 
- 依賴反轉延伸出依賴方向是可以自由選擇的
- business rule, UI, Database
- 互相獨立 → 可以讓不同 team 開發
 
 
- 上述的三個概念,沒有一個是由 OO 發明出來的
- OO 帶來的好處
- 以系統架構而言,通過使用 polymorphism 脫離程式碼的依賴取回控制權(程式碼的擺放位置變得自由)
- OO 讓 polymorphism 的使用變得更安全(可能是透過 .new)
 
- OO 讓 polymorphism 的使用變得更安全(可能是透過 
- Plugin architecuture 減少依賴,讓各部件可以獨立開發和部屬
 
- 以系統架構而言,通過使用 polymorphism 脫離程式碼的依賴取回控制權(程式碼的擺放位置變得自由)
Functional Programming
- 增加限制在 assignment statement
- imposing discipline on assignment statement
 
- immutability architecture
- 任何的同步問題都是由可修改的變數而產生的,如 race condition, deadlock
 
- 隔離可變性
- 區分 immutable 和 mutable components
- immutable component 可以跟多個 mutable component 溝通
- mutable component 要用 transactional memory 保護變數
- RDMS database
 
- 盡可能地讓所有處理是 immutable 的
- pure function
 
 
- Event Sourcing
- 類似區塊鏈的概念
- 只有新增讀取,沒有刪除更新
- 前提是要有無窮的空間和處理效能