What Sufi Mysticism Taught me About Functional Programming

Asad Rehman
3 min readOct 20, 2013

--

Sufi mystics believe that our greatest enemy is ourselves. They divide the spiritual self into components, and name the part of us that is the root cause of grief and sorrow — the part that must be overcome and defeated — the nafs, while the qalb is the spiritual heart that must be purified so that it may guide us towards truth and righteousness.

Their purpose at first seems to be to follow their practices in order to enter a spiritual unity with the infinite (God), an act that is supremely fulfilling and inherently rewarding that leads to existing in a state of enlightenment. But that’s not the only purpose of the Sufi way. This ascension is considered only the half-way mark of their journey, and Sufi teachers train their students to pull themselves away from the bliss of Unity and return to the world to realise their true purpose.

I learned programming when I was twelve and had been programming in imperative programming languages for around a decade before I understood the true nature of functional programming. I had always imagined FP was for pure, sterile academic environments where everything could be theoretically awesome but practically useless amongst the blood, sweat and tears of software engineering in the real world. Primarily I felt I lost control over how correct I would want a particular solution to be; sometimes I needed to hack a quick fix, and sometimes I had enough time to properly architect, design and code out a feature. But FP has its place and I discovered that, ironically, its greatest advantage is a side-effect of learning and using it.

Starting out with FP I was entranced by the purity and beauty of it, and quickly learned to forsake the evil of mutable state. Reading through Structure and Interpretation of Computer Programs I was amazed by how complex problems could be solved with such elegant simplicity. I also became a Test Driven Design fanatic, and soon began to hate the portions of my code that had to deal with the dirty impure outside world. Why couldn’t all the code I write be nice and pure and testable? I began to lean towards writing server-side code which would only interact with other software through sanitised, well-defined APIs. I shunned all code that involved user facing UI elements and stuck to my FP ways.

It was at this point I sensed something was wrong. Lost in thought one day, I discovered a deep void within me in the form of an existential crises that I could not resolve. Sure, I was happy with my code, and it was quite enjoyable to write out elegant solutions with my fancy new tools, but what was the purpose of my code? What was the purpose of my life? I was lost in what Sufis might call the state of fana’.The only way I could give purpose to my existence as a human being was to help others, and the best way I knew how is through my code. What good is my code if it doesn’t interact with the world and improve someone’s life? What good is a piece of software that has no side effects?

And so, as Sufis are taught, I began my descent towards baqa’.I returned from my journey to functional programming bliss back to the world of mutable state and pesky, inexact endusers. A world where the purpose of my code was not to pass a unit-test, but to touch someone’s life in a way that profoundly changes it for the better.

Given enough time, maybe someone could derive these same lessons from the first principles of imperative programming alone, or even from the roots of computational complexity theory, but learning functional programming allowed me to fundamentally change the way I think about software. It gave me the ability to write solutions in any language far better than I could have before; it allowed me to recognise those parts of a solution that were inherently dangerous, and taught me the tools and concepts to tackle those problems in a safe and simple way.

Most importantly I feel the experience taught me to overcome the pitfall that undoubtedly exists in other professions too; our obsession with perfection causes us to forget our true purpose, until we realise that the only way to live fulfilling lives is to do what little we can to — in our own way — better the lives of others.

--

--

Asad Rehman

Zerg. Sysop. Software Engineer. Co-Founder. Stares rudely at progress bars. 2400 8 N 1.