Code as Art

Andrew (he/him) - Aug 17 - - Dev Community

Banner Image: Quine Relay -- Copyright (c) 2013, 2014 Yusuke Endoh (@mametter), @hirekoke

Digital Art

In almost all circumstances, code is a means to an end.

The phrase "computer programming" itself describes the activity of programming a computer to accomplish a particular task. Often, that task is logical. Most computer programs do something "useful", whether that's calculating the best route from your home to work, balancing a budget, or running your smart fridge's Twitter client.

But many programs are written solely to produce something of aesthetic value.

Recently, Generative AI applications like Midjourney, which can produce visual art from text prompts, have seen a lot of press. But consider also dwitter.net, where JavaScript programs of 140 characters or fewer create moving 3D landscapes, gyrating fractals, and even self-playing games of Pong. And also programmers like Grzegorz Witczak, Ben Evans, and Diana A. Smith who create photorealistic works of art using only CSS, a language otherwise used mainly to style fonts, borders, and backgrounds on web pages.

Digital art is not a new phenomenon. Nearly as long as computers have existed, programmers have been using them to express themselves aesthetically.

Traditional artists have been inspired by digital art, as well. Consider Dalí's Painting of Gala looking at the Mediterranean sea which from a distance of 20 meters is transformed into a portrait of Abraham Lincoln (Homage to Rothko), produced in 1976, which appears to show a pixelated image of Abraham Lincoln when viewed from a distance.

For most digital artists, code is a medium, like marble for a sculptor, or a canvas for a painter. Code is the material through which the art is expressed. Though it takes time and skill to produce, a canvas itself is not usually seen as art itself. But could it be?

Readability

Developers often talk about writing "clean" or "elegant" code. Most programming languages are "general-purpose", meaning they can be used to accomplish a variety of tasks. Accordingly, there are often many different ways to accomplish the same task, using the same programming language.

Robert C. Martin's Clean Code: A Handbook of Agile Software Craftsmanship gives many examples of "unclean" code, with tips and rules of thumb for rewriting the code in a "clean" way.

In the overwhelming majority of cases, the most important aspect of code is its functionality -- usually to perform some computational task. A secondary aspect of the code is its readability, closely related to the "cleanliness", "elegance", or "aesthetics" of the code.

As a quick example, consider this 3x3 matrix

val identity = Array(1, 0, 0, 0, 1, 0, 0, 0, 1)
Enter fullscreen mode Exit fullscreen mode

Which elements are on the diagonal of this matrix?

It's much more readable if we manually format it as follows

val identity = Array(1, 0, 0,
                     0, 1, 0,
                     0, 0, 1)
Enter fullscreen mode Exit fullscreen mode

These two arrays are functionally identical. But aesthetically, the source code used to construct the latter array is more "readable" than the former. Functionality is paramount, but aesthetics are a close second.

But what if a program (or an entire programming language) were designed primarily for its aesthetic qualities?

Unreadability

Formatting code for readability -- or to follow established patterns and idioms -- is the norm. But some programmers do the opposite (intentionally or otherwise).

Every year, the International Obfuscated C Code Contest (IOCCC) accepts submissions of C code which accomplish some task, but do it in an unusual, hard-to-decipher way. These programs are functional, but are purposely formatted so as to be as unreadable as possible (they are obfuscated).

Some programming languages have been designed specifically to accomplish as much as possible in as few characters as possible. APL is a "serious" (non-"esoteric") and very terse programming language which programmers may be familiar with, but many code golf languages have been created specifically to minimize source code size in a similar way.

And some programmers choose to format their code so that their entire ray-tracing C++ program can fit on the back of a business card, or so the source code of their ouroborous quine written in Ruby looks like a dragon (see the header image of this blog post), or so that they look like and can be read as poems. All of these examples use "serious" programming languages in an "unserious" way.

Esoteric Languages

There are also many programming languages where the aesthetics of the source code is more important than what is actually written in that code. As most programming languages are written using ASCII or Unicode character sets, some of these look like ASCII art...

    _, ._
  ( ・ω・) んも〜
  ○={=}〇,
   |:::::::::\, ', ´
、、、、し 、、、(((.@)wvwwWWwvwwWwwvwwwwWWWwwWw
wWWWWWWwwwwWwwvwWWwWwwvwWWW
Enter fullscreen mode Exit fullscreen mode

...though most just look like jumbled messes of characters

 72_1_108::_3+:}
    0          _ """
     11_{78_23_" " "
     4         4 ".{@
     _         " {
     1           }"
     08_100_33"""""
Enter fullscreen mode Exit fullscreen mode

Some esoteric programming languages are non-textual. The source code of these programs can be visual, as with Piet

or Nice (which does not yet have a working interpreter)

Other languages are auditory, meaning programs are encoded as sound. Usually, these programs can also be written as individual notes, or as sheet music

Listen to the above program

In many cases, the simplest implementation of a program in these languages is not the most aesthetically pleasing. Programmers will opt for beautiful programs over efficient ones.

For example, the following Piet program gets a number as input from the user, squares it, and prints the result

A sample Piet program

...but so does this one

A simple Piet program

Code as Art

"There is no generally agreed definition of what constitutes art" [Wikipedia]

Even in languages which aren't specifically designed to have "beautiful" source code, some programmers opt to format their code in ways which trade efficiency, practicality, or readability for aesthetic appeal.

There is no single, correct solution to a problem which can be solved by a program. Different programs can accomplish the same task in different ways. Weighing tradeoffs and choosing one solution over another is part of the art of computer programming.

The human desire to be creative and express one's individuality does not end where a keyboard begins. Hundreds of esoteric programming languages can attest to that.

Whether textual, visual, or auditory, code can be used as a medium to create art, but it can also be art itself.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .