Questions tagged [pure-function]
A pure function is one that always evaluates to the same thing given the same arguments, and cannot change or depend on any external state.
42 questions
11 votes
4 answers
1k views
Is it OK for a function to both do-something and return-something?
Is it OK for a function to both do-something and return-something? I try to avoid it for the most part, but there are situations where trying to avoid it would lead to worse/less clean code. Ex: if ( ...
1 vote
5 answers
941 views
Should I repeat calculations twice to follow "return a value or have side-effects, but not both"?
According to Origin of "a method should return a value or have side-effects, but not both", a method should either return a value or have side-effect, but not both. However, I often see some ...
18 votes
5 answers
6k views
Is there a non-deterministic function without side effects?
By definition, a pure function is deterministic + no side effect. Is there any example for a function which has no side effects, but is non-deterministic? I.e., a function without side effects, but ...
1 vote
2 answers
851 views
What's the difference between a pure function that expects a complex object of a particular type and object oriented programming?
What's the difference between writing OO code that depends on internal state and writing a pure function that expects an argument that is a data structure of a specific type (and thus has internal ...
2 votes
1 answer
444 views
Can I enforce "functional core, imperative" shell with a framework?
The design pattern known as "functional core, imperative shell" is about separating side-effects from pure calculations, where business logic is supposed to be pure and then coordinated by ...
1 vote
3 answers
453 views
what it means when someone says - "statements/instructions are not composable"?
I have been using c# and trying to learn FP. In context of FP I often hear that usage of basic assignment or return statements are not considered composable, hence their usage is not advised in FP ...
5 votes
5 answers
631 views
Implicit reading/writing of state in OOP hurts readability, maintainability, and testability. Good way of mitigating this damage?
OOP makes state reads and writes implicit. For instance, in Python: class Foo: def bar(self): # This method may read and/or write any number of self.attributes. # There is no way ...
11 votes
2 answers
2k views
How does functional programming handle the situation where the same object is referenced from multiple places?
I am reading and hearing that people (also on this site) routinely praise the functional programming paradigm, emphasising how good it is to have everything immutable. Notably, people propose this ...