Beauty Is in Simplicity
文章链接: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. — Plato
In 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
大美至简
有一句语录,我认为让软件开发者了解并铭记会大有裨益.他是这样说的:
形式之美,和谐之美,优雅之美,以及韵律之美都取决于简单. ——柏拉图
我认为这一个句子就总结了我们作为软件开发者应该有的追求.
这里有一些事情我们在代码中需要努力达到的:
- 可读性
- 可维护性
- 开发速度
- 那些难以捉摸的美的品质.
柏拉图告诉我们,所有这些品质的促成因素是简单。
什么是美的代码?
这可能是一个非常主观的问题。对美的认知很大程度上取决于个人背景,正如我们对事物的认知很大程度上取决于我们的背景一样。
学艺术专业的人对美的认知(至少在方法上)和学科学专业的人有不同的看法。艺术专业的学生倾向于通过比较软件和艺术作品来追求软件的美,而科学专业的学生则通常谈论对称性和黄金比例,并试图将事物简化为公式。
以我的经验,简单是双方大多争论的基础。
想想你学过的源代码。
如果你还没有花过时间学习其他人的代码,那么请停止阅读本文,去找一些开源代码学习。
请认真对待这个建议!我是认真的!
就你所使用的编程语言,在网上搜索由一些知名的、公认的专家编写的源代码。
你回来了?那好。我们说到哪了?哦哦,是的……
我发现,那些能让我产生共鸣的代码和我认为美的代码有很多共同的属性。其中最主要的就是简单。
我发现,无论整个应用或系统多么复杂,各个部分都必须保持简单。比如,对象职责单一并包含同样简单的内聚的有良好描述性名称的方法[1]。
有些人认为做到每个方法短小到只有5到10行代码这种想法是极端的,而且有些编程语言很难做到这一点,但我认为尽管这样达到如此简洁仍然是一个值得追求的理想目标。
最重要的是,美的代码就是简单的代码。
通过简单的职责以及和系统其他部分的简单关系,每个单独的部分保持简单。这就是我们能一直保持系统可维护性的方法,基于简洁的、简单的和可测试的代码,确保系统在整个生命周期内保持高速研发[3]。
美源于也存在于简单之中
——Jørn Ølmheim