Life universe TDD and everything else
He was nothing but a ronin, the one without master, the one with the wind and the water.He had just been out of the great Indian education system and realised that it provided nothing but a paper which you could as well use for cleaning tables with.He wandered from dojo to dojo but was never satisfied nor was he ever interested in arts they had offered.This all changed the day he came to the new dojo.
It was nothing fancy a small dojo of 3 masters, 4 apprentices and other students, but teaching and preaching a new model of programming and delivery, something he was not familiar with, something he was alien to.He sure knew about design and sure as hell knew about object oriented design.He is aware of the oneness of design with the implementation.
The student was proud in his weapons, but the weapon of choice was something made from the fires of Mount Fuji by the well known Grand Maester Matz.It was called Ruby.He had seen it before, he had used it too but was not well versed in it.
He used the weapon in similar manner to the weapons he was accustomed to.He fought the master in the ways of Procedural Programming.The master was smart, it was not long before that student tasted blood, his own blood.He had to be a learner again.
First thing the master specified was Test Driven Development or TDD in short.TDD is a programming technique where in the programmer defines contract for the behaviour of a certain entity or Class as the practiced ones in Object Oriented Design called it.A class is nothing but a set of entities which shared same property and behaved in a similar way when encountered with a certain external impulse.An example would be a book.All books have a cover and bunch of papers inside these covers.But these covers maybe of hardcover or softcover and inside the pages maybe of sugarcane, or of unicorn hairs, but they will all respond to flipping of page with a new page different from the old one in what is written it but not in composition.
Now coming back to TDD, the contract is for defining how the behaviour worked, in this case flipping of page.You had to model the behaviour through a Behaviour Driven Development or BDD framework Rspec(more on that later).TDD follows a typical cycle better known to the world as Red Green Refactor. In the red phase you had to fail the contract that is the tool is never to be used or in this case there is no book.The next phase was of course where the contract had to be maintained a book was to be there with pages(no not the cover as we are just maintaining the contract for flipping of page ).Now the contract will be satisfied with presence of a book with pages which when flipped will return another page this is called a green stage.
The student was impatient he was much more interested in boundary cases(like the contract breach at the end of the book), a classic case of procedural programming hang over.The master served him with a freshly cut unripe papaya and asked him to eat it.The student accepted, but he could not eat it, like many unripe things it was unbearable.The master explained just like the natural process is to look at papaya and realise it is ripe when the color changes from green to orange, it is natural for the code to be derived from simpler contracts to more complex.Like at first a papaya is nothing but a flower the contracts are different at this stage, then it becomes an embryo, contracts here are build on top of those at embryo stage, the next stage is an unripe papaya, contracts here might be presence of seeds, a well developed outer skin etc.When a contract is specified the previous ones are also present.
You derive the design not design the design.
So accordingly the edge cases can be tested once the base cases has been done with in contract, and then the green stage and then go for contracts for boundary cases.
Let the baby take the baby steps and then walk and then fall.Fall is inevitable but you do not have to prevent it before the walk.
Student was enlightened.Master said refactoring is something that we will be doing when all of us are one with the concept of red and green.He added commit instead of refactor, so the new idiom of course was Red green commit.The katas now on wards will focus on this idiom.The master had only one thing to say as parting words.
Do not try and code the design. That's impossible. Instead...only try to realize the truth.There's no code without spec ever.