4 ms·
I dispute your example. If you call f(2) and it always returns 4, it's idempotent and side-effect-free. If you call f() and it returns 4, then 8, etc, it is nei
by StavrosK 9y ago
I dispute your example. If you call f(2) and it always returns 4, it's idempotent and side-effect-free. If you call f() and it returns 4, then 8, etc, it is neither.
- mbid 9y agoFrom wikipedia: "A unary operation f, that is, a map from some set S into itself, is called idempotent if, for all x in S, f(f(x)) = f(x)."
- StavrosK 9y agoYes, in mathematics, not programming. And a function that doubles a number isn't idempotent even by that definition.
- Y_Y 9y agoOf course a doubling function is not idempotent! I think the confusion arises because side-effectful functions can be considered as having type f :: (RealWorld, OtherArgs) -> (RealWorld, OtherOutputs) and so for a garage door toggle you have something like t :: RealWorld -> RealWorld where the new state is the old one with the door opened/closed as appropriate. Now the idempotence condition becomes: t(t(world)) == t(world) but clearly t(t(doorOpenWorld) = t(doorClosedWorld) = doorOpenWorld != t(doorOpenWorld) = doorclosedWorld so this is where the notion comes from. If you abuse notation and just say a function of no arguments can be idempotent then you'll get confusion like this.
- smaddox 9y agoBut `GET(GET(x))` doesn't make sense, in general (and if it did, then you would not expect it to be idempotent), so clearly idempotency in this context is meant to mean side-effect free. They should probably just say side-effect free, though, to avoid the confusion.
- Ajedi32 9y agoIt's possible to be idempotent without being side-effect free. If you PUT some record, for example, then that operation _will_ have side effects (modifying the record). If you then PUT that same data again, the result will be the same (it's idempotent).
- jiveturkey 9y agothat’s not a side effect. in common usage, a side effect is an extra action, not the desired action itself. if you PUT some record, and some other record or state changes, that’s a side effect.
- __david__ 9y agoI suppose logging is a side effect and that makes practically everything technically non-idempotent (though on purpose).
- jholman 9y agoI don't disagree with common usage, but common usage is not what's being discussed here. It is a side effect in programming usage (not just HTTP). Something is side-effect-free if and only if the only result of it running is that you get an answer. If you ignore the answer, then you cannot tell you ran the function/method/call/whatever. PUT is not side-effect-free. That said, side-effect-free-ness is an incomplete paraphrasing of the HTTP spec (RFC7231); you'll notice that the only mentions of the phrase "side effect" are giving examples of legal side effects.
- swiley 9y agoI think because in math you don’t ever have side effects you usually use composition where in programming you usually use a sequence. So to change that function in to how people would implement it means rearranging the internal stuff and then it probably wouldn’t be idempotent be either definition.
- richardwhiuk 9y agoBut not the reverse. If you are side effect free you must be idempotent.
- PuercoPop 9y agoInstead of checking wikipedia for a general definition of idempotency, check the RFC for the definition that applies to HTTP 9.1.2 Idempotent Methods Methods can also have the property of "idempotence" in that (aside from error or expiration issues) the side-effects of N > 0 identical requests is the same as for a single request.
- cryptonector 9y ago"Idempotent" is one of those overloaded terms...