What is the proper style for listing imports in Java?

前端 未结 10 1031
梦谈多话
梦谈多话 2021-02-18 16:01

Is it better to list each individual piece of a package you\'re going to need (see #1) or is it better to just import everything from a package (see #2)?

1



        
相关标签:
10条回答
  • 2021-02-18 16:06

    I know the question was not about performance but, the cost of import statements question of the month on javaperformancetuning.com perfectly summarize why you should avoid to use wildcards:

    (...) Finally, many people find that using imports with wildcards makes the source much less readable, since the reader also needs to figure out which package a particular class comes from, rather than just looking it up in the import statement.

    So the full answer is

    • There is no runtime cost from using an import statement
    • The compilation process can take a little more time with an import statement
    • The compilation process can take even more time with a wildcard import statement
    • For improved readability, wildcard import statements are bad practice for anything but throwaway classes
    • The compilation overhead of non-wildcard import statements are minor, but they give readability benefits so best practice is to use them

    It is a bad practice to use wilcard import statements, they make the code less readable, just don't do it. The only exception to this rule are static import, for example import static org.junit.Assert.*;. No, I don't want to import each assertXXX individually and I don't find that this harms code readability.

    0 讨论(0)
  • 2021-02-18 16:12

    whatever the default of your team's IDE is. that way you can just do an autoformat of your file before committing it.

    0 讨论(0)
  • 2021-02-18 16:14

    The latter is generally considered "bad form". With tool support, there's no excuse for not listing them individually, and it removes any ambiguity.

    Mind you, Eclipse by default uses wildcards for static imports, and explicit imports for normal ones. Not sure why it makes that distinction.

    edit: the distinction is obvious in hindsight - static imports often refer to static methods, and you can't unambiguously refer to a static method by name (due to overloading). So if it can't be fully unambiguous, you may as well use a wildcard.

    0 讨论(0)
  • 2021-02-18 16:19

    If you use an ide like visual studio it typically takes care of this issue for you, so you don't even need to bother thinking about it.

    Most IDEs that I am aware of choose option 1 above. This is consistent with some code analysis tools that I used in the past (like checkstyle), as they consider it bad practice to use the * notation.

    0 讨论(0)
  • 2021-02-18 16:20

    The option 1 you listed is preferable but if there are more classes you are importing from say java.awt.image, then its preferable to just have one import java.awt.image.* Most IDE's let you specify the exact count of the number of imports to be used. For eg in IDEA, we can use File->Settings->Code Style->imports In the General tab there is a field Class count to use import with * and default value is 5.

    0 讨论(0)
  • 2021-02-18 16:21

    Use import individually.

    It is way lot better for production code to list each and every one of the classes being imported.

    While the IDE's do a great job helping you know which classes are being used, it is more readable to know that you're referring to :

    java.util.List;
    

    and not

    java.awt.List; 
    

    and so on.

    Additionally it is recommended that you group them by package starting by the core libraries passing by 3rd party and ending with own project libraries:

     import java.util.List;
     import java.util.ArrayList;
     import java.util.Date;
    
     import javax.swing.JFrame;
     import javax.swing.JPanel;
    
     import javax.swing.event.TableModelListener;
    
     import org.apache.commons.httpclient.cookie.CookiePolicy;
     import org.apache.commons.httpclient.cookie.CookieSpec;
    
     import your.own.packagename.here.SomeClass;
     import your.own.packagename.here.OtherClass;
    

    Using wildcard is acceptable for small self project/classes. It is faster, but it is not expected to be maintainable. If any other developer is involved, use the first.

    0 讨论(0)
提交回复
热议问题