3 ms·
> In C, functions are supposed to be idempotent, (without side effects). What is meant by that statement? Is "idempotent" a mistype/mistakenly selected word? I
by zbentley 8y ago
> In C, functions are supposed to be idempotent, (without side effects).
What is meant by that statement? Is "idempotent" a mistype/mistakenly selected word? If the author meant "pure" instead, should I file a PR, or consider this some sort of meta-meta-obfuscation? If the author meant "pure", is that actually true of C specifically, or just generally good programming advice (localize side effects to known locations, the fewer the better, don't sprinkle them all over and document where they occur)?
- badlukk 8y agoC functions shouldn't change behavior based on state, they do the same thing every time. Instead of changing a particular global variable, any variable of that type should be passed in and changed, then returned. It's good programming advice for functional languages. In OO it makes sense to make state changes in object methods, but not usually the best functional approach.
- marcosdumay 8y agoI think the author meant something like "they are supposed to be pure, so make them not idempotent".
- jokerx 8y agoCode tells "how." Comments tell "why."
- rofo1 8y agoIdempotent is concept borrowed from mathematics. It just means: f(f(x))=x assuming f is a function (binary operators are just functions (maps) in mathematics, too, e.g. +:NxN->N (read: "plus" is a map from the Cartesian product of N by N to N))