Coding Elegance

January 11th, 2008 § 4 comments

The equivalence between elegance, beauty and correction is almost an axiom in the field of mathematics. Bertrand Russell expresses this correlation thus:

Mathematics, rightly viewed, possesses not only truth, but supreme beauty–a beauty cold and austere, like that of sculpture, without appeal to any part of our weaker nature, without the gorgeous trappings of painting or music, yet sublimely pure, and capable of a stern perfection such as only the greatest art can show. The true spirit of delight, the exaltation, the sense of being more than Man, which is the touchstone of the highest excellence, is to be found in mathematics as surely as poetry.

Bertrand Russel, The Study of Mathematics

Code, once we consider its mathematical roots, presents the same intrinsic correlation. Although it is too much mutable to evoke the cold and austere beauty to which Russell alludes, the fact that code and its other products exhibits the same aesthetic imperatives is obvious even to the most inexperienced programmers. Even users can occasionally apprehend those aspects of code when they talk about the way a given application works and how functional and usable it is.

Most of that elegance derives from the incremental economy one can achieve by successively refining a body of code. The author of The Little Prince describes those steps with the following words:

Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.

Antoine de Saint Exupéry, Terre des Hommes

Exupéry criterion is an excellent validation tool for what code should be–and by extension, any of its products–in its final form. There is beauty and perfection to be found in code, to borrow Russell words, as surely as there is beauty and perfection in the most cherished poems.

To the intelect of programmers, this beauty is visually clear in what they product, easily expressed in the successive reductions they can perform to achieve a core of functionality that will stand the test of time. Obviously, that perfection depends both on the programmer and the tools he chooses to employ, but it’s available to any practitioner of the craft willing to make the effort to become a master craftsman. As another great programmer said:

Ugly programs are like ugly suspension bridges: they’re much more liable to collapse than pretty ones, because the way humans (especially engineer-humans) perceive beauty is intimately related to our ability to process and understand complexity. A language that makes it hard to write elegant code makes it hard to write good code.

Eric S. Raymond

Being a function of a developed sense of programming, I believe it’s possible to purposefully chose to code beautifully. It’s a matter of time and options, something about every programmer should think regularly in the course of his career. Training oneself to recognize beauty may seem far fetched, considering that reading code is much harder than writing it, but that may be the key to the task: beautiful code will be much more readable than ugly code, and that will help programmers to identify and recognize good code.

Ultimately, the challenge of every programmers is to learn to code elegance, teaching himself or herself to recognize code that meets standards of concision, simplicity and beauty–which brings us to another quotation:

Simplicity carried to the extreme becomes elegance.

Jon Franklin

My advice to those who are beginning their programming careers and also to those who are feeling that their code is becoming bloated and unwieldy is this: train yourself to code in a way that will show the problem solving intent of each line, and that your code is the best way to solve the problem at hand.

In less time than you will realize, elegance will be second nature to you, with all benefits it brings. It’s hard work, but worth it.


§ 4 Responses to Coding Elegance"

  • Kaz Maslanka says:

    I enjoyed the Russell quote even though his idea of “The greatest art” and its ‘aesthetics’ would surely differ from mine.


  • Ronaldo says:

    I think Russell would agree that even for mathematics there are multiple kinds of beauty; for example, the one you find in a well-constructed proof and the one you find in a well run simulation.

    Nice blog, by the way. 🙂

  • Kaz Maslanka says:

    Oh … Thank you Sir!


  • Kaz Maslanka says:

    Hmmmm while I am thinking about it. I find it very interesting that you mention the idea of polyaesthetics (different aesthetics for different concepts) within the field of mathematics. It seems that most people don’t realize that order to accurately discus the aesthetics of art or mathematics one must focus on the specific aesthetic in order to create criteria to judge the value and effectiveness of the beauty concerned. It disturbs me that people wish not to delineate the aesthetics but to throw everything in the same bag and call it ‘art’.


Leave a Reply

Your email address will not be published. Required fields are marked *

What's this?

You are currently reading Coding Elegance at Reflective Surface.