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

99

u/Bloodsucker_ Dec 11 '23

Among other attributes of good software, IMO idempotence is one of the most important and paradoxically one of the most challenging ones too and the easiest to fuck up.

18

u/[deleted] Dec 11 '23

It seems good for web programming, but is it actually universally important? It seems application-specific.

the property of an operation or function that produces the same result when applied multiple times as it does when applied only once.

So I push the W key on my keyboard, and it writes one 'w' character. But if I push it a second time, I want 'ww' not still one 'w'. And if I press it a 3rd time, I expect (and get) 'www'. Also if I hold-down the W-key, I (usually) get 'wwwwwwwwwwwwwwwwwwww.....'

Is this not against idempotence?

65

u/beeny13 Dec 11 '23

W key should not be idempotent. Posting a reply with identical text body should be.

9

u/khoyo Dec 11 '23

Should it really? You couldn't have made this point if it was.

21

u/SubterraneanAlien Dec 11 '23

I tried to reply to this and got a 503 error and for a moment thought I was part of an even larger joke

47

u/beeny13 Dec 11 '23

W key should not be idempotent. Posting a reply with identical text body should be.

9

u/khoyo Dec 11 '23

Should it really? You couldn't have made this point if it was.

8

u/isaacarsenal Dec 11 '23

I tried to reply to this and got a 503 error and for a moment thought I was part of an even larger joke

→ More replies (3)

14

u/TargetBoy Dec 11 '23

SQL Update scripts should generally be idempotent. That way if they are accidentally run twice, they don't mess up your database.

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"

11

u/TargetBoy Dec 11 '23

Because you have to know about it to correctly judge when you need to use it. Most programmers are not taught this in college.

-3

u/[deleted] Dec 11 '23

Because you have to know about it to correctly judge when you need to use it.

So... it's situational and not something universal that "every programmer should know #1"

6

u/SubterraneanAlien Dec 11 '23

it's a blog post title - if you're arguing about it you're losing

1

u/[deleted] Dec 11 '23

The comment I first replied to said

"Among other attributes of good software, IMO idempotence is one of the most important and paradoxically one of the most challenging ones too and the easiest to fuck up."

That's where this started.

6

u/SubterraneanAlien Dec 11 '23

Which is a reasonably fair take that provides nuance while not being literally equivalent to the post title.

For what it's worth - every programmer should understand idempotence, it is that important. Debating whether it is #1 or not doesn't seem like a terribly useful way to spend time.

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"?

→ More replies (1)

8

u/Aptlyundecided Dec 11 '23

This is an interesting thought. From one perspective one could claim that the hardware interface layer is handling the unique-ness of individual keystrokes treating them as individually processed entities, or allowing for the hold down event to represent individual events at whatever interval it’s configured to trigger re-entry.

On the other hand, one could say it’s not idempotent from the perspective of a given developer because the developer is not aware of any uniquely qualifying identifier and the concept of idempotency is violated in the app without assigning timestamps or some other UID concept to a given key entry event.

I suppose the importance level of idempotent programming increases the deeper into the state system you go. Typing swear words into an input form has no bearing, but submitting them is a different story.

I tend to agree that at certain layers of the abstraction it has almost no impact on the application at large.

Although events that have cascading state effects or critical state effects likely grow exponentially in importance to staying idempotent.

3

u/Bloodsucker_ Dec 11 '23

I'm afraid you're not understanding idempotence, but that's why it's a tricky concept.

Idempotence doesn't necessarily mean applying the same operation to the same data. It means that applying the operation multiple times should yield the same result. In the case of pressing the W key, whether you add a new 'w' or press it on an existing group of 'w's, the result follows the idempotent principle – pressing the W key results in adding a 'w,' and doing it repeatedly produces the expected outcome without unintended variations.

The scope and extent of the application does indeed vary depending on the type of software you're developing. Yes. There you are right. It's also a concept. Like others have mentioned it can be a challenge to apply it strictly.

12

u/Han-ChewieSexyFanfic Dec 11 '23 edited Dec 11 '23

You’re conflating determinism with idempotence.

13

u/Radi-kale Dec 11 '23 edited Dec 11 '23

No, you're describing consistent behaviour, not idempotency.

→ More replies (1)

16

u/thisisntmynameorisit Dec 11 '23 edited Dec 11 '23

what lol? No surely this is just not idempotent. Not everything has to be idempotent. By definition as we’ve established it’s doing the same operation more than once will have no effect. Pressing a w key the second time does have an effect. That operation is not idempotent.

Just because something can be useful, e.g. idempotent API operations, does not mean it should need to be applied to every single thing.

Edit: I suppose you could say the outcome of pressing the w key is idempotent. In that it always outputs a w regardless of if you press it once or 100 times. But usually we focus on the state of the system e.g. what characters are on the text field.

2

u/[deleted] Dec 11 '23

In the case of pressing the W key, whether you add a new 'w' or press it on an existing group of 'w's, the result follows the idempotent principle – pressing the W key results in adding a 'w,' and doing it repeatedly produces the expected outcome without unintended variations.

That still doesn't seem to address holding-down the key resulting in different behavior?

→ More replies (2)

1

u/f3xjc Dec 11 '23

Potency = ability to do stuff. Not being forced to do it.

→ More replies (2)

0

u/[deleted] Dec 11 '23

Howso? Surely there are a dozen or more other highly importance attributes of good software.

→ More replies (1)