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
语言的扩展性, 人人都可以定制自己的DSLBased on JVM and Java Libs
实用性, 可以迅速重用庞大的Java库来搭建系统Code is Data, no syntax
语言的简洁性和一致性, 不需要太多的特殊语法