4

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.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
  • What do you mean by import? Are you talking about `import` statements in Java? – adarshr Jan 12 '12 at 10:29
  • Eclipse won't even allow you to have two projects depending one on another. So situation you're describing is really absurd. – dhblah Jan 12 '12 at 10:29
  • @gasan: Eclipse is not the only way to develop in Java, and you might generate two different jars from a single project. – JB Nizet Jan 12 '12 at 10:31
  • I mean `libA` depends on `libB.jar` and import classes from `libB`, while `libB` is doing the exact same! – UmNyobe Jan 12 '12 at 10:34
  • @gasan: Eclipse allows it. You can put another project on the required build path. – UmNyobe Jan 12 '12 at 10:46

4 Answers4

7

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.

codeforester
  • 39,467
  • 16
  • 112
  • 140
duffymo
  • 305,152
  • 44
  • 369
  • 561
1

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

Both the libraries were written at a the same time, possibly by different developers. Or the same developer at different times, or by a developer who treated both libraries as one big code base, and wasn't concerned about avoiding circular dependencies. e.g. They had a hard enough time writing something which worked without worrying about niceties.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Most likely it will be inexperience.
Modern build tools like Maven preclude circular dependencies between artifacts.

johnstok
  • 96,212
  • 12
  • 54
  • 76
  • It'll point them out, but how can it preclude them? What would Maven do in this case? – duffymo Jan 12 '12 at 10:33
  • It fails the build and asks you to resolve the circular dependency. – johnstok Jan 12 '12 at 10:39
  • You mean it detects in your code. It won't fix libA and libB or ask you to remove them. And what does it say about the JDK itself? Nothing. – duffymo Jan 12 '12 at 10:42
  • For me library==JAR. Maven manages dependencies between JARs (or more accurately 'artifacts'). Perhaps you think library==package? – johnstok Jan 12 '12 at 10:52
  • No, I know what a library is. You're saying that Maven would fail this guy's build because libA and libB have a cycle? Another reason to not use Maven. – duffymo Jan 12 '12 at 10:53
  • Don't you think circular dependencies between libraries are a bad idea? – johnstok Jan 12 '12 at 16:23
  • Are you saying that you refuse to ever use libraries with dependencies? What do you do when you find them - write your own? You don't use Hibernate - it's riddled with cycles. In an ideal world I'd like everyone to design perfect software wo/ cycles. Until that day comes I'm going to use what I must and do the best I can with my designs. – duffymo Jan 12 '12 at 20:34