What is the difference between afferent couplings and efferent couplings of a class?

Geek picture Geek · Mar 7, 2013 · Viewed 24.1k times · Source

Code quality metric tool like Sonar does provide the ability to drill down to a class and find out the number of:

  1. Afferent (incoming) couplings
  2. Efferent (outgoing) couplings

What are these two parameters? Can you please describe with a simple contrived example?

Answer

millimoose picture millimoose · Mar 7, 2013

According to wikipedia:

Afferent Couplings (Ca): The number of classes in other packages that depend upon classes within the package is an indicator of the package's responsibility. Afferent = incoming.

Efferent Couplings (Ce): The number of classes in other packages that the classes in the package depend upon is an indicator of the package's dependence on externalities. Efferent = outgoing.

So, if you have classes (or packages or whatever) with the following structure:

class Foo {
    Quux q;
}

class Bar {
    Quux q;
}

class Quux {
    // ...
}

Then Foo and Bar each have one efferent coupling, and Quux has two afferent couplings.