scitools.com

← Metric catalog

Coupled Classes is one of the source-code metrics Understand computes across 17+ languages — browse them in the GUI, export and track them, or script them with the Python API.

Coupled Classes

API ID: CountClassCoupled
Also Known As: Coupling Between Objects (CBO)
Languages: Basic, C#, C++, Java, Pascal, Python
Targets: Classes

Number of other classes coupled to.

The Coupling Between Object Classes (CBO) measure for a class is a count of the number of other classes to which it is coupled. Base classes and nested classes are not counted. Class A is coupled to class B if class A uses a type, data, or member from class B. This metric is also referred to as Efferent Coupling (Ce). Any number of couplings to a given class counts as 1 towards the metric total.

For example:


class Amphibian {
public:
  void eatenbybird() { ... }
};

class Toad: public Amphibian {

};

class Water {
public:
  static int temp() { return 60; }
};

class Fly {
public:
  void getEaten() {}
};

class Frog: public Amphibian {
public:
  bool swimming() {
    if (Water::temp() > 50)
      return 1;
    return 0;
  }
  void eatFly() {
    Fly edible;
    edible.getEaten();
  }
private:
  class HopCalculator {
  public:
    int calculateHops() { return 1;}
  };

  Toad mCousin;

  void hop() {
    HopCalculator().calculateHops();
    Amphibian::eatenbybird();
  }
};

Frog is coupled to three other classes: Water (via the call to Water::temp()), Fly (via the local Fly edible; and the call to edible.getEaten()), and Toad (via the member Toad mCousin;). That gives CountClassCoupled = 3. Frog's own base class Amphibian is also used, through the call to Amphibian::eatenbybird(), but base classes are not counted, per the rule above. The nested class HopCalculator, declared and used inside Frog itself, is likewise excluded, per the "nested classes are not counted" rule.

See Coupled Packages for the package-level counterpart, and Coupled Classes Modified to exclude coupling to standard-library classes. If you're calculating this for every class in a large C++ project (for example, for a metrics export), consider Coupled Classes (Batch) instead, which caches every class's value on first use rather than recalculating per class. See the Coupled Classes Metric interactive report to see exactly which classes a given class is coupled to.

Chidamber & Kemerer suggest that:
1) Excessive coupling between object classes is detrimental to modular design and prevents reuse.
2) Inter-object class couples should be kept to a minimum.
3) The higher the inter-object class coupling, the more rigorous testing needs to be.

Also known as Chidamber & Kemerer - Coupling Between Objects (CBO).


Targets By Language: Configuration: