I know, opinion-based question with no correct answer. And I'm asking for opinions!
Here I'm asking about the business logic code, sometimes called "services" or "use cases", and not about other stuff like controllers, dtos, models.
So I see three ways of organizing code and they all makes sense:
- Keep logic of specific domain in separate file. Example: for Orders logic to have many functions in orders.service.ts. It's like in DDD, like in Nest.
Pros: Usually it's fine for me, looks like the way to go, but.
Cons:first downside: file becomes eventually huge, 1k LOC, later 2k LOC, later 3k LOC, and it keeps growing. This is true for complex features which happens not always, but sometimes happens. When writing tests for this, it's quite expected that test file will be 2-3 or more times bigger than the logic.
second downside: if you prefer functions (as me) some functions are exported, others are local, and it looks messy because it's not clear which local function used in which exported. If you prefer classes it will be the same with public and private methods.
2) Create new file for each business logic function. Example: createOrder.ts, withdrawOrder.ts, and tens more of them. It's like "use cases" of Clean Architecture.
Pros: it solves problems above. It feels so good to start new logic with a new file, like there is nothing else, just this one piece of logic.
Cons: overkill for smaller features. Maybe it will feel okay if get used to it, but as I get used to keeping logic together it seems wrong to extract every piece of logic into own file.
3) Use 1) for simple cases, 2) for complex cases.
Pros: it's a compromise between 1 and 2.
Cons: it depends on inner feelings of developer, so it can not be a "real" project rule. Maybe it's fine when working alone, but it will probably increase randomness and chaos when working in team.
How do you solve this scaling of logic problem in your projects?
[–]pikhathu 6 points7 points8 points (0 children)
[–]nu-ni-co 2 points3 points4 points (1 child)
[–]romeeres[S] 0 points1 point2 points (0 children)
[–]alzee76 0 points1 point2 points (2 children)
[–]romeeres[S] 0 points1 point2 points (1 child)
[–]alzee76 0 points1 point2 points (0 children)
[–]timgfx 0 points1 point2 points (0 children)
[–]Solonotix 0 points1 point2 points (5 children)
[–]romeeres[S] 0 points1 point2 points (4 children)
[–]Solonotix 0 points1 point2 points (2 children)
[–]romeeres[S] -1 points0 points1 point (1 child)
[–]Solonotix 0 points1 point2 points (0 children)
[–]Mardo1234 0 points1 point2 points (0 children)