Kōans for Zen Coding

05 Jun 2017

A discussion of kōans for Zen Buddhists, coders, and Zen Buddhist coders

Insight is Sudden

I became interested in Buddhism during my freshman year of college - a time for…you know… soul searching. Through the magic of Google I stumbled upon an article on insight meditation, also known as vipassanā in the Theravāda tradition. The goal of insight meditation is to understand the world as it really is - untainted by ones own judgments and preconceptions. A common technique when meditating is to focus on the breath and observe, but not judge, whatever thoughts flow through the mind. If one can detach from these thoughts, without fighting them or analyzing them, then one can gain insight into the objective nature of the universe without gripping to ones own sense of identity and the baggage that comes along with it.

Whew! Heavy stuff. But it’s important to lay this groundwork to get to the matter of what koans are. Now, it is worth nothing that vipassanā is specific to the Theravāda Buddhist tradition, which is commonly practiced in South Asia. It is often viewed in contrast with Mahāyāna Buddhism, which is more commonly practiced in East Asian countries. But both traditions place an emphasis on insight and how to achieve it. In the Japanese Zen school of Buddhism, it is believed that this insight, or kenshō, is sudden. That is, while daily meditation practice is important, the practitioner isn’t necessarily wiser after each day or week or even year of practice. But there will come a day when a “Eureka!” moment happens and the truths of the universe immediately become clear. This is in contrast with gradual insight, which views each meditation session as a new layer on the practitioner’s foundation of truth.

The question is: how does one arrive at this sudden insight? This is where koans come in.

A kōan (公案) is a tool used by the Rinzai Zen Buddhist sect as an object of meditation. It often comes in the form of a short question or statement to get the practitioner to challenge the tools of deductive reasoning they have used throughout life. Here is a classic one:

Two hands clap and there is a sound, what is the sound of one hand?

At first, one might try and think about the sound of an open hand slapping air particles out of the way. However, the point is that one cannot rely on logic to get an answer. By focusing intensely on the prompt, the practitioner leaves everything else behind and attains a mindset more conducive to having a breakthrough. It is a unique approach that has received a lot of interest and attention from both practitioners and non-practitioners from all over the world - probably because kōans are seen as meaningless questions with no real answers that somehow lead to enlightenment. But this isn’t quite true! Whether a student’s answer is correct or not is left to the discretion of the instructor, who has answered this kōan before and is well-suited to recognize a genuine breakthrough.

Coding Kōans?

But what does this have to do with Zen coding? Lately, the kōan has become popular in a context far removed from life in a monastery. Ruby Koans, the brainchild of Jim Weirich, is essentially a collection of source code files with intentional errors (the “kōans”) that are meant to be corrected by the student. A program run in the terminal guides the student through these files in a predefined order while peppering in language about “karma” and “awareness” as progress is made. The completion of these kōans should lead to the student “attaining enlightenment” in that language.

Since Ruby Koans was created, similar programs have been written for other languages. I’d visit this blog for a list of collections, and I’ve included an image from the .NET Core Koan Collection as an example. It’s a fun and fast approach to learning a new language if you’re already decent at coding, and it is definitely my new favorite method of learning. They say you can read all the programming books you want, but you won’t learn how to code until you actually do it. With koans, the enlightenment comes from solving the problems, and it’s an active way to learn all the syntax, data structures, and design principles without any of the fluff.

But What About Jakugo?

Jakugo? It’s translated in English as a “capping phrase”, and it’s an expression of enlightenment that demonstrates one has solved a kōan without giving the actual answer. According to this excellent book on Zen capping phrases, “the jakugo assignment reveals the point or core…of the kōan as well as the eye-for-the-essential…the monk needs to recognize that core.”

One can argue that all these coding kōan collections actually consist of both kōans and capping phrases. For example, consider the above screenshot from Clojure Koans collection. Each exercise is commented with a quote that can be pondered on its own and demands a certain way of thinking about programming. The kōan is the quote, while the jakugo is the exercise.

For example, consider the third example: “You can do more than create strings, you can put them together”. Before even looking at the programming exercise, we get the feeling that the point of the exercise is to understand string concatenation. From our experience in the previous two examples, which are posed as equality assertions, we imagine that we may need to compare a string to the result of concatenating two or more substrings. And BAM! That’s exactly what we see in the example.

A good way to approach these collections then, in my opinion, is to try and guess what the structure of the example will be from the commenting text. Predicting and completing the jakugo is a way to demonstrate that we fully understand the kōan. It’s easy to get into a flow and fill in blanks on autopilot, but that’s not the way to go if you’re looking to achieve real insight.

And so we’ve come full circle.

Happy Kōan-ing!

Leave a Comment