























I do a lot of architecture work lately, and one thing that seems to appear more often than I thought it would be are module names that have generic-sounding names like shared, util, or core. There is something confusing about them, as they don’t tell you what’s inside!
I like to call them Schroedinger’s module names. You only know what’s inside when you take a look!
While this is something I found lately in JavaScript applications, this is of course not exclusive to JavaScript. Take other programming languages for example. Where would you expect to find a hash map? In Rust, it’s HashMap in std::collections. In .NET you get a Hashtable in System.Collections. That’s good. Hash maps are collections after all.
In Java? It’s java.util. How am I supposed to know to find that in there?
Folders (or modules) like this usually happen when there’s functionality in your application that’s so nice it might be useful for others, too:
Types that are used by both your front-end and your API. Some nice transformation function that takes an object and makes it compatible with your views. Maybe some components that take care of the typical loading process — what a great utility!
The problem is that if you put them in modules that can contain everything, not only do you occlude your module’s purpose, you also create a dumping ground for future functionality!
You can equally call the folders stuff and they would say the same thing.
Naming things is hard, and finding the purpose of your code can also show some architectural inadequacies: If there’s no right place, maybe the architecture didn’t account for what’s coming, or it makes it super hard to introduce things.
But that doesn’t need to be. For the examples above, it’s not so hard to find something more speaking:
There is always a better place to store your functionality.
You can make avoiding module names like this a rule. And as with any rule, there are exceptions to it. Take the templating system Handlebars for example. In Handlebars, you can extend templating functionality using so-called helpers. That’s a name I usually want to avoid, but in the context of Handlebars, it’s pretty clear what to expect. This is where the architecture makes it unmistakably clear what the purpose of your helpers is, even though you have to learn Handlebar’s domain.
But other than that? Take it by their initials: shared, util, core? They suc! [sic]
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。