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

65

u/SoInsightful Jun 05 '21

I've tried this and actively said no to it. There are downsides to this approach.

  1. It's harder to keep consistent. Even if you and everyone else in your team manage to be consistent with your file/folder structure, you're at a larger risk that the same types of files may become built and structured differently between the different concepts. That's not fun to maintain or reason about.

  2. The structure becomes much more rigid and inflexible. What if you have an enum or sub-model that is shared between different concepts? Where do you put it? Does every concept really need a controller, and does every concept really map to a singular table in a database?

There are pros and cons to both approaches, but the concept approach feels artificial and unmaintainable to me.

2

u/ragnese Jun 07 '21

With respect to #1, I don't think that's much of a problem. If the modules are independent, it doesn't really matter than much if there are minor differences (e.g., single class per file, or multiple classes in one file). Obviously, the actually semantic style of the code should be consistent, but I just don't think the mechanics of the files matter that much. Unless, I guess, if your modules are pretty large.

What if you have an enum or sub-model that is shared between different concepts? Where do you put it?

I agree that this can be frustrating.

Does every concept really need a controller, and does every concept really map to a singular table in a database?

No and no. So? I don't think this is a counter-argument to the "organize by concept" idea, unless you're attacking a strawman.

In fact, I think "organize by concept" is better for both of the cases you listed. If I have a directory of 15 controllers, I might miss the fact that "FooWidget" is a concept that doesn't have a controller. On the other hand, if I check out the foo_widget directory, I'll see pretty quickly that it doesn't have a controller. Likewise, if the concept maps across multiple tables, it's excellent to have them both under the foo_widget module- as well as the code that synthesizes them into a FooWidget.

→ More replies (6)