As an admirer of the “artistic flare, nuanced style, and technical prowess thatseparates good code from great code” explored by Robert Green and Henry Ledgardin their article “Coding Guidelines: Finding the Art in the Science” (Dec. 2011),I was disappointed by the authors’ emphasis on “alignment, naming, use of whitespace, use of context, syntax highlighting, and IDE choice.” As effective asthese aspects of beautiful code may be, they are at best only skin deep.
我对“艺术的闪耀,有细微差别的风格和将好的代码与卓越的代码区分开来的技术能力”敬佩至极,而在文章“编程指南:在科学中寻找艺术”(2011年12月)中,Robert Green和Henry Ledgard两位作者对之进行了探索。令我感到失望的是,作者强调了“对齐,命名,空格的使用,上下文的使用,句法规则的重要性,以及IDE的选择”。对于优美的代码来说,这些方面固然重要,但它们最多只是表面上很肤浅的东西。
Beautymay indeed be in the eye of the beholder, but there is a more compelling beautyin the deeper semantic properties of code than layout and naming. I alsoinclude judicious use of abstraction, deftly balancing precision andgenerality; elegant structuring of class hierarchies, carefully trading betweenbreadth and depth; artful ordering of parameter lists, neatly supporting commoncases of partial application; and efficient reuse of library code, leveragingexisting definitions with minimum effort. These are subjective characteristics,beyond the reach of objective scientific analysis—matters of taste not offact—so represent aspects of the art rather than the science of software.
虽然实际上只有旁观者才可能看到优美性,但相对于布局和命名来说,在更深层次的语义特性中有更具说服力的优美。这些更深层次的东西包括了抽象的明智使用,熟练的平衡精度和一般性;类层次的优雅组建,宽度与深度之间的合理折中;参数列表的艺术式排序,部分应用的共同事例的恰当支持;以及库代码的高效重复利用,用最小代价来改变当前定义。这些都是主观的特性,而非客观的科学分析——是味觉,而非事实的问题——因此代表了艺术方面,而非软件科学方面。
Formalizingsuch semantic properties is more difficult than establishing uniform codingconventions; we programmers spend our professional lifetimes honing our writingskills, not unlike novelists and journalists. Indeed, the great Americanessayist Ralph Waldo Emerson (1803–1882) anticipated the art in the science ofsoftware like this: “We ascribe beauty to that which is simple; which has nosuperfluous parts; which exactly answers its end; which stands related to allthings; which is the mean of many extremes.” It is to this standard I aspire.
要规范这些语义特性比建立统一的编程规则更难;我们程序员用整个职业生涯来磨练我们的写作水平,这与小说家和记者没有什么两样。事实上,美国最伟大的作家Ralph Waldo Emerson(1803–1882)像这样预言软件科学中的艺术:“我们将优美归结为简单的东西,它没有多余的部分,它精确地道出了其目的,它代表了相关的所有事物,它是许多极端情况的折中。”我崇拜的是这个标准。
Jeremy Gibbons, Oxford,U.K.
参考文献
The Beauty of Simplicity, Communicationsof the ACM, vol. 55, no. 4, April 2012.