Any reason to clean up unused imports in Java, other than reducing clutter?

后端 未结 11 1340
生来不讨喜
生来不讨喜 2020-11-28 06:34

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 o

11条回答
  •  清酒与你
    2020-11-28 07:16

    This has to do with clarity of the program useful for maintenance.

    If you had to maintain a program you'll find how useful is to have a single class import per line.

    Think about the following scenario:

    import company.billing.*;
    import company.humanrerources.*;
    
    // other imports 
    
    
    class SomeClass {
          // hundreds or thousands of lines here... 
        public void veryImportantMethod() {
          Customer customer;
          Employee comployee;
          Department dept. 
          // do something with them
         }
     }
    

    When you're bugfixing or maintaining piece of code ( or only reading it ) it is very helpful for the reader to know to which package do the classes used belong to. Using the wildcard import as shown above doesn't help for that purpose.

    Even with an IDE, you don't want to hover or jump to declaration and return, it is easier if you understand in terms of functionality what other packages and classes the current code depends on.

    If this is for a personal project or something small, it really doesn't matter, but for something larger that have to be used by other developers ( and maintained through the years ) this is a MUST HAVE.

    There is absolutely no performance difference with any.

提交回复
热议问题