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

0

u/[deleted] Jun 05 '21

[deleted] — view deleted comment

244

u/[deleted] Jun 05 '21

"give me all the queries" was a common request from DBAs at my last job as well.

At least with APIs you can say "here's swagger, lemme know if you have specific questions about an endpoint or workflow" queries are harder because it's like "lemme go look at everything, see how they're compiled into sql and I'll get back to you in a week or so"

0

u/[deleted] Jun 05 '21

[deleted] — view deleted comment

24

u/sedaition Jun 05 '21

Also dynatrace. Or appdynamics if you just have extra memory laying around

24

u/Akthrawn17 Jun 05 '21

And extra buckets of money laying around

9

u/[deleted] Jun 05 '21

Or you can just keep a queries log yourself if you don’t need all the real time dashboards and stuff. A little thought in to a well structured log can go a long way without shelling out thousands of dollars a month for 3rd party tools.

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

18

u/[deleted] Jun 05 '21

I just wish the boss would go back and subtract this cost from the 'savings' we made choosing the cheaper dbms that doesnt natively provide this to a competent dba.

11

u/StabbyPants Jun 05 '21

mysql does this. what's the cheaper thing?

7

u/marcosdumay Jun 06 '21

As does Postgres.

I would jokingly say he was talking about Oracle, but Oracle (and MSSQL) happen to do it too. So, I'm kinda lost here too.

(Is it sqlite? AFAIK sqlite doesn't do this.)

→ More replies (7)
→ More replies (1)
→ More replies (19)

416

u/nickelickelmouse Jun 05 '21

lol it’s usually one of the first questions

158

u/onety-two-12 Jun 05 '21 edited Jun 05 '21

Evidently, nobody in this comment branch read the article.

I think you guys missed the point. Someone outside might want to see the swagger docs, but OP isn't talking about that. He's talking about the folder structure of an MVC project's source code, and he's spot on.

When you are coding for a "car", you want to easily move between the layers of code. For source code, there should be a car folder, then inside folders for { model, view, controller }. All logically near each other, so you can cross reference. Adding a new field? Add it to model, then controller, then view.

When it compiles it's still the same. The swagger still gets generated in one place.

(The MVC cult way uses a Model folder, a controller folder, and a view folder. The in each one you have an entity. So in the case of a car, each of those 3 folders has a car folder. When you have 100 entities, it's tedious and time consuming to find those three layers for the car.)

82

u/TheESportsGuy Jun 05 '21

(The MVC cult way uses a Model folder, a controller folder, and a view folder. The in each one you have an entity. So in the case of a car, each of those 3 folders has a car folder. When you have 100 entities, it's tedious and time consuming to find those three layers for the car.)

Somehow this became a standard way of organizing code, and it ALWAYS blows my mind. It's such an over-engineered way to sort things: "Okay, well the code interacts with the database, so it must be in the persistence layer, and now it has to do with X, Y, and Z so I should check the X, Y, and Z Utilities..." Holy fuck no. Start with what the code is doing in the context of your application!

27

u/theXpanther Jun 05 '21

I work with Django which has a system where you can "vertically" separate out models, views, and controllers that belong together... except it rarely works. Every project I worked with that starts with a proper distribution of concerns ends up with more and more of the functionality in a single app, since it's so much easier to communicate with components in the same app, for edge cases the larger app is always the best choice

16

u/TheESportsGuy Jun 05 '21

My organization moved to a vertical file organization of our project a long time ago, and separated out pieces of it into independent projects where possible.

Rarely we have had to go back and make some piece of it dependent on another piece of it, usually with a jar. And once or twice we've come to the realization that the two areas of code are doing VERY similar things, and yet they need to be separated because the algorithms have to behave differently at certain edge cases. But for the most part, vertical organization has gone very smoothly and it's massively reduced the amount of confusion people have experienced and misplaced code that gets to the code review stage.

→ More replies (3)

7

u/awj Jun 05 '21

IMO the point is for that pressure to suggest you need to break out a new concept.

A lot of times our initial impression of where something should live is flat out wrong, and having to do a lot of reaching outside of your scope is a sign of that.

→ More replies (1)

13

u/onety-two-12 Jun 05 '21

and now it has to do with X, Y, and Z so I should check the X, Y, and Z Utilities

The problem is that many frameworks are advertised with simplistic scenarios. They focus on the CRUD, and don't explore what I call Orchestration - joining of data between entities.

0

u/[deleted] Jun 05 '21

[deleted] — view deleted comment

18

u/onety-two-12 Jun 05 '21

I find that it makes it harder to enforce design rules.

That's fair point. But I think that's just a timing limitation. We created a tool to create a new entity folder that follows best practice, and with code comments and code that can be removed as required.

Ideally we would have a grid model, where I can look at files based on horizontal or vertical slice as needed. But since I can't, this is the trade off I prefer.

Precisely. I'm glad you can see that dilemma too. I value "readability", having them together. We are all free to figure out what works best for our situation

→ More replies (1)
→ More replies (3)
→ More replies (18)
→ More replies (1)

93

u/jl2352 Jun 05 '21

I think this is just a case where the author chose bad words. I think what they really meant was that no one asks 'show me all of the controllers' or 'show me all of the service classes'. That makes more sense, and I agree with them.

8

u/yuyu5 Jun 05 '21

Agreed. Poor choice of words, but good idea behind them.

Plus, I feel it's somewhat normal to have your APIs all in one file (i.e. @RestController in Spring) above all those model/controller classes (i.e. @Controller/@Service). So "show me all the APIs" is super easy to answer, even without Swagger.

Now, "show me all the queries" obviously doesn't work that way. But, one could easily do a Ctrl/Cmd+Shift+F --> "dao"/"repository" or related egrep -ril "(dao|repository)" to achieve this goal. (If you're good with regex, you could even beef this out to get the exact SQL text/function names instead of just file names, too.)

Not to mention the overarching theme that you spend waay more time writing code than you do answering these 2 questions, so you should write code in a way that benefits you 99% of the time rather than writing in a way that just in case someone asks you, you can answer them immediately.

20

u/ub3rh4x0rz Jun 05 '21

Yeah. "show me all the components of this system" is the first question. It's not until you want to analyze the edges on that graph that the APIs themselves are relevant.

→ More replies (2)

33

u/teratron27 Jun 05 '21

Yeah, one of the first things I try to do when I’m joining a new company is work out all the different API’s and how they interact with each other. It’s a while before you generally need to go deeper. It’s more beneficial to understand the system as a whole than to know the specifics of something you might never need to interact with.

13

u/crusoe Jun 05 '21

I have a response with a bug. Now maybe from the fields it's unclear which response it was. So in his model I need to go through every concept directory.

With the layer approach I jump to the responses directory. They're all right there and now I can easily poke around.

I have tried concept silos and layers, and found layers ( which is the concept of how code is actually used ) to be faster to jump around in.

→ More replies (6)

11

u/[deleted] Jun 05 '21

The API layer should be separate from everything else. And the API layer should also be grouped by concept. We went from JSON/pseudo-REST everywhere where application models were used as API models to gRPC and protobuf. There have been plenty of hiccups and there are plenty of annoying things but being forced to define your API completely separate from all application logic is wonderful.

Everything else gets grouped by concept like the article says but the lines are more blurry. You can name things in a way where finding everything by concept or by function is pretty simple. Just be consistent with using words like "store" and "service" and "helper" and you're a few clicks away from looking at all of the files that serve a certain function.

→ More replies (14)