简体   繁体   English

为什么不使用所有进口?

[英]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. 谢谢。

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 现代IDE使得按需导入包非常容易,因此无需提前导入所有内容

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 (ie 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. 这个清单可以扩展到哪里。

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. 在Android SDK中,有些名称相同的类,但它们来自不同的包。 I think that was GPS location manager or something. 我认为这是GPS定位经理或其他什么。 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. 我不是一个高级程序员,但我猜测输出程序的大小可能会更大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM