r/functionalprogramming • • Oct 22 '23
Question How to manage side effects in a backend app and structure a project overall?
I'm a typescript dev of a few years, when working on my own projects have always tried to build them functional. As they've scaled though and I've gotten more into FP, started to come across the issues of: how do you structure an FP project properly, i.e. in terms of folder structure? and how do you manage the reality that a bulk of a web apps backend will be dealing with side effects?
I've been looking into hexagonal pattern - i.e. spitting an app between a pure core and an impure shell - and it looks promising, but I can't really seem to see any real way to make sense of the side effects. The closest I've come so far is for the core to export factory functions, which allows the core to stay pure and for the shell to execute said functions with it's own dependencies, i.e DB's models.
However this feels like a lot of leg work which in the end doesn't feel that different from just using the db models directly - as they are going to be called regardless? The reality is whether I export this logic from the core in a pure sense or just use the db models directly, when my app is running, they will still be called. This separation seems abit arbitrary as I'll still need to test the shell's implementation of this logic so it doesn't make much difference?
Not trying to put down FP in any way, just seem to be abit stuck in trying to find a project structure which will allow for the pure and modular style of FP to be maintained project wide. Any help would be appreciated.
3
u/freefallfreddy Oct 23 '23
I think folder structure is just one way of separating pure and impure code. I wouldn’t get too hung up on that, find a pattern you like and use it.
Secondly: for me one of the major motivations to use functional programming in an application is so I can have a lot of pure code that I can run a tonne of unit tests on.
I like to keep all my side effects as simple and dumb as possible so I don’t have the need to extensively test it. All the logic can then just be pure and really fast to test.
Does that help?
7
u/jherrlin Oct 23 '23
I don’t do TypeScript and can’t answer for folder structures.
I like the Imperative shell, functional core architecture. Depending on what context (interpreter) you run your app in you can simulate the imperative shell layer. If it’s for unit testing, intergration tests, dev environment, prod and so on. This makes the app more flexible and easier to reason about from different angles.
I tend to use dependency injection or just pass down a function.
5
u/SIRHAMY Oct 23 '23
A few rules of thumb I use:
2
u/iams3b Oct 25 '23
I've tried many attempts at doing this functional core / imperative shell in node and TS, and have come to the conclusion there is no nice way. The reason is as you've mentioned, a web api is primarily only IO and side effects, and trying to abstract away the core of your app makes it over complicated for almost no benefit -- especially in node where it's quite easy to mock APIs out at the network level and use in-memory databases for integration testing.
What I do is follow functional principles in the parts where I feel it helps. All my DB models are simple objects (no classes), updates are done immutably, responses are mapped using transform functions, and routes return a custom
OK | Errortype that I map to a response.For a lot of CRUD applications, I don't think there really is enough business logic to justify a full "functional core"