• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why PUT is idempotent ?

 
Himai Minh
Ranch Hand
Posts: 1360
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to these references :
http://stackoverflow.com/questions/19673720/difference-between-the-servlet-http-post-and-put-method and
http://www.pearsonitcertification.com/articles/article.aspx?p=30082&seqNum=3 :

PUT is for uploading a file or modifying a resource if it exists;otherwise , create the resource.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well if you put a file once or a thousand times, it won't make any difference so f(f(x)) = f(x) I guess...
 
Himai Minh
Ranch Hand
Posts: 1360
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, AJ,
Think about modifying a resource if it exists; otherwise create it.
For the first time, a resource is created.
For the second time, the resource is modified.
For the third time, the resource is modified and so on.

Does this scenario has a side-effect ?
I think the side-effect is in the first time, I get a new resource created even if I don't want it.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether the resource is created or not is irrelevant; the response will be the same regardless.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himai Minh wrote:Hi, AJ,
Think about modifying a resource if it exists; otherwise create it.
For the first time, a resource is created.
For the second time, the resource is modified.
For the third time, the resource is modified and so on.

Does this scenario has a side-effect ?
I think the side-effect is in the first time, I get a new resource created even if I don't want it.


I don't think idempotent is related to side effects. The result will always be the same; the file will be there, we don't care if it was there or not before..

Here is the Wikipedia definition:
" In the case of methods or subroutine calls with side effects, for instance, it means that the modified state remains the same after the first call"

Computer science meaning
See also: Referential transparency (computer science), Reentrant (subroutine) and Stable sort

In computer science, the term idempotent is used more comprehensively to describe an operation that will produce the same results if executed once or multiple times.[7] This may have a different meaning depending on the context in which it is applied. In the case of methods or subroutine calls with side effects, for instance, it means that the modified state remains the same after the first call. In functional programming, though, an idempotent function is one that has the property f(f(x)) = f(x) for any value x.[8]

This is a very useful property in many situations, as it means that an operation can be repeated or retried as often as necessary without causing unintended effects. With non-idempotent operations, the algorithm may have to keep track of whether the operation was already performed or not.
 
Himai Minh
Ranch Hand
Posts: 1360
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, everyone,
I don't think idempotent is related to side effects.


But on p.116 of Head First Servlet, it says
There are several different uses of the word idempotent; we're using it in the Http/servlet way to mean that the same request
can be made twice with no negative consequences on the server. We do not use idempotent to mean that the same request always
returns the same response and we do not mean that a request has no side effect.


From the last phrase "we do not mean that a request has no side effect." It means "we mean that a request has side effect."
 
Himai Minh
Ranch Hand
Posts: 1360
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why. Why I tried running the following code with Eclipse, it works. The headers are not null.

The HTTP Monitor of Eclipse shows this:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
foo: foo bar
Content-Type: text/html
Content-Length: 0
Date: Wed, 09 Sep 2015 22:40:34 GMT
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic