This is still the header! Main site

Idempotence

2022/01/22
This is post no. 70 for Kev Quirk's #100DaysToOffload challenge. The point is to write many things, not to write good ones. Please adjust quality expectations accordingly :)

Yes it's a math thing. It's a fairly general concept though:

an operation is idempotent if doing it multiple times has the same result of doing it just once.

This is a really nice property to have.

Just some examples:

"fill up the water kettle" is idempotent; the first time you do this, you fill it up, then you don't have to do anything for the followups (since it's full already).

"add a given amount of water to the water kettle" is not idempotent; if you keep repeating it, it eventually fills up, overflows, and there will be water all around your kitchen if you did this in a suboptimal location.

It's the difference between running a random Bash script that sets up something on your system and dies if it's installed already, and Ansible, where you can run the same playbook multiple times.

If your operations are idempotent, you don't have to remember what state you were already in. If you can just clear the screen and draw the things you want from scratch, the screen contents before are irrelevant; on the other hand, if your API looks like "add a button" and "remove a button", you need to figure out which ones to remove even if you know exactly what you want on screen.

It's not even just programming

User interfaces work the same way!

Take the difference between Alt-Tab and specific hotkeys for virtual desktops (I have them set to Win-1 through Win-0 on i3wm).

Pressing Alt-Tab cycles through windows. You keep doing it until you get to the window you want. Of course, what window is next depends on which one was the previous one (along with switching history); you can generally guess what will happen with two or three recent windows, but it gets tedious fairly soon after; eventually, it's just "hit Tab one more time, check whether it's the right window, if not, keep going".

Virtual desktop switching, on the other hand, is idempotent. You can switch to a given desktop even if you were already there; it's just nothing happens. If you place your windows right, you don't have to remember anything about switching history; one hotkey gets you right where you want to be.

Meanwhile... I'm fairly convinced that there is something deeper here about state spaces, which we can connect up with why Emacs is so nice with all its commands you don't have to have feedback loops for. It wouldn't be #100DaysToOffload without posts that end abruptly though.

... comments welcome, either in email or on the (eventual) Mastodon post on Fosstodon.