SuperHanz98
SuperHanz98

Reputation: 2240

Why not use all imports?

I understand that I can import packages and get access to lots of already coded classes that I can use to make my programs. But if they give you access to so many different features, why not just import them all? I understand that there are thousands of imports and I know it is uncommon to do so (I don't know of anyone that does it but maybe i'm wrong) but why don't people just import them all? Would it make the program too slow? Or just be inefficient? I'm just curious. Thanks.

Upvotes: 4

Views: 3903

Answers (3)

user6118404
user6118404

Reputation:

It makes your code unreadable because the person who is reading doesn't see your intentions.ALWAYS REMEMBER: "code is written once, twice maybe more, but hundreds of times read by someone. Example:

In Android SDK there are classes that has the same name but they come from different packages. I think that was GPS location manager or something. Whatever...other programmer could have difficulties, because he needs to think or manually check which import are you using.

I'm not a advanced programmer but I surmise that the output program might be bigger in size.

Upvotes: 1

Ömer An
Ömer An

Reputation: 664

Importing all packages will;

  1. slow down your program as it keeps all the classes, functions, etc. coming from each package alive (=readily accessible)
  2. create conflicts between packages which use the same namespace (i.e. same function name etc.), or at least make the last loaded one usable and make the previous ones masked
  3. take a lot of time every time you restart program
  4. use a lot of memory
  5. be vulnerable to crash for the above reasons

where this list can be extended.

Upvotes: 4

kyryx
kyryx

Reputation: 160

There's a few reasons besides speed I can think of:

  1. Seeing the list of imports can quickly show someone reading the file what is being used. If you import everything, you lose that ability
  2. There will be name clashes which will cause errors. See this question for an example.
  3. Modern IDEs make it really easy to import packages on demand, so there's no need to import everything in advance

Upvotes: 6

Related Questions