Skip to content
This repository was archived by the owner on Oct 27, 2025. It is now read-only.
This repository was archived by the owner on Oct 27, 2025. It is now read-only.

No star (*) imports  #5

Description

@passsy

No star imports in production code

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() {
// ...
}
importnet.grandcentrix.*importnet.grandcentrix.utils.*importcom.github.kotlinutils.auth.*// ...funlogin(username:String, password:String) {
if (username.isValidUsername() && password.isValidPassword()) {
userManager.login(username, password)
}
}

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());
}

Libraries with star imports

Java

Kotlin

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions