Pure methods – where to put ’em?

Pure functions have lots of pros. They are predictable, composable, testable and you never have to mock them. Thus, we should try to increase the number of pure methods/functions in our code base, right? So how would you do that? If you have a method with both side-effects and calculations, you can sometimes life the side-effects out of the method. That is why lifting side-effects higher up in the stack trace will increase white-box testability. Taken to the extreme, you end up with a class with only properties, and a bunch of functions that operate on that class, which is close to functional programming with modules and explicit state (although you lose encapsulation).

Anyway, you have a class, you have a bunch of methods, you realize some could be made pure easily. Would you do it? In MVC, would you create a helper namespace and put your pure functions there? Or is this just an empty intellectual exercise with no real-world applicability?

submitted by /u/usernameqwerty002
[link] [comments]