tags:

views:

88

answers:

5

I have recently read that polymorphism via classes and methods is a bad way to go about polymorphism. Why is this a problem and what are the alternatives?

I know this is a somewhat subjective question, but it is a legitimate one that spawns from the functional programming perspective such as: http://blog.thinkrelevance.com/2009/7/8/triadic-programming

If you are a programmer who lives by polymorphism via classes, I am not attacking you. I am simply trying to understand the other side. Feel free to explain why polymorphism via classes is better than some alternatives if you feel that way.

+1  A: 

It depends.

The current trend is to favor composition over inheritance. Parallel programming has also given a boost to interest in functional paradigms.

But inheritance, polymorphism, and object-orientation are not intrinsically "bad," nor are they going away anytime soon.

Learn as many techniques as you can. Learn when to apply them and what the trade-offs are (because there are always trade-offs). Use them as appropriate.

TrueWill
A: 

the link you provided does'nt say "polymorphism via classes and methods are bad" it just says "You can fight the Dyads by avoiding so-called "best practices" I dont know who he is talking about

SysAdmin
he says to avoid the practice of polymorphism via classes and methods. Thus, it is assumed to be bad.
John Baker
+1  A: 

I personally do not feel that polymorphism via classes is "bad" - however, there are other options...

I feel the real issue is that it's bad to feel that polymorphism is the only way to write software. There are many lessons we can learn from using functional programming techniques, for example, which can work around many of the limitations in traditional OOP practices, such as easing thread safety in heavily concurrent environments (which seems to be one of the linked points).

Many times, there will be a more elegant solution to a problem than polymorphism - and often, mixing polymorphism with a functional concept can produce a very elegant solution.

Reed Copsey
A: 

Polymorphism (or inheritance) can lead to problems if your hierarchy becomes too big.

For example, it might be logical to have the following classes:

class Animal;
class Mammal : public Animal;
class Dog : public Mammal;
class Cat : public Mammal;
class Fish : public Animal;

But if at a certain point you need to make a distinction between animals that can swim (dog, fish) and animals that can't swim (imagine for a moment that a cat can't swim). Then it would be more logical to have it like this:

class Animal;
class SwimmingAnimal: public Animal;
class Dog : public SwimmingAnimal;
class Fish: public SwimmingAnimal;
class NonSwimmingAnimal: public Animal;
class Cat : public NonSwimmingAnimal;

You could try to implement both hierarchies using virtual inheritance but this quickly leads to lots of problems (one of it is also called the "diamond problem").

Using interfaces can solve many of these problems, but at a cost.

class Animal;
class Dog : public Animal, public IMammal, public SwimmingAnimal;
class Cat : public Animal, public IMammal;
class Fish: public Animal, public SwimmingAnimal;

Since the classes only inherit from interfaces, they cannot easily share the same functionality. Therefore sharing functionality must be implemented via containment. If Dog and Fish want to offer both the 'swim' functionality, they have to provide the swim method, and implement it by forwarding its call to a separate Swim class, something like this (this is all pseudo-code):

class Dog : public Animal, public IMammal, public SwimmingAnimal
{
public:
   void swim(double speed) {m_swim->swim(speed);}
private:
   Swim m_swim;
}
Patrick
A: 

There is really no silver bullet in programming. To say X is better than Y for all situations is just plain wrong. I tend to follow the following guidelines when writing software.

  1. Use inheritance when I have a well defined object model.
  2. Use composition when sharing functionality across unrelated objects.
  3. Fill in the gaps with functional techniques to promote thread safety.
ChaosPandion