If you require user code to explicitly import functions from modules, you can safely add a function to a module or to the standard library without risking to break someone's code.
Importing the module is not sufficient: really, the functions (or any other imported objects) themselves need to be named. (or, the function needs to be qualified with the name of the module; actually, Python seems to do a reasonable job at this)
As a text editor user, it also helps me discover where such or such function is defined. The alternatives are:
- using an IDE that will consume a lot of CPU cycles and memory to resolve the functions
- grepping, with the risk of finding another function with the same name so I would also need to check if the type signature matches.
I'll gladly spend the time to write my import statement to solve all these issues. It does not bother me. It's not where I spend my time when I program.
It's okay if IDEs hide those import statements or produce them automatically (and it does not mean it's the wrong approach). The point is: code should not break (compilation or runtime) if a module or the standard library evolves and add stuff that could have otherwise clashed. The work being done automatically and the result being hidden by default does not mean it can be done the same way in the future and therefore should be avoided; that's because the environment can change, so it has to be done at the time the code is written, manually or automatically.
> It seems to me one of the worst problems explicit imports help solve is forward compatibility of code with respect to the modules and the standard library. And this problem seems worth solving.
This is exactly the problem in a lot of legacy Common Lisp code in which packages :USE other packages. :USE means that all symbols from package A become accessible in package B.
It also means that a widely :USEd package cannot add a new symbol without possibly breaking a lot of code that depends on it. In the optimistic case, nothing will happen; in the realistic case, you will get package conflicts if the symbol that is newly exported from A is also exported by another package C that is :USEd; in the worst case, the :USEing package successfully grabs that symbol overwrites its meaning with something else, which means that the functions or variables or classes or whatever else named by that symbol suddenly become something else and everyone else that :USEs package A suddenly needs to deal with an overwritten definition, and chaos ensues.
One CL solution is to never :USE other people's packages and explicitly import single symbols; another solution is to use local nicknames which means that it's possible to "import org.random.foo as f" and refer to symbols via F:BAR.
All exported symbols from package A become accessible in package B.
Also, symbols that package A itself uses from another package are not visible by using package A.
You can create an intermediate package to isolate yourself:
(defpackage foo-2019
(:use foo)
(:export this that other thing))
Now you can ":use foo-2019" and get only those four symbols from foo. The :export actually imports those symbols into foo-2019, and then marks them for export. The :use foo makes all of foo's exported symbols symbols visible in foo-2019, and this allows the :export clause to refer to them.Well, in Python you can do it well or poorly at your own discretion.
Good:
from module import fun
...
fun()
import module as m
...
m.fun()
Bad: from module import *
...
fun()if modules are always addressed by their full path then adding new functions to a module is not a problem.
I guess it is a matter of taste.
in the few cases where you have one or two verbose function calls that are called many times you can always assign them to some short variable and solve the problem that way.
i don't see the path checking issue. module paths form an easily recognizable pattern that doesn't increase my effort to read the code. you'd have to have some very similar named modules for that to be a problem.
Nowadays many people are letting their IDE do 90% of import statement maintenance, and then folding the lines out of the way so they don't have to bother reading them.
That suggests to me that having explicit imports at the top of every file isn't the best representation of the information they contain.
As far as naming is concerned, maybe it would be better to share a description of the project's naming rules in some larger scope, and let individual files override that.
In languages where imports are doing work for dependency resolution as well as naming, maybe it would be more readable to have this information in one place than scattered around many files.
The users don’t need to know the specific imports 90% of the time, but it really helps the compiler. So it’s a sort of metadata for the code, something we don’t really have. Usually, the same code is seen by both developer and compiler.
(It may also help incoming developers, but personally i think it’s negligible. Now they just have module+identifier instead of identifier. The real help comes from “view symbol documentation” “jump to source” which is there regardless).
Gilad Bracha had some interesting posts that talk about this pattern: https://gbracha.blogspot.com/2009/06/ban-on-imports.html?m=1
This particular benefit is almost completely destroyed by DI frameworks that use annotations and other magic to automatically wire together a codebase. So, I just don’t use those.
And when I'm using a DI framework, the only injection point I'm happy with.
But, having worked on a large codebase that used constructors with no DI framework, it makes adding a new dependency used in multiple bottom nodes of the graph very painful.
I hate Guice.
My point actually being that if folks won't accept wildcards to begin with, I can't imagine importlessness gaining any traction, although perhaps we'll bifurcate as happened with the extremes of checked-exceptions-everywhere vs. never-exceptions.
> are you sure you want to use com.do.not.use.this.is.a.copy.collection ?
Static analysis-wise (which is what most of the blog post's about), things are basically the same. The tradeoffs are mostly just in terms of readability. I used to be ambivalent about this myself, but looking at the growing body of GitHub TypeScript code with mountains of imports automated by VSCode, imo we've landed on a decent enough design space.
I guess it depends on the depth of your paths, but I'm unsure what it solves, it just moves the verbosity to call sites.
It just gets out of the way. Verbosity is easy to prevent with "open" statements. The nice thing about those is that you can put them in a local scope where you use them (which is generally recommended).
It's just a lot more flexible and a lot less hassle.
But in a language like JS where the module cannot span multiple files one quickly runs out of good short names. Then importing individual symbols or at least aliasing the module becomes a necessity.
what is wrong with using the full module name? i find it makes the code more readable.
2. bad search & code completion
when i don't know what function i need, then i need to search the whole library anyways. if i address modules by their full path then code completion will only need to complete on the top level namespace (the list of modules) and then drill down. a clever IDE can track what i already used and offer that first.
3. compiling more than necessary
the compiler only needs to track what functions are actually being called and only compile those or the modules they are in.
4. dependencies are not obvious
an ide can track which modules are used if i use the full path to address them.
without an IDE i'll have to read the code. ok. the long module names stand out, but granted, grepping for import is easier.
the pike programming language works without import. modules are always called by their full path, so the parser and compiler can find and resolve them and compile those that are needed.
pike does have an import statement. it loads all the symbols in a module into the local namespace. it is the equivalent to pythons "from foo import *"
i am not sure about this but my understanding is that otherwise only the symbols that are actually used are being loaded. import is a mere convenience for the coder but potentially causes more memory to be used and may make local namespace searches slower so it's use is not recommended. (not that this would be significant unless you import hundreds of modules)
I even added imports and modules to D's embedded ImportC C compiler.
Namespaces is pretty much what this article ends up landing on also, if I understand. There is no material difference between `\App\MyClass::myFunc()` or putting at the top of your file, `use App\MyClass` then latter, `myFunc()`
Autoloading: https://www.php-fig.org/psr/psr-4/
Way more deterministic and predictable than Rails autoloading, which always seems to devolve into a crazy mess. And you knew exactly where a constant was located on disk just by eyeballing the name.
PHP makes it pretty easy to have multiple custom autoloaders too, which I've used to help bring legacy projects into modern times before. Even inside older frameworks with no awareness of classes, you can register an autoloader and start using the classes and namespaces in your module/plugin. When PHP runtime detects an unknown class, it will go through every autoloader function registered with spl_autoload_register.
Unfortunate and a bit bummed out that nothing relevant currently turns up in google:
It is only possible when 1) you have very simple data structure. line of text in this case. 2) each command has a lot of options, thus reduces the number of commands.
Ofcause it would be alright to not import anything in a module meaning that it would have no external dependencies.