in reply to (tye)Re: Hacking with objects
in thread Hacking with objects
If the test would have appeared in 20 places, I would find it very reasonable to use inheritance to abstract out the check. And if the if would have been a case statement, well eliminating that is exactly what inheritance is for!
OTOH if the test would have appeared but once, that is a different issue...
|
---|
Replies are listed 'Best First'. | |
---|---|
(tye)Re3: Hacking with objects
by tye (Sage) on Mar 26, 2001 at 21:18 UTC | |
What is wrong with using inheritance to replace an if? I see that you didn't even answer your own question! Even if I had 20 ifs and they were all the same test and they all represented a clean bifurcation of a design, I'd still scream at you if your first thought of how to remove the ifs was inheritance. If you use only inheritance then you replace: with plus other code to implement the object... Now multiply that by 20. Gee, great solution. Now instead of 20 repeated ifs we have 40 repeated blocks of code. Oh, I'm sorry, you meant to also abstract out the code in the if blocks as separate functions. Okay, so now we have 40 new functions. What are you going to name them all? Does each of the 40 have a single, clear purpose that it serves well? Or are you assuming that each of the ifs also surround identical code? Well, then your problem isn't that you 20 ifs; your problem is that you have 20 duplicated chunks of code. So abstract that out as a function and now we have 1 if and using the sledgehammer of inheritance to squish it is still not the best first choice. Even if we decide that we have a case where using two functions in place of one if makes sense, then I'd first consider just storing a reference to the function in the object. If we have a bunch of cases like this and creating another class is warranted, I'd still prefer to have the original object contain a reference to these new objects rather than resorting to inheritance. From Advanced Perl Programming: Perl supports only [implementation] inheritance. [....]and When C++ came along, I quickly became enthusiastic about a language that supported inheritance, and attempted to implement the widget set in C++. Then when John Ousterhout's Tk came along, I marveled at the ease of creating widgets, even though it was in C and provided all the features that Motif provides (and much more). The Tk architecture used composition, not inheritance. If Perl supported other types of inheritance, then I'd be less critical of the use of inheritance in Perl. But even Perl's version of implementation inheritance has extra pitfalls beyond those in most languages. But I'll save the details for a meditation since few will benefit from an analysis deep in an old thread. So, yes, using inheritance in Perl to remove an if is still likely to tempt me to hunt you down and kill you. (: - tye (but my friends call me "Tye") | [reply] [d/l] [select] |
by tilly (Archbishop) on Mar 27, 2001 at 02:58 UTC | |
The classic situation where I would hide an if behind a method or function call is for portability. What I would do is have a module that promises a given API. Then when I load or construct objects I would - once - test for which implementation I should use and load that one. From then on I would call my API and the check for what way to do things is hidden. This can be done with or without inheritance. Usually it works better without. Consider DBI for a practical example. But if my API was one which came down to a situation where the offered API was relatively rich but the number of truly essential methods was small, a clean way to do this is to inherit from an abstract base class and then implement the necessary methods. Conceptually I like to compare this to tie. Now there are implicit "if" tests everywhere, all hidden within inheritance. An example of this is LWP::Protocol. Even with all of the usual caveats about inheritance in Perl, this example strikes me as well-designed. Yes, Perl could have a better inheritance model. And yes, inheritance is overused and misunderstood. But all that notwithstanding, there are cases where I think it is still appropriate. And keeping there from being a million if/elsif/else cases scattered through the code but instead hiding it in an interface can be one of them. | [reply] |