|laziness, impatience, and hubris|
using constants in Derived classby rajachan (Initiate)
|on Nov 17, 2010 at 10:21 UTC||Need Help??|
rajachan has asked for the
wisdom of the Perl Monks concerning the following question:
Greeting Monks. This is my first post in this wonderful forum
Help me code this scenario better.I have a baseclass named "automobile", and two derived classes named "car" and "truck". Also I have an new class named "ParkingLot", which should know what the car it is operating on, to customize the lot-size and other attributes.
Now back to the problem. The current codebase that I work on is matured and I wouldn't be able to make drastic changes. It's complete written in Perl OOPS. The "ParkingLot" constructor will always be passed with the automobile object (instantiated much before this) as an argument. The class(Parking Lot) uses this argument, does a ref on object pointer to find whether the class is "car/truck" and does some very specific operations based on vehicle type. To avoid this, I tried to make interfaces more generic and try to abstract the code inside refs to more meaningful public attribute or a private function, which can be overriden by derived class (Ex: parkingLotForTrucks and parkingLotForCars). IMHO, this would make the code more maintainable.
Where I cannot modularize/carve out functions out of code due to lack of context, for example code like below are found in lot of places (mostly the RHS of assignment is a constant number). I want to know, if I could use constants and override them in derived class, instead of defining single line functions that are not reusable?
For example, the below snippet could be changed as
I have read constant in baseclass could be overridden in derived class. But somehow I feel this will make code less readable, than using if-else. But it's less scalable than the Const method. I would like to know how Monks would go about this problem and make the code elegant and maintainable?