Hacker News new | ask | show | jobs
by mayank 1524 days ago
Yes, but modern DI (like Dagger for Java) can detect cyclic dependencies at compile time and break the build.
3 comments

Curiously i've actually seen people use @Lazy in Spring projects to allow for circular dependencies, to deal with situations where the services or their dependencies aren't structured like a neat tree with leafs, but rather some interdependent graph with cycles.

Honestly, in practice it worked and wasn't too bad to work with, which was interesting to behold - in practice everyone talks about how circular dependencies are bad for a variety or reasons (trying to print or process data and ending up with endless loops comes to mind), but then there just was that system that was chugging along without a care in the world.

I know, but just thinking though what you're doing and building a program for right now, rather than building for a future that may never happen is probably going to deliver more success. I like constructor based dependency injection personally. It's simple and I don't really see a stack of value obsessing about the possibility of code reuse.
I can detect a cyclic reference before I even finish writing the code, what's the point of letting the compiler figure it out?
> I can detect a cyclic reference before I even finish writing the code, what's the point of letting the compiler figure it out?

The compiler generally has better attention to detail and the ability to deal with larger object graphs than the typical human.

Sure, but how are you going to write code that uses a class with a circular dependency?