DEV Community

Discussion on: When is nesting good or neutral?

Collapse
 
jbristow profile image
Jon Bristow • Edited

Nesting conditional logic is not always bad.

Nesting promises is always less clear than it could be.

Consider:

  • alpha takes a url and returns a promise of an http response.
  • beta takes a url, calls alpha, and returns the promise of some heavy async calculation of an http response.

Versus in un-nested:

  • alpha takes a url and returns a promise of an http response.
  • beta takes a http response and returns a promise of some heavy async calculation of that response

All nesting can be expressed this way. (Convince me otherwise)

This property of monads is why Clojure has a threading macro, Haskell has pointfree, and most ML derivatives have a concept of piping.

E.g. you can define function gamma that is expressed as alpha | beta (Aka beta(alpha(x))) that just takes a url and spits out a single promise of a processed http response, but still isn’t nesting!