Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 722
SuperHanz98 Avatar asked Oct 29 '25 14:10

SuperHanz98


2 Answers

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
like image 155
kyryx Avatar answered Oct 31 '25 05:10

kyryx


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.

like image 41
Ömer An Avatar answered Oct 31 '25 04:10

Ömer An