Monday, January 22, 2018

Simple Made Easy

話說我長久以來一直有個問題:『看不懂別人寫的 OOP style 程式碼。』學了 Clojure 之後,這個「看不懂」似乎有理由了。

OOP style 的東西,當一組 class/object 被定義生成時,有太多可能的語意 (semantic) ,要猜的可能性太多。

可能是:
1 values object , 在  clojure 會用單純的 hashmap 或是 records 來處理。
2 entity object, 在 clojure 會用 atom 處理。
3 function with state, 在 clojure 會用 lexical closure 來處理。
4 純脆只是 namespace 功能,為封裝而封裝
5 multimethod 的語意, 在 clojure 會用 defmulti 又或是  protocol + reify 來實現。

最近偶然又重看了一遍 Rich Hickey 的 Simple Made Easy 。發現類似的概念,他已經討論過了。在 Simple Made Easy 這個 talk 裡。object 被 Rich 歸類為 complex construct ,難以做出 simple artifact 。而太 complex 的東西會讓人腦的認知能力超載,因為要同時考慮太多元件。

該 talk 中有許多深具啟發的句子和概念:
1. All too often we do what is easy, at the expense of what is simple.
2. The benefits of simplicity are: ease of understanding, ease of change, ease of debugging, flexibility.
3. Reliability tools: testing, refactoring, type systems are good but secondary. They do not enforce simplicity. They are just safe net.
4. Build simple system by choosing construct that generate simple artifacts and by abstracting: design by answering questions related to what, who, how, where, when, and why.