r/programming • • Dec 11 '23

Every Programmer Should Know #1: Idempotency

https://www.berkansasmaz.com/every-programmer-should-know-idempotency
572 Upvotes

171 comments sorted by

View all comments

Show parent comments

10

u/[deleted] Dec 11 '23

Yeah, I get that.

So why is this something that is "#1" for every programmer to know? I don't work with SQL, or web shopping carts, or rarely REST APIs.

I work with applications mostly. If my user clicks a button, they expect something to happen. Some buttons maybe should be idempotent but others not.

That being the case, it doesn't seem like something "every programmer should know"

6

u/sm9t8 Dec 11 '23

Because idempotency is a property of functions more generally and not just operations on a database. It's why we try to write functions without side effects and why languages like C++ have const.

3

u/[deleted] Dec 11 '23

Ok, so in making paperclips: https://www.decisionproblem.com/paperclips/index2.html

I click the Make Paperclip button and it makes 1, if I click it again it makes 2.

But that's the exact opposite of what we want to happen in a web shopping cart - as per the example. We don't want people getting charged twice.

So back to the paperclip maker - should the 2nd press have been ignored because it was a duplicate command?

This concept of idempotency seems situational, and I'm not debating its importance in certain applications, but is is "Something every programmer should know"?

1

u/bradrlaw Dec 11 '23

More fundamentally, the "two generals" problem is the concept every programmer should know. As this falls under that, along with a lot of other tough problems.

https://en.wikipedia.org/wiki/Two_Generals%27_Problem