The Joy of Clojure – Clojure philosophy(1)
The Clojure way
Simplicity, 簡約
It’s hard to write simple solutions to complex problems. But every experienced programmer has also stumbled on areas where we’ve made things more complex than necessary, what you might call incidental complexity as opposed to complexity that’s essential to the task at hand (Moseley 2006).
Essential complexity is inherent in the problem domain, whereas accidental complexity is introduced by things external to the problem domain. For example, in a software project that deals with filing taxes, complexity that arises from convoluted tax-codes is part of the domain, and hence essential. Any complexity that arises from, say, employing the rather intricate visitor pattern, is accidental.
Fred將復雜性分為兩種, Essential complexity, Accidental complexity
舉個例子, 你開發報稅項目, 復雜的稅費的計算屬于Essential complexity, 這部分復雜度是必須的; 而你使用負責的訪問者模式設計帶來的復雜度, 就屬于Accidental complexity, 這種復雜度是不屬于問題本身的
首先simple不是指clojure容易學或容易上手.
面對復雜問題, 需要首先把complexity分為Accidental和Essential兩種, 對于Essential部分, 無法避免.
所以simple的語言, 意思是盡量減少解決復雜問題時的accidental complexity, 而clojure就是這樣的語言.
典型的例子, oo方案, 類定義, 設計模式, 繼承等都屬于accidental的復雜度, 不屬于問題本身的essential復雜度, 而clojure使用簡單的function就把accidental的復雜度降到很低. 又比如并發問題,鎖機制屬于accidental的復雜度, 而clojure通過數據immutalbe和可變狀態管理機制大大降低復雜度.
?
Freedom to focus
如果code可以被認為是藝術的話, 那么創作最怕的就是被打斷, 復雜的語言總是要停下來思考語言的特性而阻礙真正解決問題的思路.
所以真正好用的語言, 是當你用來編寫代碼和算法的時候, 根本意識不到語言的存在, 而不是總是在考慮一堆由語言本身所帶來的復雜度
所以focus其實還是由Simplicity所保證的...
Writing code is often a constant struggle against distraction, and every time a language requires you to think about syntax, operator precedence, or inheritance hierarchies, it exacerbates the problem.
Clojure tries to stay out of your way by keeping things as simple as possible, not requiring you to go through a compile-and-run cycle to explore an idea, not requiring type declarations, and so on. It also gives you tools to mold the language itself so that the vocabulary and grammar available to you fit as well as possible to your problem domain—Clojure is expressive.
而更重要的是Freedom, clojure除了象python是動態語言, 還通過macro提供語言擴展, 使使用者具有極大的自由度
One key to delivering this freedom is a commitment to dynamic systems. Almost everything defined in a Clojure program can be redefined, even while the program is running: functions, multimethods, types, type hierarchies, and even Java method implementations. Though redefining things on the fly might be scary on a production system, it opens a world of amazing possibilities in how you think about writing programs.
Empowerment, 實用為王
Some programming languages have been created primarily to demonstrate some nugget of academia or to explore certain theories of computation. Clojure is not one of these. Rich Hickey has said on numerous occasions that Clojure has value to the degree that it lets you build interesting and useful applications.
If a decision about some design point in Clojure had to weigh the trade-offs between the practical solution and a clever, fancy, or theoretically pure solution, usually the practical solution won out.
對Java libraries的封裝, 如果加層對API的封裝更pure和beatiful一些, 但是出于實用考慮, 選擇的方案是直接調用
Clojure could try to shield you from Java by inserting a comprehensive API between the programmer and the libraries, but this could make the use of third-party Java libraries more clumsy. So Clojure went the other way: direct, wrapperfree, compiles-to-the-same-bytecode access to Java classes and methods.
對JVM的選擇, 也是實用為王的體現
The decision to use the Java Virtual Machine (JVM) itself is a clear example of this practicality. The JVM has some technical weaknesses such as startup time, memory usage, and lack of tail-call optimization(TCO). But it’s also an amazingly practical platform—it’s mature, fast, and widely deployed.
?
總結,
Function 一類公民
代碼和思維的簡潔
Data immutable, persistent
數據操作的簡潔, 尤其對于并發
將identity和state兩個概念, 由傳統的變量的概念中分離開, 利于思維的清晰
Macro
語言的擴展性, 人人都可以定制自己的DSL
Based on JVM and Java Libs
實用性, 可以迅速重用龐大的Java庫來搭建系統
Code is Data, no syntax
語言的簡潔性和一致性, 不需要太多的特殊語法
轉載于:https://www.cnblogs.com/fxjwind/archive/2013/03/01/2938817.html
總結
以上是生活随笔為你收集整理的The Joy of Clojure – Clojure philosophy(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ADO.NET学习笔记--数据分组
- 下一篇: 环境配置 2