Effect CommunityEC
Effect Community2y ago
23 replies
Kristian Notari

Optimizing Namespaced Code for Better Development Experience and Bundle Size

I repeatedly said here and there on this discord about how much I love namespaces. The thing you do when accessing and structuring your code to be used as Domain.Subdomain.Module.stuff for example.

Since I then had troubles with bundle size for lambdas and got advice to move from esbuild to rollup or webpack, I then started to think how to optimize writing and maintaining namespaced code in a more friendly way both for dx and bundle size, and a bunch of other aspects.

What if every thing you export from a file is a term named with the complete namespace you want that to put in, with some kind of separator (which is pretty much only _ as it's available as identifier in ts/js).

So Domain_Subdomain_Module_stuff instead of Domain.Subdomain.Module.stuff. I know a lot of you (myself included) would be pissed off by reading those undescorse but you get a bunch of useful advantages out of this change, IF and only IF you can always tell the namespace for everything you do in advance (which my suspect is that you really can, or at least get very close, when you work on different abstractions inside a single job/project with potentially multiple repos that share some common stuff).

Some examples of pros:
- better bundle size with any bundler, never need to worry about it again basically (bundl size)
- if you don't know where something is just start typing the thing you're interested in and autocomplete would do therest and tell you where it is (discoverability)
- if you want to add things to other's modules and namespaces, you can easily export something named that way and everything works out correctly without the need to imoprt and reexport classes/objects and mess with them
- you don't really care where you're importing from but what you're importing, so no need to organize everything with index files or special reexports

What do you all think about this?
Was this page helpful?