Add more information about #remove method#273
Add more information about #remove method#273Bartuz wants to merge 4 commits intojs-cookie:masterfrom
Conversation
It took me few minutes to find it on my own. Hope to save time of other developers! Just a useful info about remove method :)
| Cookies.remove('name'); | ||
| ``` | ||
|
|
||
| Please notice that removing unexisting cookie does not raise any exception nor return any value |
There was a problem hiding this comment.
That doesn't mean the cookie doesn't exist, that means the cookie is not accessible. It is impossible to verify if the cookie exist or not.
We have discussed this in the past, see carhartl/jquery-cookie#164 (comment).
| ```javascript | ||
| var whatWasTheValue = Cookies.remove('nothing'); | ||
| if (whatWasTheValue === undefined) { | ||
| console.log("I will be excuted"); |
| Please notice that removing unexisting cookie does not raise any exception nor return any value | ||
|
|
||
| ```javascript | ||
| var whatWasTheValue = Cookies.remove('nothing'); |
There was a problem hiding this comment.
We can name the variable consistently throughout the docs
| if (whatWasTheValue === undefined) { | ||
| console.log("I will be excuted"); | ||
| } else { | ||
| console.log("I will never be executed"); |
There was a problem hiding this comment.
Maybe a comment instead of console.log?
There was a problem hiding this comment.
I used console.log instead of comments because I wanted to emphasize that this line will be executed (i.e. there isn't unhandled exception which stops javascript thread [tbh I don't know if it happens in javascript but I'm sure some other Javascript newbies are also using this library :) IMHO it's not a big deal to make a bit longer code snipper but make it understable by broader audience])
There was a problem hiding this comment.
I mainly started this #remove research because comment here:
Cookies.remove('name'); // fail!
didn't make me sure enough if fail means exception, undefined or something else.
In Ruby fails means throw exception, so I needed to double check this one 😆
There was a problem hiding this comment.
Makes total sense your // fail! example.
Would this work then:
Cookies.remove('name'); // doesn't work
There was a problem hiding this comment.
Or maybe:
Cookies.remove('name'); // doesn't do anything
Or:
Cookies.remove('name'); // silently fails
Or:
Cookies.remove('name'); // noop
The last one can have accessibility problems because not everyone knows what the noop concept means.
The reason why I am suggesting those approaches is because it might be interesting to focus on the least amount of text that fixes any confusion. The README is in a state where any new addition will increase the friction because there will be more content, so it might be worth putting some effort to not add new paragraphs unless essential.
Since you just started with the library you don't have the maintainers bias on the README because we already know how the API works and we can't make the docs better for that reason. Your feedback is really really valuable! Thanks for helping out!
There was a problem hiding this comment.
It's really nice you appreciate my voice here, thanks :)
I like your approach to keep guides as short as possible but IMHO your new comments may confuse people even more than fail.
The problem is that current fail comment is used for the first time in context of "valid path of current page" so even adding super descriptive comment there is not intuitive enough to communicate that even removing Cookie with proper "path" argument also "fails"
I would just add block that every .remove call ends up with undefined, successful or not
Cookies.remove('name'); // returns undefined
Cookies.remove('nothing'); // also returns undefined
There was a problem hiding this comment.
I understand. What about something like this then:
Delete a cookie that is visible on the current page:
Cookies.set('name', 'value', { path: '' });
Cookies.remove('name'); // doesn't remove the cookie
Cookies.remove('name', { path: '' }); // removes the cookie
We can then consistently use "visible" instead of "valid" where we are talking about the cookie visibility based on the attributes, replacing throughout the docs.
Unfortunately, we can't document that Cookies.remove() returns undefined because that's not a supported behavior. It is actually void and returns nothing.
What do you think?
|
Maybe we can have a generic documentation saying that it will never throw an exception except for the cases it will (like the |
It seems my previous comment was wrong. We don't throw any exception when Actually, it looks like we never throw any exception for documented functionality, maybe we can make that clear in the docs instead of just documenting a single API? @Bartuz Do you still want to work on this? |
|
Ping @Bartuz, do you still want to work on this PR? |
Due to the lack of feedback from the OP I'll land with a few modifications
It took me few minutes to find it on my own. Hope to save time of other developers!
Just a useful info about remove method :)