2 ms·
What if JavaScript had persistent globals?
- jerf 11y agoI worked in this environment for a good long time. There really is something to be said for "sometable.width = 25" being all it takes to persist something. On the other hand, it was also possible to really trash your environment with one stray for loop. Threading is also pretty hard on top of a hierarchal object store shared by everything. But... well... again, the ease of the moral equivalent of "append(post, blogposts)" being the entire serialization story is pretty nice. It's 2015 and I still spend an awful lot of time opening files, reading from them, and writing from them, and I spend an awful lot of time bending my programs in various ways to fit into SQL. I've still never quite seen anything as slick. Maybe Zope, but I was never able to sit down with the docs long enough to figure out the "hello world" example. Does make me wonder if there might not be some sort of optima we're missing here. Makes me want to see if Rust couldn't support something interesting that would be both safe and easy.
- RyanZAG 11y agoI don't think this would help big projects in the long run - more likely it would hurt. The problem with globals - and why we try to minimize them - is because they can be changed by any code throughout the code base. It makes it hard to know exactly what the global's value will be. Persistent globals takes debugging a step further. We wouldn't be able to know if the globals value is being changed by another page, an earlier version of the script, etc. It would create true 'nightmare bugs'. Glad it didn't get into javascript.
- jerf 11y agoIt's a closer mental model to speak of "trivially-easy persistence" than "globals". In practice the values were as "global" as the values in an SQL database... technically the SQL database is not imposing any sort of visibility constraints on your code, but that doesn't mean that you necessarily think of those values as "globals".
- endergen 11y agoI think the real insight from this type of an approach and from the very popular single state atom architecture is that just having all state in one object makes serialization trivial. Many projects end up doing crazy things like solving serialization in too many ways in the same code base, such as too local storage, REST based serialization/deserialization, and etc. You must take considerations to not have the issues globals typically introduce such as hard to mock testing environments and the usual globals as communication shenanigans, but there are ways to treat each code concerns as it's own relative path object within the larger global object. Personally, I just think having a clean data protocol basically like a database in app space leads to much cleaner architectures, especially once you try to do offline first, data sync, and more advanced features like access models.