- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Edit Admin
I would argue that this is a good example of "doesn't work" (especially the "high probability of collisions" part), and yet the article says that it does work...
But then again:
Why did it take until 2021 for JS to sprout a random-UUID function?
Why would we want to generate UUIDs in the browser rather than in the server code? (The WTF-ness of this question may explain why it took so long...)
Admin
The probability of collisions is very low. The built-in random functions are really good enough.
It's just a random id, nothing else. You can't send a 128 bit number in a url or json, so formatting it as a uuid is probably fine.
Nobody cares about the meta data in uuid, unless you mix different types (and I can't think of a reason why you ever would, although there's probably someone here who knows an obscure use case), but this code even takes that into account.
Edit Admin
"Why would we want to generate UUIDs in the browser rather than in the server code?"
Some APIs require that the client generates the id of a newly created resource. It's bad, but at least someone else is to blame for it.
Edit Admin
CQRS
When you have strict query/command separation, you don't really have return value(s) with your write operations. So if you want to create a new resource, you have to provide a unique identifier to find you resource again.
Admin
It is not bad at all when a UUID is used as an idempotency key, as to prevent creating duplicates on network hiccup.
Admin
the "4" in the string is the type of the identifier.
Edit Admin
The proper thing to do is check whether
crypto.randomUUID
exists and use this only as a fallback.