## Idempotency is a goal in all software design

The definition of Idempotence in computer science is that an operation can be repeatedly called and still produce the same result.

This identity explains it well:

f(f(x)) = f(x) for all x

but x does not necessarily have to equal f(x) for all x

This is such a desirable goal of all code that we write. Here are some examples that occurred to me recently

**example 1** - Software Testing

If you cannot ensure idempotency of a test then every time you run a test you would have to recreate your data as its result is dependent on the state of the initial result.

**example 2** - Monads

Monads need to allow associative composition of functions, where compose "binds" two functions

f.Compose(g.Compose(h)) = (f.Compose(g)).Compose(h)

If these functions were not idempotent then associative condition of Monads could not be satisfied.

**example 3** - HTTP REST (REpresentational State Transfer)

GET, HEAD, PUT and DELETE are idempotent whereas HTTP POST is not. Observe how the majority of the operations here must be able to return the same result from multiple calls.

**example 4** - Asynchronous functional programming

If you want to spin off multiple asynchronous tasks and not worry about a function being called twice on the target object, you should design the function to satisfy idempotence, then it is just a case of "may the fastest thread win".

I'm hoping to keep this word in mind as a fundamental design principle when writing code in future.