As far as I know idempotent operation is a operation that can be applied many times with the same effect. Also I learnt recently that updating a tuple in a database is also idempotent. I thought that an idempotent operation was like a read-only stateless call (thanks to HTTP world with its famous GET
request example). So, is changing data in a file (or database) idempotent? Is appending (extending) data in file/database idempotent? Why?
An operation $O$ is idempotent if $O(O(x)) = O(x)$. For example, if you update record $R$ to value $T$ and then again update record $R$ to value $T$, then it's the same as if you updated only once. But if you append $T$ to record $R$ twice, it's not the same as appending only once.
As mentioned, an operation $O$ is idempotent if $O(O(x))=O(x)$ for any $x$. The important thing to realize is what is the domain, what are the possible values of $x$, and what are the things we're comparing with the $=$ sign.
In mathematics, there is usually no mutability, we're comparing elements constructed over some mathematical structure.
In (imperative) programming, what changes is the state of a program. So our domain is the set of all possible states of a program. You can thing of such a state a snapshot of the content of all the program's memory, CPU registers etc. In this context, an operation is something that changes a state of the program into another. By $O(x)$ we mean the state of the program that we get by running $O$ from state $x$.
Now we see what it means that an operation is idempotent. It is a piece of code that changes program's state in such a way that if we run the operation twice, we get the same state as if we run it only once. Most typical idempotent operation is setting a variable to some given value (or updating a database record etc.). Calling
// call O twice
var := value;
var := value;
is the same as
// call O once
var := value;
because the second time we call the operation, the state doesn't change (no matter what the original state was) - we know the value is already there.
-
1$\begingroup$ Maybe it is hard to understand, because we change the state with setting variables. $\endgroup$ – rook Sep 2 '13 at 15:19