markmandel / brute

A simple and lightweight Entity Component System library for writing games with Clojure and ClojureScript.
Eclipse Public License 1.0
181 stars 6 forks source link

Incrementing IDs instead of UUIDs? #3

Closed Janiczek closed 10 years ago

Janiczek commented 10 years ago

What's the reason of using random UUIDs for entities instead of, for example, integers (as you'd see in SQL databases)? Is your only concern guaranteeing that your ID-generating function never repeats itself?

Considering integers:

I think I understand the reason for the use of UUIDs now: you can create new one without knowing the last one. Is that correct? Or is the reason different?

markmandel commented 10 years ago

Pretty much.

Honestly, I didn't think about it a great deal - I just went 'I need a unique id' and so I went with UUID as it was the least amount of work without having to worry about anything else.

Also, if you end up sending the id across a network (say) or another system, you don't have to worry about collisions (or you don't to such a small extent that it's basically the same). For the same reason I've used UUIDs in databases as well. Whereas incrementing IDs can easily collide across systems.

Janiczek commented 10 years ago

True. So the UUIDs guarantee "never" returning the same ID, across different systems, without any knowledge of what IDs are already generated. With integers the machinery would be much trickier.


BTW, I've tried using Brute in a ClojureScript project - basically the only two things that needed rewrite were:

(defn create-uuid []
  ;; http://stackoverflow.com/a/2117523/403702
  (let [template "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
        f #(let [r (Math/floor (* (rand) 16))
                 v (if (= % \x) r (bit-or (bit-and r 0x3) 0x8))]
             (.toString v 16))]
    (.replace template (js/RegExp. "[xy]" "g") f)))

and using (Math/floor ...) instead of the math.numeric-tower one, and using type instead of class.

Maybe "Brute-JS" can live in its own repo, but the number of changes is so small that I think it could be in the main repo, thanks to cljx. Would you be interested in that (a pull request, in a separated issue, of course), or do you have your own plans with JS version? (DataScript or something)

markmandel commented 10 years ago

I've no plans for a Clojurescript version at this time - I'd be more than happy to have a pull request. Something like cljx sounds like a perfect fit.

The interesting problem would be running the Midje tests in Clojurescript. Maybe something like Midje-lite could be used to bridge the gap.

I've been meaning to set up a wercker test environment for a while now, so this would give me the impetus.

Janiczek commented 10 years ago

I'll try to figure the tests and cljx out and create a pull request then :)