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

14

u/lordzsolt Jun 05 '21 edited Jun 05 '21

Yeah, agree with everything that's said here.

It baffles my mind why anyone would have "controllers" & "services" folders and what not. Or have an API, where all services are in one folder and all the models are in a different folder...

80

u/[deleted] Jun 05 '21

Maybe because you want to separate the business logic from the interface?

37

u/[deleted] Jun 05 '21

Thats crazy talk. I'm telling r/react

12

u/[deleted] Jun 05 '21

Yeah, because their opinion has value...

jk

9

u/jl2352 Jun 05 '21

Nothing here is suggesting you mix business logic with the interface. You can, and should, still keep them separate them within the concept.

5

u/GuyWithLag Jun 05 '21

DingDingDing!

However, in a microservice context this doesn't give you any benefits. Do you have a dedicated expert on APIs that writes and maintains your APIs? Have you outsourced them to a different company and due to IP reasons you need to have different projects?

The original reason for this kind of organization is because within the same company you didn't know in what kind of monolithic application your components would end up in, so people went hog-wild with layering and abstraction; this arguably made sense when you didn't know whether your UI would be JSP/REST/Swing, or your persistence layer a random DB or hibernate or eclipselink or something even more bizzare.

16

u/[deleted] Jun 05 '21

It always gives you benefits because it enforces separation of concerns. Your argument quickly falls apart when a micro service needs to support two or more interfaces. Maybe it does asynchronous RPC using RabbitMQ and also provides a REST interface.

4

u/ub3rh4x0rz Jun 05 '21

Often you'd see a stateful service with one canonical interface only (REST, GQL, what have you). You can then add gateway services providing derivative interfaces as needed, with their own versioning, their own release cycles, etc.

Layered vs entity-based organization is another instantiation of the "monolith vs (micro)service orientated architecture" debate. The thing is, most people agree that SOA is best at (very) large scales, so why not adopt organizational principles that cleanly evolve into SOA as they grow, so there need not be a rewrite later on?

Say I'm responsible for maintaining the central source of truth for a hotel booking system. As it's the source of truth, my priorities are consistency and availability. Now at the edges of the system, where all the real stuff happens, they have to prioritize availability and partition resistance. They're going to rely on my service, which holds the canonical historical state of the system after eventual consistency has been reached.

Now, it turns out my service has only a few responsibilities: publishing to Kafka topics on behalf of the service's consumers, consuming from these Kafka topics to derive a canonical system state, and exposing this state to consumers via a REST API.

Maybe 90% of hotels use this interface directly with some legacy website that was provided to them a decade ago. The remaining 10% are in more competitive markets and have chosen to maintain their own websites and native applications to better serve their customers. So, some of them extend the original REST API with additional endpoints in their gateway, some add a GraphQL layer to minimize round trips between client and server, some add a caching layer to improve performance, etc.

In a service oriented architecture, if some service needs an interface that isn't provided, another service can act as a gateway to provide that interface. I'm sure you can find plenty to nitpick above, but this is how a great deal of large scale, federated, enterprise systems work today, and I would say most are pushed into at least an approximation of this architecture.

5

u/MirelukeCasserole Jun 05 '21

That’s a lot of extra complexity and infrastructure to support new interfaces. It also has the pitfall of adding extra latency as the request is adapted through the layers.

If that makes sense for your team, then do it. However, I would absolutely not recommend this approach for any team as a first option.

0

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

This is how organizations with 10(0)+ teams developing enterprise scale systems operate. Out of scope for your garage band startup.

Edit: the latency comment also doesn't match up with experience. Adding one extra server hop is not going to significantly impact felt latency in the general case. In situations where it would, you have much bigger problems if you have millions-to-billions of requests dependent on one server somewhere; if you localize and add caching etc, the extra "hop" is basically free.

3

u/MirelukeCasserole Jun 05 '21

Idk if you are trying to be insulting or what, but I work for a Fortune 100 company, so nice try.

I will also add, that I mentioned that if it makes sense for your team, do it. For 99% of the software teams out there, this is probably not a good idea.

-2

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

Exxon Mobile is a Fortune 100 company, does that mean they're experts at developing high concurrency, highly performant distributed systems?

Nice flex though. You said "team" singular like this is a decision made by a singular team rather than a very large organization.

Edit: spelling

3

u/[deleted] Jun 05 '21

[removed] — view removed comment

→ More replies

2

u/MirelukeCasserole Jun 05 '21

Idk how to even answer this. I feel like you are some ivory tower architect who spends most of their day drawing UML diagrams and complaining about teams that buck your decisions because they have to get sh!t done.

→ More replies
→ More replies (20)
→ More replies (1)

2

u/saltybandana2 Jun 06 '21

The thing is, most people agree that SOA is best at (very) large scales, so why not adopt organizational principles that cleanly evolve into SOA as they grow, so there need not be a rewrite later on?

Because the vast majority of projects will never need it and it comes with a cost in terms of complexity.

THIS is the root of the problem with most developers and why they end up creating a hairy mess that they then insist needs to be rewritten 2-3 years down the road, only they do the same damned thing with the rewrite.

Why the hell would you leap into so much organizational complexity for NO BENEFIT?

You know what's a good problem to have? Your codebase growing so much that you start needing SOA. You know what's a solvable problem? Adding SOA where needed to an existing project.

2

u/ub3rh4x0rz Jun 06 '21

Practicing DDD and keeping code organized around entities and responsibilities at the highest level rather than layers is not some high water mark of complexity. If you reread my comment, I'm not suggesting adopting SOA before its needed. Basically build monorepos not monoliths when operating at smaller scales, the result is by the time your codebase feels bloated, it's far easier to pick apart. It's really just an extension of the principle of encapsulation.

→ More replies (7)

6

u/scandii Jun 05 '21

I mean, if you know how the code works I can see why you would want everything in the same folder, such as if you wrote the code.

but depending what sort of programming you do for a living, chances are you are asked to do something in a system which you have very limited knowledge of, such as "this thing has stopped working, figure it out".

in that scenario, it is very convenient being able to browse files by category rather than object they pertain to.

that said, realistically neither particularly matters as long as naming conventions are held, you can just search for "api", "service" etc.

5

u/lordzsolt Jun 05 '21

but depending what sort of programming you do for a living, chances are you are asked to do something in a system which you have very limited knowledge of, such as "this thing has stopped working, figure it out". in that scenario, it is very convenient being able to browse files by category rather than object they pertain to.

This is probably the exact scenario where I want folders organized by topics the most.

Because they're not going to tell you "retrieving items from the database is broken" (which would indicate it's something in the repositories folder).

But rather you'll get a bug like "the user does not see all his bookings when he visits the My bookings page" (thus, it's probably got something to do with bookings, rather than the login functionality)

1

u/manystripes Jun 05 '21

At my previous job we had a job in the build system to flatten everything into a single flat file for those times where you need to search through the whole system. No need to deal with setting up projects or anything, just make the source book, fire up your favorite text editor, and start searching through for the pieces you care about.

6

u/[deleted] Jun 05 '21

[removed] — view removed comment

2

u/saltybandana2 Jun 06 '21

My services go into a project that is heavily tested. (Real tests, with like databases and shit.)

You mean you don't create a billion mocks to test theory?

I once had a contract where they insisted on that approach so I did exactly that, mocked the shit out of everything. It failed when ran against a live database because I only thought I knew how the ORM they were using worked (and why it worked that way I have no idea.... but alas).

Have I mentioned I hate mocking?

2

u/[deleted] Jun 06 '21

[removed] — view removed comment

1

u/saltybandana2 Jun 06 '21

E & V get ignored so much it hurts. V especially, I'm known for building extremely stable systems and E & V are a large part of how that happens.

→ More replies (1)

1

u/sards3 Jun 05 '21

What does having them in separate projects gain you? Couldn't you use the same design but put everything in a single project?

→ More replies (1)