0

I am working on research that analyzes dependency injection (DI) in Java projects. The more I read, the more I get confused by DI in relation to other frameworks and even software quality metrics.

I have been recently reading about afferent couplings (Ca), efferent couplings (Ce), and instability (I) with the formula below proposed by Martin Fowler:

I = (Ce/(Ce + Ca))

I noticed that the definition of efferent couplings is that it counts the number of classes the current class depends on. Is that essentially the same as dependency injection definition-wise, or are there more nuances to what is considered a dependency that has the DI framework as opposed to simply a class that the current class depends on?

2
  • Does this answer your question? When is Efferent / Afferent coupling good or bad
    – gnat
    Commented Jul 19, 2021 at 15:32
  • 1
    There is more nuance. DI is not a framework, it's a technique where you make a dependency explicit from an external point of view, and you also relinquish the responsibility of creating an object that satisfies that dependency (something else creates it, and passes it in from the outside). To be clear, strictly speaking, the dependency is not the instance that gets passed in, it's the (possibly abstract) type (of, say, the constructor parameter). But more generally, efferent coupling is anything external your class mentions, calls, relies upon, knows its structure, etc. Commented Jul 19, 2021 at 15:54

2 Answers 2

2

They are different things.

(efferent) Coupling refers to classes your code won't compile without.

Dependency Injection is a way of passing in references to a class.

So

class Dog
{
   List<Leg> Legs {get;set;}
}

Dog is coupled to Leg

class Dog
{
   Dog(LegBuilder lb)
   {
       this.LegBuilder = lb
   }
   List<Leg> Legs {get;set;}
}

new Dog(legBuilder);//injecting the required legbuilder class

Dog Is coupled to Leg and LegBuilder, and we are injecting a LegBuilder

1
  • Thank you! This helps a lot.
    – Narnian12
    Commented Jul 23, 2021 at 14:18
2

Efferent and Afferent coupling can be explained in simplified terms and visualized as follows:

  • Efferent couplings count the number of things your component can be Effected by.
  • Afferent couplings count the number of things your component can Affect.

enter image description here

Dependency Injection has no direct relation to this concept of counting and evaluating dependencies; rather, it is a technique for providing access to dependencies.

The formula you present evaluates a specific kind of complexity based on the number of dependencies related to your component. DI on the other hand, is just one of several possible "methods of delivery" that can provide access to those dependencies.

1
  • Appreciate the diagram!
    – Narnian12
    Commented Jul 23, 2021 at 14:18

Your Answer

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

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