简体   繁体   English

优化进口的原因是什么?

[英]What are the reasons to optimize imports?

Are there any technical reasons to "optimize imports" via your editor? 是否有任何技术原因通过编辑器“优化导入”? Eclipse, Intellij IDEA, and NetBeans all have ways to optimize imports. Eclipse,Intellij IDEA和NetBeans都有优化导入的方法。 I'm wondering if there are reasons other than consistency. 我想知道是否有其他原因而不是一致性。 Also, is there a more optimal way of importing? 还有,有更优化的导入方式吗? I've seen different standards that individuals and orginations have for optimizing imports. 我已经看到了个人和组织用于优化进口的不同标准。 For example... 例如...

import java.util.Map;
import java.util.List;

import com.company.MyClassThatUsesMap;

If I understand right, in the above example the classloader will load the Map and List classes before MyClassThatUsesMap . 如果我理解正确,在上面的例子中,类加载器将在MyClassThatUsesMap之前加载MapList类。 Will this add any benefit to the speed at which the code will run versus the example below? 这会增加代码运行速度的任何好处,而不是下面的示例吗?

import com.company.MyClassThatUsesMap;

import java.util.List;
import java.util.Map;

Does this even matter at all or does the compiler fix it altogether? 这甚至是重要的还是编译器完全修复了它?

If I understand right, in the above example the classloader will load the Map and List classes before MyClassThatUsesMap. 如果我理解正确,在上面的例子中,类加载器将在MyClassThatUsesMap之前加载Map和List类。

You don't understand right. 你不明白。 Imports have nothing to do with execution-time handling. 导入与执行时处理无关 They only affect how the compiler resolves short names (eg Map ) into fully-qualified class/method names. 它们影响编译器如何将短名称(例如Map )解析为完全限定的类/方法名称。

The aim of "optimizing" imports is for readability - not execution-time performance. “优化”导入的目的是为了提高可读性 - 而不是执行时性能。

There's no difference. 没有区别。 It's only for the developer's eyes, although most IDEs hide the imports since the developer is rarely interested in seeing them. 这只是开发人员的眼睛,虽然大多数IDE隐藏了导入,因为开发人员很少有兴趣看到它们。

Rearranging import wouldn't effect the performance of your code. 重新排列导入不会影响代码的性能。 Java compiler is smart enough. Java编译器足够聪明。 Yes, you are right, that IDEs do it for consistency and code readability/maintainability. 是的,你是对的,IDE是为了保持一致性和代码可读性/可维护性而做的。 If you want to compare java.util.Map; 如果要比较java.util.Map; with java.util.*; java.util.*; then it is different story all together. 那么这一切都是不同的故事。

Here is the official documentation. 是官方文档。 Optimizing the imports is not about speed. 优化进口与速度无关。 Its about cleaning up unused imports, and grouping similar packages together. 它是关于清理未使用的导入,并将类似的包分组在一起。 It is mostly for readability. 它主要是为了可读性。

It might change the order to put them in alphabetical order or another arbitrary order. 它可能会更改顺序,将它们按字母顺序或其他任意顺序排列。

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

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