22

Is the following method Pure? I'd say so, as it doesn't change in anyway the current class, thus, everything we can now currenly "see" in the class, before running this method will still be exactly the same after. Am I correct?

class Set {
    ...
    public ISet<T> UnionWith(ISet<T> set) {
       ISet<T> unionSet = ...

        foreach (Element element in this) {
            unionSet.Add(element);
        }

        foreach (Element element in set) {
           unionSet.Add(element);
        }

        return unionSet;
    }
}
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
  • Perhaps you're looking for the term *idempotent*? – Ben Voigt May 07 '10 at 05:18
  • Note that (conceptually) it's important to consider your level of abstraction when thinking if a function is pure. It's indeed pure object-wise, because it doesn't change the state of any present object (except the new one), but it's not pure system-wise - it allocates memory, so it changes the state of the system. – Kos Jul 31 '12 at 10:16
  • I can see the point of this feature but not the value: "This attribute is not enforced by the current analysis tools; you should use this attribute only if you are sure that the methods are pure." -MSDN – RBZ Dec 14 '12 at 16:59

1 Answers1

31

If by [Pure] you mean labeled with the Pure attribute from System.Diagnostics.Contracts, the documentation says:

Pure methods do not make any visible state changes.

Since your method appears to not make any visible state changes (i.e. no side effects), it would qualify for the [Pure] attribute.

Gabe
  • 84,912
  • 12
  • 139
  • 238
  • 1
    Is there any definition of "pure" to which his method doesn't conform, anyway? So far as I can see, it's mathematically pure as well - it will always produce the same output given the same input (`this` counting as input), and it will not change the state of the overall system in any way. – Pavel Minaev May 07 '10 at 05:42
  • 1
    Pavel: Some definitions of "pure" require that the inputs be immutable. Consider a wrapper function that memoizes an input function if it's pure. If the Set can change without the wrapper knowing about it, you could say that UnionWith isn't "pure enough" to memoize with a wrapper. – Gabe May 07 '10 at 05:52
  • There was right in the moment I made the question some answer stating it wasn't, by using a different pure definition that from C#'s Code Contracts. But its author later deleted it. The idea was that if at different times you pass the same ISet to my class, as my class' elements might be different at different times, you get different outputs, so your function is not pure. http://en.wikipedia.org/wiki/Pure_function – devoured elysium May 07 '10 at 08:08
  • @devoured: That was me, and Pavel can probably see my deleted answer; it depends on how you look at it I guess; if you view `this` as a parameter then it's pure, if you take the function signature literally (one argument only) then it's not. Gabe's answer was the technically correct one because `PureAttribute` is different from "pure function." – Aaronaught May 07 '10 at 12:43
  • I'd add that as the question stands, one can't tell whether the class itself is mutable or not. If it isn't, and the elements of the set cannot change after construction, then the UnionWith would be a pure function. (But this answer is still technically correct regardless of the class's mutability.) – Josh Gallagher Jul 07 '11 at 20:58