Wednesday, January 19, 2005

Object BootCamp - day7

As usual, we started the day with a discussion about the previous session. I said that the conversion of units was now taking place in both Quantity and Unit classs. The code was getting ugly. Itneeded to be in one place and that should be the Unit Class. Fred readily agreed to that view. Also, if we need to have a method for finding average of Temperatures, where would we put that? There were ways of achieving it. One was subclassing a Collections class writing our own Collection and the other alternative was to have a static method in Quantity class that finds the average of a collection of Quantitites. Fred's opinion was that the second alternative was better because subclassing a Collection class was a bad idea. More on that a bit later. There was a discussion on naming standards, and how important it was in an agile team. Naming a collection of plants as Forest is a bad idea, instead naming it Plants yells out the intention better.

Fred then started talking about Inheritance. Inheritance is bad, but still important(around 10% on the PIE chart). So when do we go for inheritance? Answer is, there is a two question test:

1. Almost all of the code is reused.
2. The two objects exhibit "kind of" relationship. Also called the Grandmother test. Which means that it should be simple enough to say that they are of same kind. It should not require a programmer to say that such a relation exists.

Two level inheritances are absolutely bad. It means something is wrong with the design. Inheritances are normally not designed for. They are normally discovered.

Fred then asked us to design a Square class. I went ahead and implemented a square, which inherited from Rectangle class. I also overrided the equals method in Rectangle class so that a Square ofside 10 equals a Rectangle with length and breadth equalling 10. All the tests ran and I was happy. Fred saw the Square class and said that it does nothing, so there is no need for such a class. We were as usual surprised at that remark, but it made a lot of sense.

That was the day 7 session.


At 4:49 PM, Blogger sundar said...

what was the rationale behind subclassing the Collections class. I thought using the static method was a obvious choice. What am i missing here?

if square is not doing anything, then what is the rectangle class doing? why is that existing?


Post a Comment

<< Home