Refactor Stateful Code To Use A State Monad

Share this video with your friends

Send Tweet

When we start to accumulate functions that all work on a given datatype, we end up creating a bunch of boilerplate code in almost every function to handle the changes we need to make to our records’ values. This can lead to not only undesirable boilerplate present in all of our functions, but also can cause us to have to create variables just to manage our stateful changes.

We’ll take a look at a couple patterns that can act as early warning signs that will eventually cause us to not have a good time. Once we know what the smell is, we’ll look at how moving our computations into State can clean up all of our state management code by making it the responsibility of State. This allows our functions to only describe how state should change over time versus us having to change it ourselves.

Jorge Cortes
~ 7 years ago

Hi everyone, I've been watching these lessons which I think are one of my favorites :) But I have a question on this video. When I get to the part where I'm using propOf the node throws an error that says:

Error: Cannot find module 'crocks/helpers/propOr'

How can I fix that? thank you1

Ian Hofmann-Hicksinstructor
~ 7 years ago

Sorry for the trouble, propOr is not available in crocks yet. (It will be in the next release tho) This lesson is using a propOr in the helpers.js file.

it can be found on the github here.

hope this helps!!

Jorge Cortes
~ 7 years ago

ohhh thanks a lot !!

Mitchell Stewart
~ 7 years ago

Thanks! This was one of my favourite egghead courses.

Robert Pearce
~ 6 years ago

Great that this mentions recognizing not great patterns and uses what we've learned to address the issue.