My 2 cents: it’s way better to keep a flat structure with flat names like UsersActions, DialogsAction etc.
Main reason I started to like this: it’s really easier to discover an entire project to contributors (which could be yourself a few month later).
I don’t see any cons.
I have folder with hundreds of modules and in practise it’s not a problem.
In comparison, I recently had to contribute to a project that were abusing the number of folder and it was a mess to discover & understand the global modules & topics of the project.
These are two extremes. I think that true is around middle. Need keep balance between hierarchy-hell and flat structure. I don’t like “Hungarian notation”, and your way look like the notation.
If I have a lot of files with one namespace then I will repeat prefix too many times. Also, I can’t be sure that my code won’t have conflicts with outside libraries in the future.
Your decision is ok for small/middle-size projects.
I’d just use submodules. In the users module, create a submodule called Actions. If you want it separate, give it a local name and rebind it. The module name space is flat.
Some variants of Standard ML has a beautiful way to circumvent this through the use of ML Basis files. They allow you rename-control on import/export to avoid these kinds of trouble.
Users.res
Users_Actions.res
Dialog.res
Dialog_Actions.res
etc.
Group them in subfolders if you want… they don’t have any impact on the module names. It’s pretty liberating to know that you can just rename directories / move files around without breaking things.
In JS you would basically do the same thing, but with import paths and pretty noisy index.js files. No matter how you do it, at some point you need to contextualize your module names. Might as well be explicit with fully qualified, unique file names.
If you worry about long component names e.g. in JSX expressions, you can alias modules at the usage site as well (as @jlouis already pointed out):
module Button = My_Super_Cool_Users_Button
@react.component
let make = () => {
<div>
<Button />
</div>
}
When this happens you can quickly rename modules that cause any collisions (oftentimes with a single global regex replace), or vendor the library with a different module name in your codebase.
I’d argue that this convention is okay for any codebase size (even at Facebook scale). It feels weird at first, but you’ll quickly get used to it.
This namespacing mechanism is generally terrible: Your filenames look even more convoluted, you introduce a bunch of files that literally do nothing than forwarding modules as submodules (which often make goto-definition functionality useless), and all the leaky details show in the autocompletion as well:
Might as well do no namespacing at all tbh. Usually when I spot this pattern, I copy paste the externals I am interested in and roll my own bindings instead.
In theory, this could be solved by the language server? If we decide that namespacing is useful, that is. Which it kinda is, because you do need to organize your code to tame the complexity. Human brain has its limitations.
Erlang is a flat namespace, and you can’t have modules in modules. Rescript allows for modules in modules, but the limitation of one-module-per-file comes from OCaml, and I generally regard that as a language design mistake. You should not bind your module structure to the file system, because people might stream your code over another medium than a file system! You shouldn’t bind the name of the module to the name of the file either, but it’s so common in many languages.
In general, I agree one should allow for flexibility here. As a data point consider CWEB/Weave/tangle from literate programming. There, we have a system built on top of our code, which can circumvent the limitations of the underlying system.
Namespaces can be emulated with modules. For example, we had module
/// Buttons.res
module Ok = Buttons__Ok
module Cancel = Buttons__Cancel
module Back = Buttons__Back
It allowed us to write <Button.Ok />, which is familiar. But downsides are:
You have to maintain barrel module
If you add another button or change api of existing one, rescript will recompile all modules dependent on Buttons, it could easily be half of the project.
At some point big rebuilds got disturbing enough to stop this practice, and use <Buttons__Ok /> directly.
But they’re not restricted to that. You can put more items in there. E.g. we have a few things in Webapi module itself, and similarly in other index modules. Yes, the file names look ugly and autocomplete gets more annoying (although you could also show the dot-completion i.e. Webapi.), but in exchange you have pretty complete control over namespacing in the project.
Of course, this should be done only when the project is large, we don’t want to see lots of little files with a few lines of code each scattered throughout many subdirectories.
I agree with you. You can have one bundle file with modules inside.
I think that imports - that’s good. I see what use this code. I see where is it. I can change module by change import path after refactoring.
And I have will have pain with name collisions.
Autocomplete Webapi__Dom__Attr - looks ugly, sorry.
If my project is big, my IDE will be a broken autocomplete.