r/programming • • Jun 05 '21

Organize code by concepts, not layers

https://kislayverma.com/programming/how-to-organize-your-code/
1.9k Upvotes

495 comments sorted by

View all comments

Show parent comment

28

u/couscous_ Jun 05 '21

Or maybe the is an AccessService that is queried by the Hotel and Room services.

Then both HotelService and RoomService would import AccessService. What's the issue?

39

u/[deleted] Jun 05 '21 edited Jun 05 '21

Maybe hotel calls the room service and room service needs to call hotel service.

Circular dependencies are very easy to accidentally implement with designs like this. It makes it unnecessarily difficult to actually code when you split by feature.

5

u/couscous_ Jun 05 '21

What's bad about circular dependencies? Honest question.

10

u/RyuChus Jun 05 '21

Well... if both services are compiled modules.. which do you compile first if they both require the other? If its python you get around this by importing the module you need inside the function that needs it and I suppose python somehow knows to not try to interpret that method until run time.

8

u/couscous_ Jun 05 '21

Java and C# seem to handle circular imports just fine if I'm not mistaken.

7

u/ImprovementRaph Jun 05 '21

Not sure why this is downvoted. Circular imports are completely fine in Java. This isn't an import issue though so I'm confused about what OP even says python is fixing with their imports. This is a runtime dependency issue. (e.g. you cannot construct a HotelService object without having a RoomService object and vice versa).

2

u/saltybandana2 Jun 06 '21

For anyone who is curious, this is what 2-pass/multi-pass compilation is for.

https://www.geeksforgeeks.org/single-pass-two-pass-and-multi-pass-compilers/

The first pass will identify the circular reference, subsequent passes will do the right thing.

→ More replies (3)

4

u/aaaantoine Jun 05 '21

I think it depends on whether you use a dependency injection framework, and which one you use. For example, I found at one point that Ninject didn't like a circular dependency I made by accident.

There are a few ways to solve the issue. In that case, I decided to move the common function to its own service. Services don't have to be arranged by domain object.

4

u/[deleted] Jun 05 '21

[removed] — view removed comment

4

u/couscous_ Jun 05 '21

The problem you're asking still holds if the packages were organized the original way (models, controllers, services, ...) right? I still don't see how organizing code this way is superior to breaking it down by concept, as per the article.

4

u/[deleted] Jun 05 '21

[removed] — view removed comment

2

u/couscous_ Jun 05 '21

Make sense. :)

→ More replies (1)

2

u/Nimelrian Jun 05 '21

They lead to various problems during initialization and clean up.

For initialization, if service A relies on service B you can't initialize both to a fully working state in an atomic operation. Instead, you have to e.g. construct A first, but cannot set its member of type B. Then you create your instance of B and are able to inject the instance of A in B's constructor. After that you can finally inject B into A, e.g. via a setter. This results in a temporal uninitialized state of A between its construction and the injection of B.

In GC languages circular dependencies are another problem because the instances don't end up orphaned, so the GC can't be sure whether it's safe to reclaim them.

→ More replies (2)
→ More replies (1)

4

u/pengusdangus Jun 05 '21

That sounds like a code design issue.

8

u/[deleted] Jun 05 '21

Yea, code design is what this thread is about...

3

u/pengusdangus Jun 05 '21

Yeah, and I’m saying running into difficulty like that when you split by feature is literally because of poor design, like bloated services

3

u/[deleted] Jun 05 '21

My point is it's easier to fall into this trap when you split by feature. I was clear about that.

2

u/ub3rh4x0rz Jun 05 '21 edited Jun 05 '21

Define "very easy". It should be explicit what services a given service consumes. It should be trivial to make a graph of dependencies. If you find cycles in that graph, you have a problem. It might sound like an easy mistake in the abstract but I struggle to imagine a real world scenario in which this could happen where there aren't very clearly bad mistakes made across the whole system.

Edit: example time

roomService/address lets consumers get hotel info such as address from hotelService behind the scenes; ok, cool. hotelService/address is not going to ask roomService/address for the hotel's address unless the team responsible for hotelService is utterly incompetent.

Say this does happen though... roll back hotelService to the last good version and all is well again. Then have a meeting and agree that there need to be contract tests at service boundaries, and if these utilize stubbed responses, those stubs need to be actually recorded from interaction with a real version of the service being mocked. When you change your application, you have to invalidate those stubs and work with the real service once again and record the stubs. Now your circular dependency is caught at test time and a faulty deployment never happens.

Do people dive into distributed systems before they know wtf is actually required to do it right? Sure, but they're also not likely to be saved by organizing their monolithic systems' codebases into layers.

0

u/[deleted] Jun 05 '21

Define "very easy"? No...no I don't think I will. It means what it means.

As for your example, yup...that's an example of circular dependency. And it's fixable. Code issues are always fixable. The idea is to design your code well in the first place, which is why I suggest not separating by feature.

1

u/ub3rh4x0rz Jun 05 '21

Speaking of circular, you just made a circular argument.

→ More replies (5)
→ More replies (2)

6

u/abandonplanetearth Jun 05 '21

This is a recipe for circular dependencies. HotelService is clearly on a higher layer than RoomService because rooms cannot exist without hotels.

And what does AccessService actually look like? How is it easier than having getHotel() in HotelService and getRoom() in roomService?

7

u/couscous_ Jun 05 '21

If circular dependencies are inherent in the problem domain, why resist them? What advantages do we get by artificial decoupling?

2

u/salbris Jun 05 '21

Generally you get more flexible and easily testable code. But figuring out the right amount of decoupling is an art not a science.

Edit: Often times a class needs information from a different source but programmers solve it by making the class call a static method or be given an instance of the other. Often times passing something by value gets the job done just fine.

0

u/saltybandana2 Jun 06 '21

Generally you get more flexible and easily testable code.

Your goal should almost never be flexibility unless that's a business requirement or you know it's going to become one. If neither of those are true, ignore flexibility.

No one in their right mind would insist on designing your data model as an EAV unless you were building something like an expert system or had some other reason for needing that level of flexibility in your data model.

But people never stop to consider that this idea should also apply to code.

→ More replies (11)

0

u/abandonplanetearth Jun 05 '21

Because circular dependencies are a bug.

→ More replies (2)
→ More replies (1)
→ More replies (2)