Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two java libraries importing each other?

I am working on a legacy framework and apparently there are two libraries, which are inter-dependent. By that I mean libA import from libB, and libB import from libA. First i think it is a terrible design, but why would somebody do something like this? Rather which conditions can lead somebody to write this ?

edit:

Each library depends on classes in the other, so they do import packages and have the other library jar in their build path.

like image 564
UmNyobe Avatar asked Feb 22 '23 21:02

UmNyobe


2 Answers

It's easier to do in this case, because the two parties are independent. If they don't talk to each other, it's not hard to create cycles. You have to be mindful to avoid them.

Cyclic dependencies aren't hard to create. Look at Java itself: java.lang, java.util, and java.io have cycles. Will you stop writing Java, since it's so "terrible"?

It means that you can never use libA without libB and vice versa. They've become one big library. Same with packages in Java and other systems: once you have a cycle, you have to use all those packages together as if they were one.

The guys who write Spring pay a lot of attention to cycles. They design and refactor their framework to eliminate them.

So - what's the harm? Juergen Heller says they're bad, and he's right. But from your point of view, what evil is visited upon you? It means you have to use both when you run and test. You can't test class A without class B and vice versa when there's a cycle between them. It makes testing and running harder.

You can choose an alternative that doesn't have the cycle. If you can change the source, you can refactor and maintain it. But that's it.

You should check your own code to see if you've done it to yourself. IntelliJ has nice analysis tools which can be applied to a code base. Check it out.

like image 62
duffymo Avatar answered Feb 24 '23 09:02

duffymo


While developing lib A, the developer found that the class Foo from lib B was useful. And while developing lib B, the developer found that the class Bar from lib A was useful.

I'm not saying it's a wise thing to do, but your question asks why anybody would do that. This is probably the answer.

like image 23
JB Nizet Avatar answered Feb 24 '23 09:02

JB Nizet