Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 394
Geek Avatar asked Mar 07 '13 13:03

Geek


People also ask

What is efferent module in software engineering?

Efferent coupling is a coupling metric in software development. It measures the number of data types a class knows about. This includes inheritance, interface implementation, parameter types, variable types, and exceptions.

How do you calculate the instability value of a class?

We define instability metric as the ratio between efferent coupling and total coupling: I = Ce / (Ce + Ca).

What is the metric for coupling?

What are coupling metrics? Software coupling metrics help development teams determine the complexity of their architecture based on the dependencies between classes, modules and methods.

How do we quantify coupling?

There are 2 ways of measuring for a proper bowl size fit. One way is using a string or a OD tape to measure the hose. Another way and most accurate is using a digital caliper to measure the inside of the bowl on the coupling.


3 Answers

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.

like image 59
millimoose Avatar answered Oct 16 '22 10:10

millimoose


Since you mentioned Sonar, here is the definition provided by their documentation page

  • Afferent couplings : A class afferent couplings is a measure of how many other classes use the specific class.
  • Efferent couplings : A class efferent couplings is a measure of how many different classes are used by the specific class.
like image 40
ppapapetrou Avatar answered Oct 16 '22 09:10

ppapapetrou


Coupling is a measure of dependencies.

Afferent Coupling :

  • Who depends on you.
  • Measure of how many other packages use a specific package.
  • Incoming dependencies.

Efferent Coupling :

  • Who do you depend on.
  • Measure of how many different packages are used by a specific package.
  • Outgoing dependencies.
like image 10
aniruddha Avatar answered Oct 16 '22 10:10

aniruddha