编程的97件事——5、美在简洁之中
美在简洁之中
有一句格言我任务应该是所有的软件开发者都应该知道并且铭记于心的:
风格、融洽、优雅和节奏之美都是基于简洁的。
——Plato
一言以蔽之,我认为这句话概括了我们软件开发人员应该追求的价值。
对于代码我们有很多追求的东西:
- 可读性
- 可持续性
- 开发效率
- 美感
Plato告诉我们实现这些品质的因素就是代码的简洁性。
什么是漂亮的代码,这是一个主观性很强的问题。对美的看法很大程度依赖于个人背景,就像我们许多人对任何事物的看法都是基于自己的背景。学艺术的人对美的看法和学科学的人就有不同的视角(至少有这方面的倾向)。艺术专业的人倾向于在软件作品和艺术作品的比较中找寻软件之美,而科学专业的人倾向于谈论对称性、黄金比例,试图将美简化成公式。以我的经验看来,简洁性是这两方大部分论点的基础。
想想你曾经研究过的代码。如果你不曾花时间研究过别人的代码,现在就停止阅读这篇文章去找些开源代码来研究。去网上找一些知名的技术专家写的、你选择的编程语言的代码。
你回来了?很好。我们说到哪了?啊,是的……我发现和我产生共鸣的、我认为美的代码有许多共性。其中最主要的就是简单性。我发现无论应用或系统整体上有多复杂,每个独的部分必须保持简单。这些简洁的对象都有相似的简单性,包含单一的职责、方法高内聚并且方法名是自描述的。有人认为5-10行的短方法过于极端,有的编程语言很难做到这一点,但我认为这种简洁是我们应该追求的目标。
底线是漂亮的代码是简单的。每一个独立模块保持单一的职责并与系统中的其他模块保持简单的关系。在系统的生命周期里,通过干净、简单、可测试的代码,保持高效的开发,我们的系统就能经受住时间的考验。
Beauty Is in Simplicity
There is one quote that I think is particularly good for all software developers to know and keep close to their hearts:Beauty of style and harmony and grace and good rhythm depends on simplicity. — PlatoIn one sentence I think this sums up the values that we as software developers should aspire to.
There are a number of things we strive for in our code:
* Readability
* Maintainability
* Speed of development
* The elusive quality of beauty
Plato is telling us that the enabling factor for all of these qualities is simplicity.
What is beautiful code? This is potentially a very subjective question. Perception of beauty depends heavily on individual background, just as much of our perception of anything depends on our background. People educated in the arts have a different perception of (or at least approach to) beauty than people educated in the sciences. Arts majors tend to approach beauty in software by comparing software to works of art, while science majors tend to talk about symmetry and the golden ratio, trying to reduce things to formulae. In my experience, simplicity is the foundation of most of the arguments from both sides.
Think about source code that you have studied. If you haven't spent time studying other people's code, stop reading this right now and find some open source code to study. Seriously! I mean it! Go search the web for some code in your language of choice, written by some well-known, acknowledged expert.
You're back? Good. Where were we? Ah yes... I have found that code that resonates with me and that I consider beautiful has a number of properties in common. Chief among these is simplicity. I find that no matter how complex the total application or system is, the individual parts have to be kept simple. Simple objects with a single responsibility containing similarly simple, focused methods with descriptive names. Some people think the idea of having short methods of five to ten lines of code is extreme, and some languages make it very hard to do this, but I think that such brevity is a desirable goal nonetheless.
The bottom line is that beautiful code is simple code. Each individual part is kept simple with simple responsibilities and simple relationships with the other parts of the system. This is the way we can keep our systems maintainable over time, with clean, simple, testable code, keeping the speed of development high throughout the lifetime of the system.
Beauty is born of and found in simplicity.
By Jørn Ølmheim

浙公网安备 33010602011771号