Question

Imagine you have software component named A which knows component B
Lets say knowing each other is via reference or imports, or two of them.

Is it safe to tell the following statements:
1. A-->B this connection is loosely coupled?

  1. If it was also in addition that B knows A (B-->A) then we could safely say that components are then tightly coupled and only then?
Était-ce utile?

La solution

"Loosely coupled" is not binary. It's not true or false. It's how much. Some organize this into a hierarchy. Others throw math at it. I measure it with how miserable you're making life for anyone who'd need to change this code.

In software engineering, coupling is the degree of interdependence between software modules; a measure of how closely connected two routines or modules are; the strength of the relationships between modules.

Wikipdedia - Coupling

The loosest coupling is no coupling at all. B doesn't know A exists.

Let's say knowing each other is via reference

This is very very low coupling. It's the kind of coupling collections have. In B-->A the only thing B knows is where to find A.

B might also know that A is an A or at least some A like thing. That is, B knows A's type. Which is a little more coupled. It might know the type from an import or by being in the same package.

But that's still not as coupled as B needing to call A.foo() and only after that call A.bar() and, depending on what that returned, call A.baz(). Yeesh.

Now if A also knows things about B, well now you've entered cyclic hell.

Autres conseils

For a complete answer we have to distinguish the concepts of low/high coupling and loose/tight coupling.

For the sake of simplicity I’ll use the term class but the principles also apply to systems, components and even methods.

A class has low (no) coupling if it has few (0) dependencies on other classes. It has high coupling if it has many dependencies on other classes. Few and many are of course arbitrary terms, but we can say that a class with 2 dependencies on other classes has lower coupling than a class with 3 dependencies.

Loose and tight coupling are not about the number of dependencies, but about how dependent class are coupled. If inside class A you initialize a new instance of class B, this is tight coupling. If instead you use constructor injection via the interface of class B, this is more loosely coupled. Even more loosely coupled would be if class A emitted some event, which class B responds to.

I think the common meaning is as Fabio says. Can you compile A without B ? loose : tight

If you are using a crazy language that doesn't worry about types or compiling then its a pretty simple conversion. Will my A suffer run time errors randomly* if I run it with a C rather than a B *(more than normal) ? tight : loose

*(i mean not due to missing a space or something, a proper error)

Licencié sous: CC-BY-SA avec attribution
scroll top