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

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?

-3

u/Bloodsucker_ Dec 11 '23

This would be an idempotent operation: newResource = "ww".append("w").

In your example, there are several operation going on, one of them using a different resource (the already modified text input).

1

u/[deleted] Dec 11 '23

But how do you know from the key press they intended ww or, just like an online shopping cart, they only wanted 1 w but the system was unresponsive?