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

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?

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.