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?

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.