Hamon or the Skin Deep Beauty of Code

The craft of programming is a little depressing: you spend days and nights pushing bits inside a computer, weaving a pattern that will ignite the right pixels on monitors or magnetize the right atoms on disk platters. But it is craft nevertheless, except one where beauty matters very little. Coders create the true ukio-e – floating world, but one that is as ugly as it is fleeting.

I’ve read a good chunk of Beautiful Code: Leading Programmers Explain How They Think, but I’m still far from understanding what beauty is in code. Ugliness is self-explanatory: if you recoil in horror after reading a few files, it’s ugly. I recoil on the daily basis.

I’m not talking about the success or usefullness of things that code creates. I am pretty sure that the code that runs plentyoffish.com is as ugly as the website itself, but it does not detract from its success and utility.

I think the key to my preception of code beauty is related to “hamon”: a concept in Japanese swordmaking. If you’ll look at a Japanese sword, you’ll usually see a wavy pattern near the edge of the blade. Hamon literally means “blade pattern”. It is created when a swordsmith applies special mud to the edge of a in a wavy pattern of varying thickness and composition, and then heats the sword and plunges it into hot water. You can see the process in this video (it starts at about 1:30 mark).

The primary purpose of this treatment is to create a blade that is very hard and can take a lot of stress, which is a contradiction, as hard steel is very brittle. The edge gets the thinnest coating, and after heating and cooling turns into martensite hard crystals of high carbon steel. The thicker coated back turns into pearlite, which is a softer, but very flexible lower carbon steel. The hamon pattern is formed where these two types of steel interlock. The wavy shape makes the sword able to absorb shock better.

The absence of or inferior shape of hamon clearly indicates the inferiority of the sword. The aesthetics of it are just a coincidence, but it is also the resason why the craft of the samurai sword endures: collectors and museums purchase them as works of art. I’ve seen many swords in museums, and even though you can’t appreciate them as well as by holding them, the patterns in the hamon are hypnotic.

Here’s a good video of what hamon looks like in different ligting conditions.

There are many different styles of hamon, here are some examples from very excellent “The Craft of the Japanese Sword“:

There are of course many swords out there with fake blade patterns made with acid etch, wire brushing or other abrasives. Fakes are neither beautiful nor strong and easily identifiable.

When I look at a code listing I also see a wavy pattern. It’s nowhere near as connected to quality as the hamon, but it is still often an indicator of it. Indentation, spacing, length of keywords and function names, the amount of syntactic sugar, overall length of the listing: good code is sometimes beatiful to look at.

Sublime Text 2, a new programming editor that I am using, has an interesting feature: an insert that shows the whole file in miniature. Here are side by side beginnings of commit.c from (top to bottom) cvs, svn and git. I don’t know if anything can be determined by looking at these pretty skyscrapers of code, but aestetically I like git better.

This goes a level further – into software architecture. There’s this old chestnut of the maps of system calls in Apache and IIS web servers. You can easily tell which is the product of Redmond craftsmanship.

I would like to add that wavy patterns are the last things that I look for in code and this whole article is just a fluffy philosophical musing. Yet, maybe there’s something there in those patterns.

The (Drive)way of a Samurai

Larry Ellison’s Japanese fetish is well known. He owned at different times yachts named “Ronin”, “Katana” and “The Rising Sun”. According to “The Difference Between God and Larry Ellison” Larry often flies to Japan for Sakura Matsuri. And now he is building himself a $60 million 3000 sq. foot replica of a 16th century Japanese warlord’s mansion.

The house will be built using traditional materials and without nails (good idea earthquake-wise), but I doubt that any warlord had a house that big, especially with an amazing master bath that will include “.. boulder that will be part of the master bathroom shower. The 30-ton stone (yes, that’s 60,000 pounds) was recently moved into place with a rented high-rise crane that was brought to the site expressly to move the “shower rock.” ”

A house like that needs a driveway to match. Larry wants it paved with natural hand cut stone. The stone is quarried in Japan and China (Larry chose China because the labor is cheaper there) and for some reason needs to be cut on site. This means that the architect needs to provide a template for each and every stone. It would take months to do this by hand, but luckily a CIS student helped him, making Larry’s driveway his CS270 Project. Random, but not too random, concave and convex, angles not too sharp, 5 to 9 sides to a stone – this is not as simple as it seems :

By the way, is it just me or does Mr. Scorpio from “The Simpsons” look very much like the samurai in question?