Appreciating Beauty in Code

Apparently Jeff Attwood has commented that there can be no beauty in the code, but can only be in the algorithm.

I agree with Maarten that he is not entirely right. A given algorithm can be expressed in different ways and the piece of code which demonstrates the idea very elegantly is beautiful code. Consider, for example, swapping a segment of an array. One solution could be:

void rev(int a[], int p, int q) {
  for(int i = (p+q)/2; i >= p; i--) swap(a, i, q-(i-p));
}

The same algorithm can be expressed more elegantly like this:

void rev(int a[], int p, int q) {
  for(; p<q; p++,q--) swap(a, p, q);
}

One person in the comments section expresses the above code even more elgantly like this:

void rev(int a[], int p, int q) {
  while (p<q) swap(a, p++, q--);
}

The idea is the same, but expressions are different. And I believe beauty is all about expression – nature has expressed its beauty in the form of greenery, programmer expresses beauty in the form of code. That does not mean that idea and algorithms are not beautiful, it just means that code too can be beautiful.

And what is it is to feel the beauty? One Dik Winter (one of the students of the author) wrote this at the end of the code (for cyclical shifting) – giving a different solution all together:

Like as the waves make towards the pebbled shore,
So do our minutes hasten to their end,
Each changing place with that which goes before
In sequent toil all forwards do contend.

Clearly, Dik has felt the beauty of algorithm and has expressed it in this poem. That is a mark of brilliance. The code talks about cyclical shifting of array elements and this is what Dik wrote:

void cycle(int a[], int n, int k, int start) {
// executes single cycle starting from "start"
    int i = start;
    int j = (i+k)%n;
    int saved = a[i];
    // save element at first location of cycle
    while (j != start) {
      a[i] = a[j]; // shift element
      i = j; j = (j+k)%n; // shift indexes
    }
    a[i] = saved;
}

void cycShift(int a[], int n, int k) {
// shift a[0..n-1] to the left by k places, cyclically
  assert((0 < k) && (k  0));
  int start = 0; // index where cycle begins
  for (int i = gcd(n,k); i > 0; i--)
    cycle(a, n, k, start ++);
}

While what the code says above is clear, I am yet to fully understand why looping for gcd(n,k) times, we will cycle all the elements. Moreover, the above example also says beauty is not visual  – the above piece of code may not “look” beautiful, but is indeed beautiful when we understand what it does.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s