FUNCTIONAL PROGRAMMINGNo Man's Land makes you sick
We've done significant efforts to move to functional programming and immutability at my customer in our current project. The application is written in Kotlin and uses Spring (rest - yes, certainly REST as well, but the linguistic rest of the framework jungle is insignificant now).
The immutable part was OK thanks to Kotlin and its excellent support for data classes, but the FP...
Yes, we had many top level and higher order functions. Yes, they were pure, but..
They were still sitting in *Service
, *Repository
, *Adapter
, etc. classes implemented as companion object methods. Some places it was even worse. And yes, we still had way too many val
declarations in the code. It was something in between OOP and almost FP, but heavily infected by the old, imperative, bad smelling way of coding (in FP sense). It was a kind of in between. It was a No man's land.
I was sick last weekend. First I thought, it was the flue. Now I'm sure, it was the code that made me sick.
I had an adapter class, that looked so ugly, that my immune defence weakened, and on the way home on Thursday the viruses grabbed the chance and took over. I deserved it.
But being sick is not just bad. It is rather just bed, but as a positive outcome I slept enough - 2 days in row. On Monday I was still not OK (yes, you got it, that adapter class was that ugly), so I worked from home while the tea and honey was working on (or rather in) me. Anyways, it was total calmness. I had good chance to think, read, and deepen my knowledge as I was cutting through the curtain of still unknown which was hiding the real purity of FP from my eyes.
I read about functors, applicatives and monads in Kotlin. Than about the reader (environment) monad, and how to solve DI with it. And once in a while, as I started coding, the picture just became cleaner and sharper. In part of a second I ended up with really cool Functional Code with chained method calls, contexts injected through reader monad, and no val declarations here and there. It was an aha moment, a personal moon-landing. Amazing.
Then came the next day. As said, I've been really thinking hard for many days about the problems we had, and it did not let me sleep well. Thanks to that I got far ahead in FP and how to integrate it with Spring and Kotlin - compared to my teammates.
But I had to check in that Dancing Queen Code, which was young, charming, and fresh compared to that old, bloody, slow, gunshot veteran that we had in advance.
And I did it.
Vegard, my teammate. A very smart guy. He looked at the code, and I saw the despair taking over his face.
Took a while, and than he got enlightened and he really got engaged. It is an amazing world we have ended up now in, and we are still discovering the countless benefits. Because there are. Many of them.
I have to mention what was the last kick in the right direction. I have recently exchanged mail with Venkat Subramaniam about FP and logging. In his answer this little expression:
'in the middle of the pipeline...'
grabbed my mind. I was thinking... Yes... Piping... Piping functions... And in that specific moment - as I was reading these words -, we did not really have well-formed piping at all in the codebase.
Not like today.:)
So, the moral of the story: if You decide to go the FP way, do not do it halfway. Halfway is No Man's Land. Halfway makes You sick. Go Ahead! Be Functional!
References: