Mar 26, 2005

Automated Tests are Great

I was asked to write this utility to convert a graph of objects of type 'X' into type 'Y'. The difference between X and Y is that X objects sit in a layer whereas Y travel all over the network.

I was already handed out some partly written code. I was happy because I am training myself to be an agile practitioner and reading, working on someone else's code should be easy for me. The hitch here was the part functionality wasnt complete and it wasnt backed up by any tests.

I first wrote the part functionality. Then I wrote a junit test to verify it. THen i added few more test methods which made this part functioality a solid one. Next I moved to write the other test cases/methods and completing the whole utility. The utility used Reflection API. THe key to usage of this utility was people would have to genreate X and Y objects and have not irritating get/set methods to do the conversion.

The user of this utility reported me one bug. Immediately I recreated that in junit and went on to fix it. And it was real quick.

Now If you ask me how otherwise without junit I would have handled this surprisingly I dont know. Through some irrelevant uncomfortable system outs and lots of doubt, I believe.

Hats off to Agile.

Mar 5, 2005

Changing Mindset

In an earlier post I had pointed out use of TDD and Refactoring to write the domain logic for poll measurements.

2 Key activities still were to be done:
  • Persist this data onto database.
  • Create views to play with this data.

You may have guessed I believe in working demos. Not html prototypes. I want to see in code that I can do it atleast in 1 way what I or you claim to be possible.

So I started off with the view. The user would enter the montly data and check up with the measurements (quarterly etc). When satisfied he would want to persist the data. But I like to take baby steps. Hence I decide let me get the calculation on a view out of the way.

I now moved to bea workshop to do this. I have liked its struts framework. I designed a jsp. THe jsp would invoke a calculateMeasurement action on this web app controller. The action would load the domain objects from the form and invoke appropriate domain methods and inform the view. The view would update it with the measures. The thing I like about struts framework provided by bea is the ease of use in dropping actions and invoking views without making my hands dirty.

This to my surprise worked fine. Because my domain logic was pretty much in place. Now I moved to handle persistence. I saw my db structure and domain structure have pretty much 1-1 relationship. SO active record could be a choice. Somehow I feel active record makes my objects bloated. I tend to shy away from it. I just used a db control a db facade. I wrote the insert logic on this control. Next I created a controller action to handle inserts. It would just pass on to the db control required data. Also the choice of db was pointbase. :-). Spend only 5$ when you need to spend 5$. A 100$ would be stupid aint it? oracle = 100$ pointbase = 5$.

Similary I crated update action. For handling the object insert/update state, I didnt use a state pattern. I would use that if I had db objects. So for this very simple case an insert update flag would do. But as db - domain impedence mismatch increases I would have to refactor to a domain mapper somethign like hibernate which would take care of this.

As of now its pretty much stable demo. It lets you do what you want to. It persists to a database. Thus it does what it claims to.

I pretty much attribute all this to my agile learnings. Agile lets me ask right simple question. Or as these questions a result of my changed mindset? I have to yet to find an answer. ;-)

It must be noted that key is to:

  1. Take baby steps.
  2. Think on lines of TDD at each and every possible step.
  3. even if not agile use TDD, Refactoring.

When this project becomes multi member project: we must:

  1. Introduce automated builds.
  2. Introudce automated functional tests.
  3. If performance is a major factor right from start, have performance tests in place.
  4. Run functional and performance tests every night.

Stay Agile. Stay in Control. Claps.