太多的进口垃圾邮件我的代码 [英] Too many imports spamming my code

查看:145
本文介绍了太多的进口垃圾邮件我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有一个形状包,它有我为我的图形程序设计的形状,例如Rectangle,Circle。我还有一两个包有与 java.awt 类同名的包。



现在,由于我不想重命名我的代码库中的每个类,以显示我的源文件,我的意思是什么,我说,声明一个新的矩形,我需要:


1-明确导入矩形类,即import shapes.Rectangle



2-只导入我需要的java.awt类import java.awt。*自动包含awt.Rectangle


现在问题是这两种方法都会导致 ,我目前在每个源文件中平均有15-25个导入,这严重使我的代码混乱和混乱。



您的代码中有太多导入是不好的事情?有什么办法吗?

解决方案


  • 而不是导入整个包


  • 任何好的IDE(例如 Eclipse )都会在一行中折叠导入,


  • 如果发生冲突,您可以随时参考完全限定的类,但如果其中一个两个类在你的控制之下,你可以考虑重命名它。 (使用Eclipse,右键单击类,选择 Refactor - > Rename ,它会小心更新其所有引用)。


  • 如果您的课程是从AWT和您的形状包导入,是确定。从几个类导入是确定的;但是,如果你发现自己从很多不同的来源导入,这可能是一个迹象,表明你的类做太多,需要分裂。


In my project I have a shapes package which has shapes I designed for my graphics program e.g Rectangle, Circle. I also have one or two more packages that have the same names as java.awt classes.

Now, since I do not want to rename every class in my code-base, to show my source files which class I mean when I , say, declare a new Rectangle, I need to either:

1- import the rectangle class explicitly, i.e import shapes.Rectangle

OR

2- import only the java.awt classes I need and not import java.awt.* which automatically includes the awt.Rectangle

Now the problem is that both ways result in a lot of importing, I currently have an average of 15-25 imports in each source file, which is seriously making my code mixed-up and confusing.

Is too many imports in your code a bad thing? Is there any way around this?

解决方案

  • It's a good practice to import class by class instead of importing whole packages

  • Any good IDE, such as Eclipse, will collapse the imports in one line, and you can expand them when needed, so they won't clutter your view

  • In case of conflicts, you can always refer to fully qualified classes, but if one of the two classes is under your control, you can consider renaming it. (with Eclipse, right click on the class, choose Refactor -> Rename, it will take care to update all its references).

  • If your class is importing from AWT and from your package of shapes, is ok. It's ok to import from several classes; however, if you find yourself importing from really lots of disparate sources, it could be a sign that your class is doing too much, and need to be split up.

这篇关于太多的进口垃圾邮件我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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