0

Why is there a such thing as import in programming languages? If a package does not exist, then trying to import it would cause an error anyway. So why don't languages just auto import ALL available packages? Instead of requiring programmers to have 'redundant' import statements?

6
  • 4
    Can you clarify what language you are talking about and what, exactly, you mean by "import"? Of the three languages I am using on a regular basis, one doesn't have anything named "import" and the other two both have something named "import", but they do completely different things. Commented Apr 23, 2023 at 21:00
  • 1
    @JörgWMittag Python, JavaScript, C, C++, etc. Basically all languages. But not Java because Java import is different. Commented Apr 23, 2023 at 21:16
  • 8
    The set of functionality available to mature languages is massive. Import by default would mean that even the tiniest program starts out with a hundred name clash errors before your own code is even looked at. Commented Apr 23, 2023 at 21:20
  • @user1345541 it's useful to remember that import is really just an abstraction of a concept. While each language might have their own specific implementation for how import happens, it is, always, a manner by which we tell our executing platform what references/hooks need to be managed into other modules. Yes, Java does it differently, but technically? They all do it differently whether a little or a lot. Commented Apr 23, 2023 at 23:55
  • 3
    @user1345541: if you want to give your question a chance to get reopened and get better answers than the mediocre accepted one, please do what Jörg W. Mittag asked for, edit it and tell us which programming language you have in mind. Python? Java? Javascript? Something else? Currently, your question is way-too-broad.
    – Doc Brown
    Commented Apr 24, 2023 at 6:33

5 Answers 5

3

As a software developer, I don't want my compiler to import anything unless I tell it to. Just because a package is "available", doesn't mean I trust it. And I certainly want to be able to download a package and try it out in a test project, without it automatically being included in all my code.

The risk of such a "feature" very much outweighs the slight benefit.

PS. Consider that different developers have different packages installed. Worst case you make a change that builds and runs just fine on your machine but not on mine. That’s fun.

4
  • 1
    I don't buy this. Presumably anything you installed on your computer is trusted. Commented Apr 24, 2023 at 13:19
  • 5
    @user253751 assumptions are the seed of evil. You are assuming you have control over everything that is installed on your computer. The truth is that you are, probably, only aware or in control of an insignificant part of it. Just think of transitive dependencies. Dependencies you might want to exclude or overwrite yourself due to bugs. Not everyone can wait for official bug fixes. Some might not have even a bug fix ever (due to EOL)
    – Laiv
    Commented Apr 24, 2023 at 13:42
  • 3
    @user253751 On the computer I am typing this with, I have 5 different versions of the same library, in a shared location. It would be incorrect to import them all, and I don't want a tool to automatically pick, because the correct answer is different for different projects.
    – Caleth
    Commented Apr 24, 2023 at 15:31
  • 1
    My previous job, we had one guy responsible for trying and verifying new versions of third party libraries. NOTHING on his computer was trusted, that was the whole point of it. (These were packages that were blindly downloaded and used by many developers at other companies, but we had rather high security requirements).
    – gnasher729
    Commented Apr 27, 2023 at 17:27
3

'import' allows to you select just those features you need from other packages, libraries and/or modules in your application.

Since a package is often a whole set of files and/or binaries and resources used to encapsulate or abstract some set of functions/behaviors, in order for your application to use those features, they need to be available in someway at runtime. If you tried to import all in a compiled language like C, you would be including every DLL, compiled module, and file from all of those libraries and this would dramatically increase not just the size of the application, but also the startup time, since every library would need to be processed. By importing only some parts, we can not only selective choose which modules are included, but also which versions of those modules we want to include.

2
  • 3
    In the C case, the linker should remove unused code. But also in the C case, there are bigger issues with polluting the global namespace, #defines and the like... Commented Apr 23, 2023 at 20:50
  • 1
    Sure, but I don't think any of that is relevant for the purposes of immediate explanation at this level of dialog. Commented Apr 23, 2023 at 20:55
1

The main reason is code organization and readability. Almost all programming languages have some idea of namespaces or modules - something that allows name reuse within a context.

By default this would have you write code like:

com.example.foo.bar.Quuz myQuuz = new com.example.foo.bar.Quuz();

Which is valid, but entirely too long. So the language allows you to write

import com.example.foo.bar.Quuz;
// ...
Quuz myQuuz = new Quuz();

As a short-hand. I've worked with multiple packages containing the same class which forced me to adopt a mixed style to disambiguate the classes.

import com.example.foo.bar.Quuz;
// ...
org.example.lib.Quuz input = retrieveQuuz();
Quuz myQuuz = new Quuz();
myQuuz.setBar(input.getBar());

A single global namespace would result in every addressable object requiring a unique name. The result would either be very long names (like the one above but for the entire codebase) or else inscrutable ones because their naming would start to resemble an alphanumeric soup.

0

Here are some advantages:

  • Predictability. Today foo = 5; print(ffo) is a bug due to a variable being undefined due to a typo, but tomorrow it imports the Foreign Function Object library and prints it.
  • Also, someone reading the code without an IDE for that language can see that ffo is the name of a library they need to install. This is a reason why it's good to declare everything or at least limit the scope of things that do not need to be declared. I frequently rely on this when browsing Java source on GitHub.
  • Compiler speed. The compiler only has to look at the files you ask it to, not everything you type in your program that could possibly be a filename.

The last reason is probably the direct one.

1
  • I did not downvote. The compiling speed should not be affected. An import is used to identify where a method or property belongs when it can't be located by the compiler. Unless the name to be resolved appears in too many libraries, in which case you would get a redundant reference error still. In short compilation speed is probably not the reason.
    – NoChance
    Commented Apr 25, 2023 at 1:52
-3

Think about how long compilation (or starting the interpreter or whatever) would take if I have 1000s of packages installed.

1
  • 3
    I suppose it could use lazy import, i.e. import on demand.
    – Erik Eidt
    Commented Apr 23, 2023 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.