Code as Literature

XKCD: e to the π times i © Randall Munroe - Creative Commons Attribution-NonCommercial 2.5 License.

One of main activity of a programmer is to read code. Readable code is something that many talk about, but few achieve. The core problem is that computer code is written for two completely different readerships: on one a program, typically a parser or a compiler, on the other hand, fellow programmers. The first will do exactly what your code says, the others are generally trying to understand what you are trying to do with the code. You learn to master the first aspect of the code when you write it alone, or at school. The second part is more fuzzy, you learn it quite late, typically in projects where you have to interact with teams. While there are many theories on the way of writing clear code, they tend to be vague and contradictory, with opposing philosophies yielding different languages. In the end my definition of unclear code is similar to Oscar Wilde’s definition of vulgarity: “other women”.

One school of though in code writing is the one of elegance: code should minimal, with a minimal amount of boilerplate and specification. In essence code should be like haiku poetry or mathematical formulae. I used to like this idea and hate languages that force the programmer to write boilerplate code, but I think it suffers from a basic problem, those two forms are lousy for telling things to humans. Haiku poetry is good a communicating something very subjective, the feeling of an instant. Mathematical formulae are very good to convey mathematical truths, but lousy at explaining what they mean. Equations like eπ⁢i = -1 are very elegant, but take a long time to understand. Having a text that requires satori moments to go forward might be fine for learning complex subjects, like mathematics or philosophy, it is very inconvenient in code, and let’s face it, most of the computer code is not that subtle.

For me, good code should be readable as flow, closer to a good scientific paper: with precise terms, relatively standard structure, relevant references, and occasional equations, typically explained in a specific paragraph… In the end, all fairy tales start with the same text «Once upon a time» and nobody thinks this is boilerplate.

6 thoughts on “Code as Literature

  1. Si seulement tout le monde était comme toi…

    Le code EST une œuvre d’art. Hors des cas dégénérés « sportifs » comme les démos warez ou l’IOCCC, un code bien fichu doit être comme tu dis clair comme un papier scientifique, mais sa simplicité (quand il ne reste rien à enlever) est un critère majeur. Oui, je hais le « boilerplate ».

  2. Je suis complètement d’accord avec toi, Thias… Incroyable non ? :-) Bon, en dehors de tous les défauts qu’on peut trouver à l’implémentation de Python, le langage en lui-même satisfait presque tous mes besoins en matière de clarté. J’aimerais un peu plus de détails sur le typage (même si c’est seulement pour faire des garde-fous et pas un typage dur) et un mécanisme de packaging et de distribution moins bricolé sur un coin de table.

    Un autre langage que je trouve intéressant, enfin plutôt qui me fascine dans ce cas, c’est Inform7 ( http://inform7.com/ ). C’est un langage pour écrire des fictions interactives, et c’est un bon exemple de langage littéral.

    Un exemple de code en Inform7 : http://inform7.com/learn/eg/bronze/source.txt

    Etonnof niet ? ;)

  3. Je ne suis pas d’accord, le code n’est pas une œuvre d’art, et ceux qui pensent que c’est de l’art sont à mon avis un problème. L’art peut être incompréhensible, obscur, ambigu, et ne doit surtout pas être fonctionnel ou consensuel. Écrire du code est un artisanat, une technique. Tout code génial est par définition illisible…

  4. Je ne dirais pas “génial”, mais “astucieux”, “malin”, voire “intelligent”. J’ai rencontré du code génial dont la simplicité était stupéfiante, mais rarement du code astucieux clair et compréhensible sans efforts cérébraux intenses.

  5. Le code est de l’art. Comme des équations mathématiques ou physiques, ça peut être beau et profond (je me suis fait calligraphier les équations de Boltzmann par ma douce pour les accrocher au mur). Y a un côté Zen. Ou comme un barrage : gros et lourd mais avec une certaine beauté. Ou comme une cathédrale : inmaintenable et rustiné de partout mais avec de la personnalité.

    Si c’est obscur ou ambigu, certes ce n’est pas du code – ou alors un exercice de style genre OCCC.

    Après on peut discuter ce que c’est que de l’art mais y a déjà eu des livres entiers écrits là-dessus

  6. Pour compléter : effectivement au boulot on te paye d’abord pour faire une tâche, pas une œuvre. L’essentiel de ce que je produis n’a rien d’artistique, juste utilitaire. Il n’empêche. Même quand je rédige un mode d’emploi je m’attache au côté (très modestement ) littéraire de la chose, ça fait partie de la mise en forme.

Leave a Reply to ThiasCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.