Zen of programming
March 18th, 2006Key belief of Buddhism is that human life is suffering (苦海). Suffering from birth, aging, sickness and death (生老病死). One important theory of Buddhism is that how the world looks like is interpreted by how you think it is (象由心生). Cause of suffering is really because of greediness, emotions and addiction (贪,嗔,痴). To avoid all these, you need to become a Buddhist, meaning taking a training, so that you learn deeper and deeper about your life, and you rank higher and higher in this training course. Very much like a school system that you are in primary school, gradually up to middle school, high, college, graduate, postdoc, etc. In Buddhism, depending on how well you get trained, you get different titles as well, and all these titles are named after Buddhism Gods, thus encouraging people to consider themselves becoming different levels of Buddhism Gods and also allow people to compare with each other on what level they are at.
So what is “Zen”? Zen is simply one way (法门) of getting trained among many many different ways. Zen is an accelerated program, just like a talented student program that gives people who are born to be smart (慧根) to take a quicker route to become highest ranked Buddhism God. Key theory of Zen is to “think out of box”(顿悟). When I use my finger to point to moon to tell you, “hey, look at that moon”, you don’t look at my finger and you should look at the moon. When you read a book, you don’t care how letters are printed, and you want to grasp what’s the meaning behind those characters. Zen is all about thinking on a higher level. When a flag is waving, everyone sees the flag is waving, but a Zen student should identify that it’s the wind that waves the flag. An even better trained Zen student should realize that it’s not the flag, neither the wind, it’s really your mind/sensor that makes you perceive a waving flag. It is this deeper and deeper understanding about why and what we are here and how we feel about different things that makes you finally understand why human life suffers, thus eventually saving your life from being miserable.
Taking a break here, you may have learned different interpretation of Buddhism and Zen, and if yours is different from what’s here, yours could be wrong or derivatives :-) There are so many different intepretations out there, and it really polluted Buddhism theories and Zen theories, and both are actually very concise and elegant, yet people derived so much unrelated stuff from them. Maybe I should start a new blog on Chinese philosophy and Buddhism theories. Evey single bit of it is a shining piece of wisdom.
Anyways, why is it related to programming at all? Well, practicing Zen of programming is just my way of saying generalizing your program to make it run on a higher level. A higher level means that it can take a larger set of inputs, or in other words, it can handle more situations so that it becomes a more general program.
But why? Isn’t extreme programming telling us the exact opposed? “Don’t do any extra work to over-engineer anything”. No, no, it is dangerous for a young and junior programmer to take advices like that, and it’s only okay to veteran and experienced fellows to pull themselves back from having a habit of over-engineering. When I first start programming, all my focus are on how I am able to write a program to finish the job. There is absolutely no over-engineering at all. I didn’t really know how to make my programs general to handle more problems. Oh well, not that “I didn’t know”, just I didn’t have a tendency to make that happen at all. Only when you develop yourself on a higher level, so that finishing a programming task is no loner a problem, you then start to pay more attention to interactions of different programs in a system, to how quality of software affects maintainence, to how easy a program breaks on new requirements, to how data model is important in minimizing code changes, etc..
So, when you are young, don’t read anything about over-engineering, because you won’t, and you are just not capable of doing that. When you are older, maybe.
More importantly, making your program more general is a learning process. This is a process that helps you identify how programs and programs are related to each other, helps you understand data transformation, helps you understand code transformation, and helps you understand how software should be maintained over years. Yes, “years”, and we are not talking about some graduate school projects that only has a life cycle of a couple of months. When we write a piece of code that you know for sure it’s only for short period of time, just get the job done, and forget about it. When you are writing a piece of code that’s meant to last, you better be serious about over-engineering it. The longer it will last, the more thoughts you should put to think ahead.
Many argued with me that it’s not fast doing that way. An otherwise 3-day written code becomes longer to finish. Yes, this is exactly why you need to keep practicing on this, so that 3-day will become 4-day, instead of 4-month. This is exactly why you need to take any daily programming task as a test to develop your habit to become a good “think-ahead”-er. It is all about experience. When you become acquainted, planning never takes unreasonable amount of time. When you are on a higher level, it never becomes a problem that “whether I should over-engineer or not”, because there is no time difference at all!
Many also argued with me that a more general program is cryptic to understand. This can happen, but normally it’s not because the complexity the program becomes, it’s really because code is not written cleanly enough, and this is why I’m writing all these blogs
. There is of course a tendency of that, and esp. with higher order constructs like templates and generics, but there are a lot more time a program all the sudden becomes so clear, just because it’s written on a higher level.
In real life, a nicely shared components, meaning a general purpose library, will saves tons of developement time and QA time. How much savings there is considered into the “over-” “under-” engineering equation? It is only badly written over-engineer-ed components that might be taken as bad examples of “planning too much”. The Zen training of programming will exactly help you avoid that.
How though? How can I improve my code to become a more general one? I may use my next blog to show you one real example that you may then tell me it’s not that hard at all.