Introduction
A little background on the subject, Sashimi is a concept of Japanese Food making where every possible piece of food is perfected to its excellence, assembled and presented. Each and every piece in its own is considered to be concise and precise. Can we apply this concept to writing code, yes why not? As I myself have experienced its benefits so it’s a good idea to give you a taste of it. TDD is an approach where the test is written first, and then the implementation is written to satisfy the test. Why these two concepts are introduced? Because they both fit each other like glove on a hand.
Before you proceed I want to specify that this tutorial is not intended for programming novices, it is intended for Tdd or Sashimi novices. If you have trouble understanding the free form syntax that I use I suggest you consider develop basic programming expertise first. I have used ‘pseudo’ keyword that means you have to translate it to whatever language you are using, but as a programmer I am certain that you would understand what I want to express.
psuedo programmercheck() { if (!gotmypoint()) exit 0; else continue reading;}
if you cannot interpret what it means you need some programming germs, this tutorial is not for you. As I expect you to read a code line and understand it instantly. That is if you want to make a dish and yet u don’t have understand ingredients, Its not Japanese style !!
reading:
So you passed the test by following psuedo instructions, you are welcome to proceed.
Analyzing the Framework (Prepare for Battle)
Let’s say we want to make an application to display 2D graph for charting purpose. We will keep it simple enough for minimal scope. The key to the beginning is ‘Drill Down’ for more information.
First we identify major components, starting from Graph Itself (class Graph). I assume you understand what a 2D Graph is. Ok do we know what a graph is composed of ? Yes Lets say we have identified its components Axis, Plot, Lines, Points etc.
Do you have the complete understanding of the concept in the context of the problem? If so then proceed
Graph has two Axis , Ok Right !
Ask yourself, What is an Axis?
An Axis is displayed for reference, it has an orientation (horizontal or vertical) Ok, It is represented normally by a Line.
Ask yourself, What is a Line?
A line is defined by two points, Lets say p1 and p2, it has some length calculated by some formula.
Ask yourself, What is a Length Formula? Lets say u don’t know but have a gut feeling that you may find it. Leave the ‘How’ now concentrate on ‘what’.
Ask yourself, What is a Point?
A point is represented by two values x and y in 2D representation.
Now reconsidering above dialog, We have identified these classes.
Graph, Axis, Line, Point.
Implementation
So where do we Start Now !!!! Pick up the easiest and smallest concept, i.e Point
psuedo partial class Point { int x,y;};
What Else is required? Does it fulfill the requirements of a point, Yes I think so because point has no perceivable behavior associated in the context of our application, So this definition seems enough.
So let’s move on to next class Line that uses Point
psuedo partial class Line { Point p1,p2;};
Ok consider now the attributes of class are defined, does it have an associated behavior? Yes, the Line would be required to return its length, for now define a function that returns 0;
psuedo partial class Line { Point p1,p2; float length();}; float Line::length() {return 0;}
Now here is the point where you will be tempted to write the function length as f(x,y), But I must warn you to stop here, A basic concept of Test Driven Development is ‘Write Test First !!", makes sense? To me it makes a lot of sense, Refer to TDD approach.
Now how can I test a code when it is not yet written, Oh yes, You can write a test code but the test is bound to fail. The Test is bound to fail, Not You!! So Behold.
The length is calculated, how? I deliberately left it, because at this point of time you should focus on ‘what’ and not ’how’. Lets define a simple test, Lets say p1 is at (10,5) and p2 is at (20,5) length should be 10. This can be one simple test, if you are new to TDD you might think what this meager little test might benefit. But as you keep my track, I will demonstrate you how much it can benefit you, that’s why I am writing this tutorial.
pseudo test1() { p1.x = 10; p1.y = 5; p2.x=20; p2.y=5; if (length()==10) return true else return false;}
We will write another function to clear the test data
pseudo clear() { p1.x=0; p1.y=0; p2.x=0; p2.y=0;}
Now it would be prudent to define a TestManager class to encapsulate our test methods. You may also consider using a Unit testing Framework (e.g. CUnit, CPPUnit ot JUnit) to manage your tests. That might look something like this:-
pseudo partial class TestManager { void RunUnitTests() { line->test1();line->clear(); }}
If we can compile the code at this moment and compile and run the application,the test will run and fail !!
Because length is not implemented, Test is defined so we can check our code prompting a failure. Spooky isn’t it!!! But we have to deal with it for this failure indicates success.
Now is our Line Class Ready to be used, No not yet because it is not implemented!!
So we don’t proceed ahead until we make it usable. This is the point we implement the length function. You may do it now, your test will tell if you are done with it.
Do you use Tdd in your team? leave your mark
This Site has moved
If you are not redirected automatically within 14 seconds then please click here
Recent Entries
Thursday, July 10, 2008
Subscribe to:
Post Comments (Atom)
Search Tutorials
BlenderNation
Home and Learn - Free Beginners computer courses: VB .NET, Microsoft Word, Excel, Web Design, PHP, Javascript. Aimed at those who have little or no experience. Courses takes you to an intermediate level. Free exams are also available.
No comments:
Post a Comment