The book Beautiful Code has sparked a variety of opinions. A couple of critical observations about the book:
It has a variety of authors, and the book comes across as a collection of independent essays, with each chapter standing on its own.
More often than not, there is no real discussion from the authors about what beautiful code really means to them.
I thought about my own definition of beautiful code and decided to solicit the opinions of others on LinkedIn to see if they shared my opinion. I received some great responses, and let me once again thank everyone who did respond. The majority of the respondents felt (as I do) that beautiful code is something that can be easily maintained over time, and not something expressed in an "artful" way that requires study for a period of time to appreciate its beauty – and to understand its function. There is a definite difference between was is desired in the working world and what would be acceptable as “art” in other settings.
In particular, I’d like to expand upon (and slightly modify) one response from Bidyut Raj, who provided a truly great analogy.
Beautiful code is like a diamond.
The diamond metaphor defines beautiful code and explains why beautiful code is more valuable than ugly code. The analogy covers the 4 C’s of a diamond as well as the setting of the diamond. Consider the following in terms of both the definition and value of beautiful code.
Clarity describes the purity of a diamond, and is determined by the number, size, and location of imperfections. The fewer imperfections that a diamond contains, the more beautiful the diamond is.
Ergo, beautiful code should not contain imperfections, whether these imperfections are design flaws, bugs that lead to problems during execution, or even the use of confusing, inconsistent coding standards.
The best color for a diamond is no color at all. A totally colorless diamond allows light to pass through it easily.
The interfaces of beautiful code should be intuitive, simple, and easy to use, whether the interfaces are programming interfaces that another programmer invokes or the user interface that customers utilize to interact with the software. Beautiful code should be completely readable (by humans) to the extent that the code is literally self-documenting and something that can be easily maintained over time by others. The expression of logic should be precise and understandable, which means that the code should not contain arcane tricks that can take an extraordinary amount of time for someone else to decipher.
Cut refers to the proportions, finish, symmetry, and polish of the diamond, factors that determine the fire and brilliance of a diamond.
Beautiful code should have a design and implementation that meets the functional and non-functional requirements to such a degree that anyone looking at the code recognizes the brilliance in the craftsmanship of the code. Like diamonds, this is difficult to quantitatively judge, but a great “cutter” (programmer) can significantly influence the value of the code, and can even transform existing, ugly code into beautiful code.
The carat is the unit of weight for a diamond, with value increasing with carat size.
Beautiful code should “weigh” just enough to get the job done, and no more. The weight of software should be a function of the business value that it is providing, and there should be a need – and value – derived for each and every task that the software performs. If the code weighs more than the value being derived, it should be scrapped or re-cut. Conversely, some code returns a great deal of value in comparison to its weight (and other code), which translates into higher profit margins or savings in terms operational efficiencies in some way for the owner of the code.
Diamond settings are the platform in which the diamond sits.
The setting is the business context and need that the beautiful code fulfills, and what is beautiful in one setting may not be so beautiful in another. For example, a beautiful client/server, Windows®-based application may be elegant as an in-house, single-customer solution, but it will not shine in a Software-as-a-Service setting intended for a broad customer base.
Code Isn't Beautiful
Wikipedia.org: Diamond (gemstone)
The Four C's of diamonds
And many thanks to Bidyut Raj.
18 hours ago