-2

A pure function is one which given the same arguments, will always return the same result and will have no side effects.

So Sum(x,y) => x + y; is pure because it meets this criteria.

However, in a language like C# where you can have properties, this makes things more complicated...

class Summer { public int X { get; set; } public int Y { get; set; } public int Sum() => X + Y; } 

In the above, can Sum be considered to be pure? Can you make the argument that X and Y are still just parameters to the function?

Or would it be better if refactored to something like:

class Summer { public int X { get; set; } public int Y { get; set; } } static class SummerExn { public static int Sum(this Summer s) { return s.X + s.Y; } } 

In the extension method, s is a parameter so this meets the criteria for being pure I think, but realistically there is no practical difference since the underlying variables are the same. Would there be a technical reason this code is better, such as being more easily tested, faster, more memory efficient, easier to logically reason about etc. ?

14
  • 1
    i would say only if the properties themselves are pure. Commented Sep 23, 2021 at 20:51
  • 2
    The version with an X and Y property and calling Sum() gets X and Y is not pure. Easy to prove: Call Sum(), get 0. Set X to 1. Call Sum() get 1. Called with the same arguments, got different results. Commented Sep 23, 2021 at 20:52
  • 2
    This has now been closed as being opinion based - I'm not sure this is correct. A function is either pure or it isn't, and my question was "is this pure". Can anybody suggest an edit to make this clearer? Commented Sep 23, 2021 at 21:04
  • 1
    May be helpful: stackoverflow.com/questions/65872468/… Commented Sep 23, 2021 at 23:43
  • 1
    By your definition of pureness, your second method is actually not pure, because it may be passed "the same" reference to a single Summer object multiple times and come out with different results (if Summer has been mutated inbetween). Of course the exact same argument applies to the first method if you consider this to be a hidden parameter. In other words: there is quite literally no difference and there cannot be, as long as you mix in references to mutable objects. You can have methods that will not modify state and so are easier to reason about, but that's not the same thing. Commented Sep 24, 2021 at 20:32

1 Answer 1

-1

Your example doesn't meet the definition you gave:

A pure function is one which given the same arguments, will always return the same result...

Every call is given the same arguments (none) yet obviously can return different results. Another definition from Wikipedia makes this a little more explicit:

The function return values are identical for identical arguments (no variation with local static variables, non-local variables...)

Properties are non-local variables.

And to be especially pedantic, not only is your example not a pure function, it's not a function at all. A function-like thing that's a non-static class member is called a method.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but I'm not sure I understand - the wikipedia definition doesn't say it can't use non-local variables, just that there can't be variation in them, unless I'm misreading it. If I don't change the property values between calls, the result would be the same each time I call it. Also, I'm aware C# calls this a method, but is this an important distinction here? I'm all for being pedantic if it helps it to be clearer.
It matters that it can change, not that it did or didn't in a particular case. A function that isn't pure because it sometimes mutates external state doesn't become pure when it doesn't mutate state. A function that returns a different value when external state changes doesn't become pure when the state doesn't change.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.