0

Currently I'm studing functional programming and I have a doubt about when should I use monads...

Is there some rule that I can use to know whenever to use it?

  • 2
    You could also have asked _"Should I use Promises?"_, because they are monad-like. Your actual question should be (and probably is): Should I think and code like a mathematicians. Definitely yes. Its harder and less intuitive, but the result is also more universal, more reusable, less error prone. –  Jan 29 '18 at 13:09
  • I"m here to echo Ftor's comment and add this is an opinion-based question and doesn't belong on SO – Try including a [mvce](http://stackoverflow.com/help/mcve) with a *specific* question about it – Mulan Jan 29 '18 at 15:39
  • Related: [*How do pipes and monads work together in JavaScript?*](https://stackoverflow.com/q/46917309/633183) – Mulan Jan 29 '18 at 16:03

1 Answers1

0

There is not a rule and it is not mandatory but failing to take advantage of monads may make your code harder to work with (e.g., callback hell, nested conditional branches, more verbosity). The essence of modern software development is composition, and monads make composition easier.

There is a very good article on medium covering the topic deeply:

https://medium.com/javascript-scene/javascript-monads-made-simple-7856be57bfe8

Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43
  • 2
    It might be worth mentioning that [Elliott does have his detractors](https://www.reddit.com/r/javascript/comments/625yaz/eric_elliots_series_on_functional_programming_in/?st=jd061k08&sh=a3aa9ebb) because he often gets material wrong. – Andy Jan 29 '18 at 12:09
  • All programming trends have detractors, and it is good otherwise we would be stuck in 1978 with programming languages and patterns – Mosè Raguzzini Jan 29 '18 at 12:13
  • 1
    My comment wasn't about a trend, but about that particular writer. – Andy Jan 29 '18 at 12:14
  • 1
    @Andy Yeah, he is at least sloppy in his statements "_Flatten means unwrap the value from the context. `F(a) => a`_" (from the linked article). –  Jan 29 '18 at 13:31