除了减少混乱之外,还有什么理由清理 Java 中未使用的导入? [英] Any reason to clean up unused imports in Java, other than reducing clutter?

查看:24
本文介绍了除了减少混乱之外,还有什么理由清理 Java 中未使用的导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有充分的理由避免在 Java 中使用未使用的导入语句?据我了解,它们用于编译器,因此许多未使用的导入不会对编译后的代码产生任何影响.是否只是为了减少混乱并避免命名冲突?

Is there any good reason to avoid unused import statements in Java? As I understand it, they are there for the compiler, so lots of unused imports won't have any impacts on the compiled code. Is it just to reduce clutter and to avoid naming conflicts down the line?

(我之所以这么问是因为 Eclipse 对未使用的导入给出了警告,这在我开发代码时有点烦人,因为我不想删除导入,直到我很确定我已经完成了类的设计.)

(I ask because Eclipse gives a warning about unused imports, which is kind of annoying when I'm developing code because I don't want to remove the imports until I'm pretty sure I'm done designing the class.)

推荐答案

我认为,如果您不删除导入,就不会出现性能问题或类似问题.

I don't think that performance problems or something like that are likely if you are not removing the imports.

但可能存在命名冲突,在极少数情况下,例如导入列表界面.

But there could be naming conflicts, in rare cases like importing the list interface.

在 Eclipse 中,您始终可以使用快捷方式(取决于操作系统 - Win:Ctrl + SHIFT + O 和 Mac:COMMAND + SHIFT + O)来组织导入.Eclipse 然后清理导入部分,删除所有过时的导入等.如果您再次需要导入的东西,Eclipse 会在您使用 Ctrl + SPACE 完成语句时自动添加它们.所以没有必要在你的类中保留未使用的代码.

In Eclipse you can always use a shortcut (depends on OS - Win: Ctrl + SHIFT + O and Mac: COMMAND + SHIFT + O) to organize the imports. Eclipse then cleans up the import section removes all the stale imports etc. If you are needing a imported thing again eclipse will add them automatically while you are completing the statement with Ctrl + SPACE. So there is no need in keeping unused code in you class.

一如既往,未使用的代码会在阅读代码时分散您和其他人的注意力,并在您的活动代码中留下一些东西,因为也许我以后需要它,这通常被视为不好的做法.

As always unused code will distract you and other people while reading the code and leaving something in your active code because of maybe I need it later is mostly seen as bad practice.

这篇关于除了减少混乱之外,还有什么理由清理 Java 中未使用的导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆