Beauty in software and literature

Jun 2017

For the few who have a passion for both literature and software(I take the pleasure of considering myself one of these), there is a great semblance in the beauty that is seen in an artistically written piece of literature and that evident in a well crafted piece of software. The reverse is also true. The ugliness seen in either of these likewise bears the same semblance, arguably caused by ignoring similar principles and ingredients requisite for rendering such works elegant.

The beauty I am speaking of is evident on the first glance yet an attempt to describe it is no mean feat. Anton Chekhov, the great Russian author knew of this. In The Beauties, a short story of his, we see the following lines(not taken verbatim):

I saw the bewitching features of the most beautiful face I have ever met in real life or in my dreams. Before me stood a beauty, and I recognized that at the first glance as I should have recognized lightning... I am ready to swear that Masha was a real beauty, but I don't know how to prove it.

Even if a perfect description of that beauty is above our reach, an attempt at the same should be considered a noble endevour worthy of praise. I proceed herein to enumarate the marks of an elegant piece of software or literature, and​ in passing to make mention of the distinguishing marks of a poor piece of software or literature.

We will make liberal use of the terms ‘coder’ and ‘writer’ all through. We take a writer to be anyone who finds pleasure in literacy work and a coder one who finds it in writing software. It is to be noted these are not hawked as definitions with precise scientific accuracy.

A clear goal

When a writer puts the thoughts, fears and desires of his or her heart in a composition, or when a lonely coder pours forth line after line of code at midnight in a lonely room, it is usually towards a well known goal. This goal can be something as important and wonderful as describing the angelic features of a beautiful lady, or as mean as attempting to break into the software of a financial institution known to be using a compromised encription algorithm. In both cases, the goal is known from the beginning.

Experience(not necessarily mine) has shown that the clearer this goal is known in the simple terms possible at the beginning, the higher the chances of the whole endevour to prove fruitful at the end. Many a writer and coder have ended up sheding tears because they didn’t clearly know what they were attempting to do. For the latter it may not only be frustration and wastage of time, but it may also be baptized with a wastage of lots of money and other costly resources. Every well written piece of literature or software has a clearly defined goal that it aims at.

Coherence

When reading an elegant piece of code or literature, one is usually struck by the seamless flow of thought and ideas through the whole. Everything seems to have been said or placed at the perfect place. The secret to this lies in focusing on the goal at hand and resisting any temptation to add even one more single line which is not an essential part of the whole. The writer whose sole goal is to describe a river has no business describing buildings and leopards and chairs.

The fewer the lines, the better

Closely related to this, a good writer should always try to express something with as few words as possible, insofar as the meaning is not injured or mangled in any way. All meaningless repetitions are shunned because they not only dilute the flow of things but can render the whole creation chaotic. The less experienced are they ones who are more prone to fall into this. Though the writers of literature at times claim exception from this in the name of emphasis, to the writers of software this principle is central to their art. The writers of software, being always overzealous for acroymns, gave this principle an acroymn. They call it ‘DRY’- Don’t repeat yourself. The following words succintly attesting to this are generally attributed to Blaise Pascal:

I only made this letter longer because I had not the time to make it shorter.

There is beauty in simplicity

Again, with a few exceptions, a wonderful creation of literature or software is usually marked with a simplicity that makes it beautiful and natural. All complex and cryptic wordings and flows of thought are shunned for more simpler options which convey the same meaning. Unfortunately, of all the beautiful things there are in the world to boast of, it is a painful wonder that some boast in being intentionally cryptic and obscure. The following words by St. Gregory of Nyssa are a guiding principle to the true coder or writer:

A writer of sense should have, I take it, for his chiefest object not to be admired above all other writers, but to profit himself and them, the many.

I take the following satirical piece from Jonathan Swift’s A Modest Proposal to be an example of simplicity and elegance:

I shall now therefore humbly propose my own thoughts, which I hope will not be liable to the least objection. I have been assured by a very knowing American of my acquaintance in London, that a young healthy child well nursed, is, at a year old, a most delicious nourishing and wholesome food, whether stewed, roasted, baked, or boiled; and I make no doubt that it will equally serve in a fricasie, or a ragoust.

The same with the following simple snippet taken from the Django web framework:

# django.utils.datastructures
class OrderedSet:
    """ A set which keeps the ordering of the inserted items. Currently backs onto OrderedDict. """

    def __init__(self, iterable=None):
        self.dict = OrderedDict(((x, None) for x in iterable) if iterable else [])

    def add(self, item):
        self.dict[item] = None

    def remove(self, item):
        del self.dict[item]

    def discard(self, item):
        try:
            self.remove(item)
        except KeyError:
            pass

    # .....

Subscribe to our Newsletter

Receive updates when we add new content. No spam or some funny tricks.