DEV Community

Discussion on: How do you convert a class to functions in FP?

Collapse
 
vonheikemen profile image
Heiker

Classes are not the problem. I would focus on the methods, do they behave like pure functions? Refactor those that don't.

For new projects you might want to start making a dedicated space where you have all your pure functions. The idea here is that you have a place where everything is pure and nice, and another where you can bend the rules a little bit.

Collapse
 
michaelcurrin profile image
Michael Currin

Thanks, good point. Refactoring the methods that do too much and are hard to test will help achieve some of my FP goal while still using classes.

My concern is that I will fall into OOP thinking and pitfalls by using classes, so am hoping to change the mindset for new projects.

One of the bad things I've about OOP is developers tend to put way too many methods on a class it makes sense conceptually to put the methods for updating employee and representing employee in the same class. But these are separate responsibilities and so should be in different modules and also should not depend on each other and should rather be dumb - just processing data they get.

I like the idea of pure functions in one section and less pure in another. Like where the IO gets handled.