Namespaces and names in javascript
document.getElementById
has been bothering me for a while, and I believe I’m finally able to say why. It’s the naming.
In practice, I have to type lots of letters. That has a big, practical reason: document.getElementById
lives in the global namespace! It has to have a long name that says all the things it does because it doesn’t live in a namespace with a tight purpose.
With namespaces, it could easily have been named query, or just q if you will. Yeah, I know we have document.querySelector
and document.querySelectorAll
, but they suffer from the same problem! Multiple solutions for different aspects of the same problem, and each has to have a super long name because the name isn’t namespace qualified.
With namespaces and a module system we could even work on improving those namespaces over time. Imagine how nice that would be! For instance, datascript.core
will not delete its names. And if some revolutionary new knowledge that changes what we know about how EAV querying should be done, datascript.core2
or datascript.advancedquery
could be introduced!
This is probably old news to lots of you. I’ve just had this itch for such a long time, and now I’m finally able to say what I didn’t like.
Namespaces, names, operations and entities in software design
I’ve come to approach software design as the creation of a cohesive set of operations and entities, named in a namespace. This is how I approach all code I write now. Though in certain languages, I feel like this is made harder.
I don’t think I would have come to learn this lesson if I hadn’t started digging into the Clojure rabbit hole. I’ve learned a lot from how Clojure is designed, from Zach Tellman’s Elements of Clojure, and more recently reading Clerk source code.
Having a well-designed language with well-designed namespaces and good names at the bottom is such a superpower. Both for using the provided names, and for learning to design new namespaces, names, operations and entities.
For a while, I put way too many “loose ends” into namespaces. Because everything is data, my “entities” weren’t clear from the code. Slowing down helped.
Does this make sense? Do you agree?
I’ve been pondering about this for a while. I’m posting here on Clojureverse because I’d love to discuss this, and I’ve reallly appreciated previous discussions on Clojureverse. I was half-way through writing this on the Clojurians slack in #off-topic
when I realized that Clojureverse, with better support for long-form discussion and and searching would probably be a better place.
Let me know what you all think,
—Teodor