[HN Gopher] Heard of Idempotency but unsure what it is?
       ___________________________________________________________________
        
       Heard of Idempotency but unsure what it is?
        
       Author : timmoth_j
       Score  : 16 points
       Date   : 2024-06-10 21:10 UTC (1 hours ago)
        
 (HTM) web link (timmoth.github.io)
 (TXT) w3m dump (timmoth.github.io)
        
       | jfengel wrote:
       | I heard of it once and now every time I remember.
        
         | readthenotes1 wrote:
         | I'd make a joke about nullipotency if I could remember what it
         | meant
        
       | maname wrote:
       | IMO these statements contradict themselves:
       | 
       | > Idempotency refers to the ability of an operation to be
       | performed multiple times whilst still yielding the same outcome
       | 
       | and
       | 
       | > The issue with the above code is that if the event is processed
       | twice the EmailService will send a duplicate email.
       | 
       | In other words, the operation (three lines of code) yields the
       | same result (sending an email) after repeated execution, and thus
       | is idempotent isnt it? Or is the point that idempotency is
       | harmful in this case?
        
         | phoe-krk wrote:
         | _> In other words, the operation (three lines of code) yields
         | the same result (sending an email) after repeated execution,
         | and thus is idempotent isnt it?_
         | 
         | The meaning here is: after the operation is performed _at least
         | one time_ , exactly ONE mail is sent. Note the part "at least
         | one time": it can be one, two, or more.
        
           | maname wrote:
           | thanks, that clicks.
           | 
           | "Simply put, we can perform an idempotent operation multiple
           | times without changing the result.
           | 
           | Furthermore, the operation must not cause any side effects
           | after the first successful execution."
           | 
           | Baeldung got it right.
        
         | ReleaseCandidat wrote:
         | Yes, the first statement can be easily misunderstood. It could
         | also mean that the operation `f` does not have side effects.
         | What it actually wanted to express is that `f(x) == f(f(x))`.
        
         | happytoexplain wrote:
         | The world is a system that has state. If two invocations of
         | your function with the same parameters both mutate any part of
         | that state, and nothing else mutated that part in between, then
         | it's not idempotent. In this case, there are now two emails in
         | the recipient's inbox.
         | 
         | I.e. "result" refers to state, not actions. "Send one email
         | saying 'hi'" is an action. "There is one email saying 'hi' in
         | their inbox" is a result.
         | 
         | There is also usually an implicit limiting scope (except in
         | very pure functional contexts). E.g. just because two
         | invocations both produce entries in an activity log somewhere
         | doesn't usually count as "not idempotent".
         | 
         | You can also think of it in terms of declarative vs imperative.
         | 'x = 1' is declarative (and idempotent). 'x += 1' is
         | imperative. YMMV with this use of those terms, though.
         | 
         | To oversimplify, an idempotent function is "safe to spam", e.g.
         | if you're not sure it worked the first time, you can do it
         | again without worrying about duplication. A very relevant
         | concept in networked communication.
        
           | PaulDavisThe1st wrote:
           | > If two invocations of your function with the same
           | parameters both mutate any part of that state, and nothing
           | else mutated that part in between, then it's not idempotent.
           | 
           | Still not accurate. What matters is whether N calls to the
           | function changes the state differently than a single call. If
           | not, then the function is idempotent.
           | 
           | Another issue with idempotency is that there's an implicit
           | temporal component that is rarely explicitly discussed. Most
           | people don't expect a function to _necessarily_ be idempotent
           | over long periods of time.
        
       | phoe-krk wrote:
       | Idempotency: the ability to be able to hit Ctrl+C several times
       | in a row, just to be sure.
        
       | codeulike wrote:
       | If I've told you once, I've told you a thousand times
        
         | readthenotes1 wrote:
         | That's more nullipotency
        
       | TheAlchemist wrote:
       | The example is not really great, but Idempotency is a very
       | important concept for everything 'data'.
       | 
       | You very much want to be able to run the same pipeline several
       | times and get the same result, or overwrite the results if you
       | changed the inputs. You don't want to wonder if somewhere down
       | the line, there will be duplicated data that will corrupt results
       | for processes that consumes this data - you just want to be able
       | to re-run your pipeline.
       | 
       | Sadly, that's not how many systems seem to be designed.
        
       | o11c wrote:
       | This fails to address that fundamental confusion, which is that
       | there are several different (but related) things all called
       | "idempotent":
       | 
       | 1. a value `x` such that, for some pure binary function `f`
       | (specified by context), `x === f(x, x)`
       | 
       | 2. a pure binary function `f` such that, for all values `x`, `x
       | === f(x, x)`
       | 
       | 3. a pure unary function `f` such that, for all values `x`, `f(x)
       | === f(f(x))`. Often this is formed as a partial evaluation of a
       | binary function, as a relaxation of the previous kind.
       | 
       | 4. a (non-pure) procedure that, when called with "the same"
       | arguments (FSVO same), has no side-effects after the first time.
       | 
       | 5. a binary relation `R` such that, for all values `x, y, z`,
       | `R(x, z) <-> R(x, y) AND R(y, z)` ??? (or is this just "some y"?)
        
         | dataflow wrote:
         | I'm pretty lost, I thought idemopotent was basically a synonym
         | for a projection (which sounds like your #3)... do you have any
         | links to literature using it differently?
        
           | GrantMoyer wrote:
           | https://en.wikipedia.org/wiki/Idempotence#Definition
        
             | dataflow wrote:
             | That seems consistent with #3, I don't see how it's
             | different. Operator or function, the name doesn't change
             | anything?
        
               | o11c wrote:
               | Operators and functions are effectively the same thing,
               | yeah.
               | 
               | In CS we normally mean #4 though ... which can
               | technically be thought of as a variation of #3
               | passing/returning "the whole universe" as an argument
               | (and including the actual arguments in the implicit
               | lambda capture), but thinking like that only makes sense
               | for functional programmers.
        
               | GrantMoyer wrote:
               | The wikipedia definition is directly equivalent to #1.
               | Try rewriting it with f(a, b) = a [?] b.
               | 
               | That's not to say #3 and #1 are unrelated, though.
        
         | GrantMoyer wrote:
         | Definition 3 is the same as definition 1. Defn. 3 is
         | idempotence (in the sense of defn. 1) of the function f under
         | function composition. In other words, f [?] f = f.
        
       | lxgr wrote:
       | I've heard of idempotency before, so this won't change my opinion
       | of it.
        
       | jiggawatts wrote:
       | My mental model is to replace "do" words with "ensure".
       | 
       | Instead of "send notification message", it is "ensure recipient
       | is notified."
        
       ___________________________________________________________________
       (page generated 2024-06-10 23:00 UTC)