views:

1240

answers:

6

What is an idempotent operation?

+7  A: 

no matter how many times you call the operation the result will be the same.

Robert
+15  A: 

In computing, an idempotent operation is one that has no additional effect if it is called more than once with the same input parameters.

Pure functions (those whose return value only depends on the input parameters) are always idempotent (Python syntax examples):

def sq(x):
    return x * x

Functions that only set a value are idempotent:

g_x = 5

def set_x(x):
    global g_x
    g_x = x

However, functions that do something like append to a global list are not idempotent:

a = []

def add_x(x):
    a.append(x)

Idempotent operations are often used in the design of network protocols, where a request to perform an operation is guaranteed to happen at least once, but might also happen more than once. If the operation is idempotent, then there is no harm in performing the operation two or more times.

It is somewhat unfortunate that in mathematics, the use of idempotent is slightly different. See the Wikipedia article on idempotence for more information.

Greg Hewgill
The mathematical definition of idempotent is only superficially different. If you realize that an idempotent function with side effects also has an implicit parameter of the state of the interpreter/machine, then it is clear that the function is also idempotent in the strict mathematical sense.
@unknown: That's true, but in the computing sense you don't usually talk about functions taking as a parameter the state of the whole world (unless you're working in Haskell!). So by drawing the natural comparison between "computing function" and "mathematical function", the use of idempotent looks different.
Greg Hewgill
The concept is, however, fairly common, and one that all programmers should understand. For example, in object oriented programming, methods have a pointer to "this" as an implicit parameter, which is in strict OOP languages like Java the full extent of the non-local variable state that can be modified directly (ie. without another method call).
That's a good point about the implicit "this" parameter and the object state that implies.
Greg Hewgill
+6  A: 

An idempotent operation leaves everything in the same state if you call it once or many times, provided you pass in the same parameters.

Caleb Huitt - cjhuitt
+2  A: 

It is any operation that every nth result will result in an output matching the value of the 1st result. For instance the absolute value of -1 is 1. The absolute value of the absolute value of -1 is 1. The absolute value of the absolute value of absolute value of -1 is 1. And so on.

See also: When would be a really silly time to use recursion?

Oorang
+4  A: 

Hi

An idempotent operation over a set leaves its members unchanged when applied one or more times.

It can be a unary operation like absolute(x) where x belongs to a set of positive integers. Here absolute(absolute(x)) = x.

It can be a binary operation like union of a set with itself would always return the same set.

cheers

Andriyev
+3  A: 

An idempotent operation can be repeated an arbitrary number of times and the result will be the same as if it had been done only once. In arithmetic, adding zero to a number is idempotent.

Idempotence is talked about a lot in the context of "RESTful" web services. REST seeks to maximally leverage HTTP to give programs access to web content, and is usually set in contrast to SOAP-based web services, which just tunnel remote procedure call style services inside HTTP requests and responses.

REST organizes a web application into "resources" (like a Twitter user, or a Flickr image) and then uses the HTTP verbs of POST, PUT, GET, and DELETE to create, update, read, and delete those resources.

Idempotence plays an important role here. If you GET a representation of a resource (eg, GET a jpeg image from Flickr), and the operation fails, you can just repeat the GET again and again until the operation succeeds. To the web service, it doesn't matter how many times the image is gotten. Likewise, if you use a web service to update your Twitter account information, you can PUT the new information as many times as it takes in order to get confirmation from the web service. PUT-ing it a thousand times is the same as PUT-ing it once. Similarly DELETE-ing a resource a thousand times is the same as deleting it once. Idempotence thus makes it a lot easier to construct a web service that's resilient to communication errors.

Further reading: RESTful Web Services, by Richardson and Ruby (idempotence is discussed on page 103-104), and Roy Fielding's PhD dissertation on REST. Fielding was one of the authors of HTTP 1.1, RFC-2616, which talks about idempotence in section 9.1.2.

Jim Ferrans