The Joy of Clojure – Clojure philosophy(1)
The Clojure way
Simplicity, 簡(jiǎn)約
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將復(fù)雜性分為兩種, Essential complexity, Accidental complexity
舉個(gè)例子, 你開發(fā)報(bào)稅項(xiàng)目, 復(fù)雜的稅費(fèi)的計(jì)算屬于Essential complexity, 這部分復(fù)雜度是必須的; 而你使用負(fù)責(zé)的訪問(wèn)者模式設(shè)計(jì)帶來(lái)的復(fù)雜度, 就屬于Accidental complexity, 這種復(fù)雜度是不屬于問(wèn)題本身的
首先simple不是指clojure容易學(xué)或容易上手.
面對(duì)復(fù)雜問(wèn)題, 需要首先把complexity分為Accidental和Essential兩種, 對(duì)于Essential部分, 無(wú)法避免.
所以simple的語(yǔ)言, 意思是盡量減少解決復(fù)雜問(wèn)題時(shí)的accidental complexity, 而clojure就是這樣的語(yǔ)言.
典型的例子, oo方案, 類定義, 設(shè)計(jì)模式, 繼承等都屬于accidental的復(fù)雜度, 不屬于問(wèn)題本身的essential復(fù)雜度, 而clojure使用簡(jiǎn)單的function就把a(bǔ)ccidental的復(fù)雜度降到很低. 又比如并發(fā)問(wèn)題,鎖機(jī)制屬于accidental的復(fù)雜度, 而clojure通過(guò)數(shù)據(jù)immutalbe和可變狀態(tài)管理機(jī)制大大降低復(fù)雜度.
?
Freedom to focus
如果code可以被認(rèn)為是藝術(shù)的話, 那么創(chuàng)作最怕的就是被打斷, 復(fù)雜的語(yǔ)言總是要停下來(lái)思考語(yǔ)言的特性而阻礙真正解決問(wèn)題的思路.
所以真正好用的語(yǔ)言, 是當(dāng)你用來(lái)編寫代碼和算法的時(shí)候, 根本意識(shí)不到語(yǔ)言的存在, 而不是總是在考慮一堆由語(yǔ)言本身所帶來(lái)的復(fù)雜度
所以focus其實(shí)還是由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是動(dòng)態(tài)語(yǔ)言, 還通過(guò)macro提供語(yǔ)言擴(kuò)展, 使使用者具有極大的自由度
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, 實(shí)用為王
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.
對(duì)Java libraries的封裝, 如果加層對(duì)API的封裝更pure和beatiful一些, 但是出于實(shí)用考慮, 選擇的方案是直接調(diào)用
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.
對(duì)JVM的選擇, 也是實(shí)用為王的體現(xiàn)
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.
?
總結(jié),
Function 一類公民
代碼和思維的簡(jiǎn)潔
Data immutable, persistent
數(shù)據(jù)操作的簡(jiǎn)潔, 尤其對(duì)于并發(fā)
將identity和state兩個(gè)概念, 由傳統(tǒng)的變量的概念中分離開, 利于思維的清晰
Macro
語(yǔ)言的擴(kuò)展性, 人人都可以定制自己的DSL
Based on JVM and Java Libs
實(shí)用性, 可以迅速重用龐大的Java庫(kù)來(lái)搭建系統(tǒng)
Code is Data, no syntax
語(yǔ)言的簡(jiǎn)潔性和一致性, 不需要太多的特殊語(yǔ)法
轉(zhuǎn)載于:https://www.cnblogs.com/fxjwind/archive/2013/03/01/2938817.html
總結(jié)
以上是生活随笔為你收集整理的The Joy of Clojure – Clojure philosophy(1)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ADO.NET学习笔记--数据分组
- 下一篇: 环境配置 2