News for hackers. Stuff that matters.
Re:That won't scale Or present cost vs. future value by SPBS on Sunday April 23, 2023 @11:44PM (#35683440)
Simple code is easy to change, I feel like a lot of architecture astronauts (cough Enterprise Java cough) don't get that.

"Why is everything an interface?"

"Because we may need to swap out its implementation in the future. It's Clean Code."

"Then refactor it out into an interface when you need it! What's an IDE for?"

Re:That won't scale Or present cost vs. future value by bluejekyll on Monday April 24, 2023 @12:03AM (#35683530)
Interfaces have a different value, they make it easier to build testing facilities for unit tests as well. This works in many languages, Java, traits in Rust, etc.
Re:That won't scale Or present cost vs. future value by rf15 on Monday April 24, 2023 @03:27AM (#35684632)
Is it really a good idea to bend your code to satisfy your testing environment and not the other way around?
Re:That won't scale Or present cost vs. future value by rtheunissen on Monday April 24, 2023 @03:37AM (#35684705)
"Bend" is not a good description of the intention here. Good code should be easy to test effectively and interfaces are a common way to achieve that. We should absolutely design our code with testing in mind.

For example, you have a repository for some entity as a dependency of a service and you would like to test some behavior of the service when the repository fails. In this case, the service can depend on an interface of the repository instead and the test can create a stub that implements that interface, whereby you can simulate the error. If the service depended on the actual repository, this becomes very difficult to do.

Re:That won't scale Or present cost vs. future value by agalunar on Monday April 24, 2023 @06:29AM (#35685771)
> Good code should be easy to test effectively

Can you explain why this is? or maybe more specifically what you mean by this.

Re:That won't scale Or present cost vs. future value by robertlagrant on Monday April 24, 2023 @05:00AM (#35685206)
Your tests are part of your code. Your implementation code should not make writing your test code difficult.
Re:That won't scale Or present cost vs. future value by pjc50 on Monday April 24, 2023 @09:35AM (#35687455)
It's kind of unavoidable once you're trying to do unit tests with mocks. You either end up with interfaces for everything and a dependency injection framework, or you end up with some very non-isolated tests that take ages to run because they need inseparable parts of the whole real system.
Re:That won't scale Or present cost vs. future value by Jach on Monday April 24, 2023 @12:03AM (#35683534)
The real answer is interfaces, even if they only have one implementation, let you not have to worry about cyclic dependencies.
Re:That won't scale Or present cost vs. future value by cjblomqvist on Monday April 24, 2023 @02:08AM (#35684197)
How? (honest question, I'm curious!)
Re:That won't scale Or present cost vs. future value by Jach on Monday April 24, 2023 @03:05AM (#35684491)
If you start with AService in module A, other modules need to depend on A to use it. You get into circular dependencies trouble when A needs to use a service from another module B that itself or through a dependency needs the AService. One solution is to split it into an AService interface you put in a new module, call it e.g. A-api, and leave the implementation in A which now depends on A-api. Do the same for B/B-api. It's a lot less likely that your -api modules will circularly depend on each other because they only contain interface definitions, so A can safely depend on both A-api and B-api and B can safely depend on both B-api and A-api.

A more concrete example of an A and B that need to interact with each other might be a user account service and a (financial) transactions service. (Why not let them live in the same module? That can also be a solution, but as the scope of each grows and especially if separate teams end up owning each it can make sense to split them and enforce some boundaries via the interfaces.) Another example could be a notifications service and several of its clients, like whatever is handling user posts kicks off a notification event and asynchronously the notification service processes it and may need to reach around to call back into other services that could be part of the sender's module. (Passing simple lambdas may be an adequate alternate solution too.)

Re:That won't scale Or present cost vs. future value by jt2190 on Monday April 24, 2023 @09:00AM (#35687125)
> One solution is to split it into an AService interface you put in a new module, call it e.g. A-api, and leave the implementation in A which now depends on A-api.

This is what Fowler describes as “Separated Interface”. [1] Specifically he calls out the specific need for this special case:

> “[Y]ou might need to invoke methods that contradict the general [module] dependency structure. If so, use Separated Interface to define an interface in one package but implement it in another.”

It’s a special case because until you need to “contradict the general dependency structure”, you don’t need to do it. In particular, this is not the first tool one should reach for if you have a circular dependency between two modules.

[1] https://www.martinfowler.com/eaaCatalog/separatedInterface.h...

Re:That won't scale Or present cost vs. future value by lenkite on Monday April 24, 2023 @04:59AM (#35685203)
I dunno - I found "Enterprise Java" objectively quite easy to refactor to fit high-scalability and modularization needs. Having stuff defined as interfaces actually helped greatly with this.
Re:That won't scale Or present cost vs. future value by Retric on Monday April 24, 2023 @05:29AM (#35685397)
Straightforward yes, quick no.
Re:That won't scale Or present cost vs. future value by preommr on Monday April 24, 2023 @05:53AM (#35685537)
Because then someone else comes along and builds to the implementation and now that simple change becomes a mess of a refactor.

An interface isn't just a type construct, it communicates the intent of the systems design.

Re:That won't scale Or present cost vs. future value by magicalhippo on Monday April 24, 2023 @09:19AM (#35687303)
Depending on language, interfaces also places additional constraints as it limits interdependencies.

For example, in Delphi classes in the same unit (file) can access each others protected members. This is usually a code smell, which interfaces prevent.

Re:That won't scale Or present cost vs. future value by marginalia_nu on Monday April 24, 2023 @06:30AM (#35685774)
It's not really about replacing the implementation.

Depending on interfaces means you (by definition) can't depend on the implementation. It's a decoupling thing more than anything else. You get some benefits for mocking during testing and so on but it's mostly just to get something like a C++-style class declaration/definition (or .h/.cpp)-split.

It's admittedly taken a bit too far in some codebases and often done without much thought to why, but it's also a bit of an artifact of pre Jigsaw-era Java where it was very hard to enforce boundaries between modules. In that light it makes at least some sense.

Re:That won't scale Or present cost vs. future value by ParetoOptimal on Monday April 24, 2023 @12:06PM (#35689400)
> Simple code is easy to change, I feel like a lot of architecture astronauts (cough Enterprise Java cough) don't get that.

It's not easy to uniformly change in a way that doesn't add regressions. It's also not usually easily changed except by a large team. That large team likely adds more regressions because communication is lossy.

"Simple code" that avoids abstractions leans more on people, code that embraces abstractions (good or bad) attempts to lean more on the machine.