Wednesday, June 23, 2010

Clean Code That Works

TDD is something that has transformed the programmer within me. Kent Beck's popular book "Test Driven Development By Example" is a must read for any programmer who is serious in learning TDD.

In his book Kent Beck beautifully explains how TDD addresses the goal of every programmer: "Clean Code That Works".

Although it’s the goal of every programmer; there is always a dilemma.
There are two ways to achieve this:
  1. I'll write a clean code first; then make it work later.
  2. I'll write a working code first; clean it up later.
I have tried committing both the sins; both are disastrous.
Problem with first approach is; it leads to design that is unnecessarily complex & rigid.
By the way; what's use of clean code if it doesn't work!

The second approach "clean it up later" never happens.
We go on adding code; but never refactor. More we postpone it; more & more it becomes difficult to refactor.

It becomes an ever growing Technical Debt; that at some time becomes impossible to repay. It can potentially reach a state where one has no option but to close the shop.
Look how TDD helps to solve this problem: 
TDD goes in iteration : RED - GREEN - REFACTOR
  • RED bar gives us reason to write a code.
  • After GREEN bar; we have a working code; but that may be dirty.
  • During REFACTORing we clean up the code. 
So at the end of every iteration we have Clean Code that Works!
And these iterations are matter of minutes.
RED - GREEN - REFACTOR really gives a nice rhythm while writing code.
---------------------------------------------------------------------------------
There is one parallel:
There are two schools of thoughts:- (1) Capitalism and (2) Communism

Communism says equality should be first achieved in the society; then we should make further economic progress.
Capitalism says we should prosper first; then we can make things equal (or cleaner).
Everyone shares the dream of prosperous society, whose structure is fair. It's just the matter of what comes first.
  
However; it doesn't happen with Capitalism. Rich become richer & poor become poorer.
But there is problem with Communism as well. If we don't prosper what to do with equality?

Kent has solved our dilemma in programming. 
But it seems this problem is still unsolved.
This is quite disturbing. Isn't it?

Any views?