Hacker News new | ask | show | jobs
by hschne 920 days ago
Idempotency is one mathematical concept that's pretty useful for software engineers to understand. Heck, might be more useful than BigO.

- Useful to know when you work with APIs (as the article outlines).

- Very useful when working with background jobs. You're not gonna have a good time if those aren't idempotent.

- Good to know for interviews. I was asked to explain idempotency a handful of times, weird as that is.

5 comments

Interestingly, the mathematical definition (and functional programming definition) of idempotency is slightly different. Or I've seen it used slightly differently, anyways - specifically, a function is idempotent if the result of f(x) is the same result as f(f(x)).

I've seen this lead to some confusing back and forth between software engineers, where one engineer means one definition, and the other engineer meant the other definition.

One irritating thing is that there are at least 4 different senses of "idempotent" in the wild:

* In the object-oriented sense, `obj.f(args...)` does not mutate `obj` after the first time.

* For pure unary functions, `f∘f === f` - that is, repeated application does not change the result, e.g. `abs(abs(x)) === abs(x)`

* For pure binary functions, `f(x, x) === x`. I'm not sure how useful this is but it seems to be used in math.

* For pure binary functions, `f(x, a) === x` and/or `f(a, x) === x`. These would probably be individually called left-idempotent and right-idempotent in some order, along with two-sided idempotent for the combination (akin to left identity (related!), left inverse, etc.); notably these are more general than (and all imply) the above. This could be extended to further arities; usually the preserved element will be either first or last in sane functions. "and not" works in one direction; many functions like "bitwise and", "bitwise or", "min", and "max" work in both directions.

Sense 2 is an example of sense 3, where the binary function is ∘.
> Good to know for interviews. I was asked to explain idempotency a handful of times, weird as that is.

Do you now reply "I've been asked this before, so your question has no effect"?

I've asked it a few times. Not knowing the specific word is fine -- but I expect devs touching http requests at all to understand the concept.
I want to see an Up Goer Five⁰ explanation of all the ways "you will not have a good time today" with distributed systems.

0: https://xkcd.com/1133/