You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Java has two different ways to import classes. Explicit or with a wildcard.
// class importimportandroid.app.Activity;
// import using wildcardimportandroid.app.*;
Actually there is not a big difference. Imports are handled by the IDE and collapsed by IntelliJ. Autocompletion doesn't benefit from wildcard imports.
The main reason why we don't use wildcard imports is that they hide which explicit class or static method is used in code reviews. This problem gets bigger with Kotlins extensions functions which also have to be imported.
Without an IDE it's impossible to know where a extension method is defined.
// Guess the class where this method is definedfun String.isValidUsername() {
// ...
}
This makes code reviews harder because you really want to know if the method is defined in a third party library or is part of the project.
Therefore we don't use star imports ...except for test.
Star imports in tests
Testing libraries such as Mockito or AssertJ have tons of static helper methods. If you use one of them it's very likely you want to use all of them without explicitly importing the function. Without star imports you have to write Mockito.<method> for every new method and then import the static method manually. For a simple test this is a lot of typing.
// no star importsimportstaticorg.mockito.Mockito.mock;
importstaticorg.mockito.Mockito.never;
importstaticorg.mockito.Mockito.verify;
importstaticorg.mockito.Matchers.anyInt;
importstaticorg.mockito.Matchers.anyString;
@TestpublicvoidcheckLoggerDoesntLog() throwsException {
// ...// this line requires 4 manual static importsverify(logger, never()).log(anyInt(), anyString(), anyString());
}
With star imports you have to import org.mockito.Mockito.* once and then all other methods are available via autocompletion.
// star imports in testsimportstaticorg.mockito.Mockito.*;
@TestpublicvoidcheckLoggerDoesntLog() throwsException {
// ...// once verify was imported all other methods are available, tooverify(logger, never()).log(anyInt(), anyString(), anyString());
}
No star imports in production code
Java has two different ways to import classes. Explicit or with a wildcard.
Actually there is not a big difference. Imports are handled by the IDE and collapsed by IntelliJ. Autocompletion doesn't benefit from wildcard imports.
The main reason why we don't use wildcard imports is that they hide which explicit class or static method is used in code reviews. This problem gets bigger with Kotlins extensions functions which also have to be imported.
Without an IDE it's impossible to know where a extension method is defined.
This makes code reviews harder because you really want to know if the method is defined in a third party library or is part of the project.
Therefore we don't use star imports
...except for test.
Star imports in tests
Testing libraries such as Mockito or AssertJ have tons of static helper methods. If you use one of them it's very likely you want to use all of them without explicitly importing the function. Without star imports you have to write
Mockito.<method>
for every new method and then import the static method manually. For a simple test this is a lot of typing.With star imports you have to import
org.mockito.Mockito.*
once and then all other methods are available via autocompletion.Libraries with star imports
Java
Kotlin
The text was updated successfully, but these errors were encountered: