2

Two of the OOP metrics, Coupling Between Object classes (CBO) and Polymorphism Factor (PF) both measure coupling.

Is there an instance where of them has a higher coupling while another has a lower coupling? Is interface inheritance one such situation?

Also, how does each of them measure coupling?

1

1 Answer 1

3

The coupling between object classes (CBO) is a metric proposed by Chidamber & Kemerer to measure for a class to how much other classes it is coupled. A coupling means that an object of one class use methods or properties of an object in another class.

The polymorphic factor (PF) is a metric proposed by Abreu & Melo to measure how much a derived type overrides a method from the base class. It's calculated by counting the number of overridden methods compared to the number of methods.

These are independent measures, so in theory you can find any kind of relationship. But let's have a look at an an example to show how they relate in practice.

Suppose you have classes for representing different shapes (Circle, Square, etc...) and a class Figure, composed of several shapes. Figure has a draw() function that invokes a drawing method for each of its shapes.

  • Uggly implementation: each shape as an own independent class with own specific drawing function (e.g. draw_circle(), draw_square(), ...). Figure must then be coupled to all the shapes, with a lot of conditionals to cope with the different shapes. Every time you add a new shape, you add a new coupling.
    • CBO = N, where N is the number of shapes.
    • PF = 0 because there's no inheritance and hence no overriding.
  • Polymorphic implementation: each shape inherits from a superclass Shape and overrides the polymorphic draw() function. Figure only knows about Shape.
    • CBO = 1, because Figure only invokes the draw() on a Shape object .
    • PF tends to 100% (precisely it's N/(2+N) because Figure::draw() and Shape::draw() are no overrides, but Shape::draw() is overridden by N shapes

The common assumption is that a system with low coupling is easier to maintain. Therefore, many design patterns intend to reduce coupling. In most of the cases, polymorphism is the enabler for a decoupling.

Abstraction is the key, not the language feature used to implement it. Interface inheritance and normal inheritance are two ways to achieve abstraction. Interface inheritance requires you to provide an implementation for the interface functions (i.e. to provide an override for abstract functions which are not defined). It hence increases mathematically PF. However, some languages like C++ don't have interfaces but use pure virtual functions eventually with multiple inheritance to do the same, with identical PF. In our example, the inheritance is the enable for reducing coupling.

3
  • Perfect answer!! +Christophe I have one small doubt. could you further explain this point- " polymorphism is the enabler for a decoupling." I thought inheritance increased coupling since it increased the number of derived classes and thus the number of inherited methods?
    – Char
    Jun 18, 2017 at 16:05
  • 1
    @Char Thanks. First for CBO, I used the original definition of use by object of one class of operations on an object of another class. This is not something that would automatically increase with inheritance. Because a circle object would use its own functions would still use its own functions and not those of another object. Now, even if you count inheritance as an implicit link, it would be a unidirectional coupling of the child to the parent. So for the whole system, it would remain small if compared to to the total potential links (combinatorial explosion).
    – Christophe
    Jun 18, 2017 at 16:28
  • 1
    In addition, polymorphism combined with inheritance reduces the coupling by replacing a bunch of relations with many child classes, with one relation with a more abstract class.
    – Christophe
    Jun 18, 2017 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.