How to turn a script with shared state into stateless modules and pure functions?

I've been advised that I should write only pure functions and stateless modules, but I don't really know where to start. Would someone be willing to give me an example of how a base script like this could be turned into stateless modules with pure functions?

https://jsfiddle.net/r4gbdqmz/

It's quite tiny, don't worry. I would really appreciate it so I can start figuring out how to refactor the right way, thanks!

Posted Feb 6, 2018, 9:17 AM UTC

05
raleighpoint•

a basic attempt here https://jsfiddle.net/r4gbdqmz/14/

  • state should come from "above"

  • dependencies should be parameters for purity and statelessness

  • things that have "side effects" exist, but you can isolate them to keep as many pure functions as you can--see how now fewer things have access to external "shared" state?

  • if it's easy to write a unit test for, it's probably closer to the pure and stateless side

2
CertainPerformanceOP•

Thank you, I actually realized myself a couple days ago that one of the main keys to it was probably using higher-order functions like that - not that higher-order functions were a novel concept, but I never put together the idea that they're an appropriate tool for something like that.

"State should come from above" is a good phrase to remember. I had the feeling that what I heard was dogmatic for the sake of it and not to be taken entirely seriously. It's not so much that state should be avoided everywhere at all costs (if that's even possible), but that if state is necessary for non-convoluted code, it's better to contain it as much as possible, and like you say, send the state (or the necessary data in the state) as a parameter to other functions - which means it needs to start from somewhere high-up in the hierarchy.

2
jcunews1•

I don't think it's possible because the code which initially populates the results is not mentioned.

1
CertainPerformanceOP•

It's only populated in the updateElm listener (... .then(data => results = data) )

1
jcunews1•

That's not possible. The code in updateElm event handler tries to check the results variable first. If the full code is run, line #14 would trigger an exception because the results variable is still undefined.

1
CertainPerformanceOP•

Good catch, this was only a example I wrote in the hopes of getting a sense of how it would be done at the most basic level - it's not proper code. So let's assume that it checks that results exists first, such as if (results && results[1] === 'do not update')

1
MoTTs_•

For this particular piece of code, I don't think you can. Ultimately real world applications do have state. Pure functions are good, and I personally tell people to prefer pure functions, but I think it's important we don't turn it into religious dogma. The fact is your code has state that changes. If you were using React, you'd call setState(). Or if you were using Redux, you'd call dispatch() and pass in new state. And ultimately your code wouldn't look all that much different.

// React style
.then(data => {
  this.setState({results: data})
});
1