Monads: It's OK to Feel Stupid
Zone leader Matthew Casperson reports on his quest to find some clarity on Monads in Java EE 8
Join the DZone community and get the full member experience.Join For Free
Now that Java 8 has been released and the functional programming paradigm has seeped into my everyday code, I find myself once again scouring the internet for some clarity on Monads.
Invariably I end up trying to decypher code like this, which looks like spaghetti soup spilled onto the floor:
infixl 1 >>, >>= class Monad m where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a fail :: String -> m a m >> k = m >>= \_ -> k
Or trying to understand statements like “monads are just monoids in the category of endofunctors”.
Needless to say, my quest has ended in failure.
All of which left me feeling, well, exceptionally stupid. How is it that this concept that has generated more web pages than cute kittens could still remain so utterly incomprehensible to me? The only conclusion I could come to was that there was something fundamentally wrong with me. I gave up Googling Monads and instead looked back over my life, and the choices I had made, to try and find that moment when I should have known that I was never going to be smart enough to participate in this brave new world of software engineering, and instead should have moved into a career more suited to my dumbness.
After a few weeks stewing in self pity I decided to approach Monads from a different direction. I asked myself a few simple questions.
Can I Use Monads Without Understanding Them?
Yes I can. In fact, I have been using Monads for quite a while without realizing it, because jQuery is a Monad. Or maybe not. But either way I have been using something that provides benefits similar to those of a Monad without really understanding (or caring) what a Monad is.
My first big realization is that I won’t be writing my own Monads. Java 8 has streams and Optional, and 3rd party libraries like Javaslang have a bunch more Monads that I could use. It is unlikely that I will find a situation in my code that could be better solved with a custom Monad. All I need to do is recognize patterns that are suited to an existing Monad, and refactor my code accordingly.
Will Using Monads Make Me a Better Developer?
I’m going to say yes, although at this point that answer is based more on faith than actual experience.
Having seen how much more robust and testable my code is when it is written using the principles of functional programming, and given that Monads feature so heavily in Java 8, I’m inclined to believe that people smarter than me have seen value in Monads, so I should too.
Even if I don’t completely understand why I should use Monads, they are actually easy enough to put into practice, and doing so will ultimately make me better at my job.
Will Using Monads Help Me Understand What Monads Are?
Of course, and I’m kicking myself for not realizing this sooner.
Understanding Monads by reading tutorials on the internet is like learning to drive a car by reading the owner's manual. My head was being filled with abstract ideas and mathematical concepts that I had nothing to relate to. No wonder my Googling was getting me nowhere.
If you are going to read any tutorial on the internet about Monads, read monad tutorial fallacy. It made me realize that I’m maybe not as stoopid as the internet led me to believe.
So today I will dive into Monads, willfully ignorant of the mathematical principles behind it, and with blind faith that my code will be better off because of it. And hey, maybe one day I’ll believe I understand Monads enough to contribute my own well meaning Monad tutorial to the internet.
Opinions expressed by DZone contributors are their own.