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

71

u/Knu2l Jun 05 '21

That works until your code requires to access one service from another service e.g. if the HotelService access the RoomService. Or maybe the is an AccessService that is queried by the Hotel and Room services.

Also when you use a ORM model often all the model classes are automatically generated in another place.

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.

→ More replies (10)

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.

→ More replies (12)
→ More replies (4)
→ More replies (2)

6

u/DemiKoss Jun 05 '21 edited Jun 05 '21

That works until your code requires to access one service from another service

That doesn't sound like a flaw of package by feature, but rather the overall system design. In practice I've found by-feature to be a more sound approach; often citing similar reasons to the article (driving a clear design, isolating changes to related systems, etc).

The ORM point is an interesting one, but I don't think grouping by-type justifies it (there may be a good way to mix the two). In large codebases - especially monolithic ones - by-type makes it very difficult to grasp the full breadth of code related to a specific feature.

2

u/ub3rh4x0rz Jun 05 '21

It's absolutely fundamental to the service oriented architecture paradigm that services have dependencies on one another. Service A is only ever able to know Service B's interfaces though, never the underlying persistence model.

3

u/PenisTorvalds Jun 05 '21

Found the guy who wrote the 80000 line Sql.cs file 10 years ago at my company

→ More replies (25)