Search code examples
oopdesign-principles

Characteristics of bad object oriented design


I am reading about object oriented design principles. I came across the characteristics of a bad design.

  1. It is hard to change because every change affects too many other parts of the system. (Rigidity)
  2. When you make a change, unexpected parts of the system break. (Fragility)
  3. It is hard to reuse in another application because it cannot be disentangled from the current application. (Immobility)

I am able to understand the first two but the third one is little difficult for me to understand. Is it about extracting common features of related classes in a Base class, making methods from the code which is repetitive ? But it says hard to reuse in another Application. Usually we write context specific code and Over-engineering is not a good idea, we have good principles like YAGNI (You ain't gonna need it) I find these ideas little contradicting.

Please provide your valuable thoughts for this.


Solution

  • Mobility example:

    Assume the following classes:

    1. Animal
    2. Canine
    3. Dog

    As you would expect, Canine extends Animal and Dog extends Canine.

    One way to poorly design Animal is to give it a method talk() which prints out bark. Perhaps, the original intent of this application was for dogs only and therefore the talk method barking was fine. But reusing this in another code base would lead to issues.

    Say we want to extend Animal and create Bird. Birds don't bark :)

    It's hard to image that someone would do this. But it happens all of the time. Base classes aren't abstracted out which leads to misplaced code making it difficult to correct/reuse.

    One could argue that the Bird class could override the talk method. That would work, however, another developer extending Animal for yet another reason may forget to override that method... etc.

    I know this isn't the best example, but it demonstrates the problem.