<rant>
There’s a common phrase used within software development:
If the only tool you have is a hammer, everything looks like a nail
It exists in various forms, but the original source is the Law of the instrument, or Maslow’s Hammer.
I started thinking about this while reading through Head First Design Patterns today (a bizarre book, not quite sure yet whether I can recommend it).
What struck me was that the solution to every design problem in Java revolves around writing a new class. It just astounds me that there’s basically no other means of abstraction in the language. I’ve got so used to languages with lambdas, for example, that the idea of creating aLightOnCommand
class that implements the Command
interface to encapsulate the act of turning a light on feels like such a waste of brainpower, bytes, and sanity. I’d hate to have my expressivity limited so heavily.
</rant>
在软件开发领域,有一句话经常被拿来用:
如果你手头只有锤子,那所有东西看起来都像钉子
这句话有各种各样的类似的说法,但最初的来源是工具定律,也叫做马斯洛的锤子。
我是今天在阅《Head First 设计模式》这本时想到这个故事的。(一本奇书,不确定我是否该推荐你们读这本书。)
让我印象深刻的东西是,对于任何设计上的问题,Java里给出的解决方法都是创建一个新类。我很吃惊,这种语言里基本上没有第二种用于抽象代码的方法。我已经用惯了带有lambdas语法的语言,所以,举个例子,为了封装一个“开灯”的动作,你需要创建一个LightOnCommand类来实现Command接口,这种做法感觉就是浪费脑力、字符,精神。我讨厌这严重受限的代码表达能力。
马斯洛的锤子
马斯洛的各种言论中,有一个关于锤子的比喻,是在提醒主观与客观的相互作用,被他多次重复,十分重要。
下面就是马斯洛的锤子语录:
- "When the only tool you have is a hammer, every problem begins to resemble a nail."
- 当你手里唯一的工具是锤子时,所有的问题看起来就像是钉子。
- "He that is good with a hammer tends to think everything is a nail."
- 善用锤子的人倾向于把所有的事都看成钉子。
- "If the only tool you have is a hammer, you tend to see every problem as a nail."
- 假如你手里唯一的工具是锤子,你容易把所有的问题都看成钉子。
- "If you only have a hammer, you tend to see every problem as a nail."
- 假如你仅有锤子,你就倾向于把所有问题看成钉子。
- "To the man who only has a hammer in the toolkit, every problem looks like a nail."
- 即如一个人的工具箱内只有锤子,所有的问题就看起来像钉子。
- "When all you own is a hammer, every problem starts looking like a nail."
- 当你所有的拥有仅仅是锤子,所有的问题就开始看起来像钉子。