Explain Codes LogoExplain Codes Logo

Why is using a wild card with a Java import statement bad?

java
best-practices
code-readability
explicit-imports
Anton ShumikhinbyAnton Shumikhin·Sep 19, 2024
TLDR
Avoid `import java.util.*;` due to **Readability** and **Namespace pollution**. Specific imports clarify the used classes (`import java.util.List;`), preventing ambiguity and conflicts, especially when classes share names across packages. Explicit declarations enhance **clarity** and **maintainability**.

Understanding explicit versus wildcard imports

Remember how in good thrillers, the mystery deepens when we find an unidentified fingerprint at the scene? Similarly, in coding, having unidentified classes (Hello, import java.util.*;) can create mystery—unwanted mystery in this case. Here's why:

Explicit import declarations (import java.util.List;) help developers understand class usage clearly without ambiguity, providing easier readability and maintainability of the codebase.

For instance, if the code references the List class, the explicit import demonstrates its origin from java.util package, eliminating ambiguity. Aside from providing ease of understanding, it also prevents potential conflicts when classes share names across different packages.

// Explicitly importing classes: import java.util.List; import java.util.Map; // No more hide & seek with class origins!

Embracing IDE capabilities: Refactoring made easy

Modern IDEs come equipped with powerful features to optimize import statements. They can transform wildcard imports to explicit ones, making refactoring easier and efficient. But what if you're already a fan of Eclipse IDE and its wildcard imports habit? No worries! You can change this setting to align with best practices for explicit imports.

// Before IDEs' magic: import java.util.*; // After IDEs' magic: import java.util.List; import java.util.Map; // Abra-ka-dabra, wildcards be gone!

Weighing the trade-offs

There’s a famous saying—”All that glitters is not gold.” Similarly, all that cuts down on import lines is not always beneficial. While Robert C. Martin underscores the advantage of wildcard imports in reducing the intimidation factor of large import lists, the broader agreement leans towards explicit imports to maintain clear references.

It's like picking up a soda can instead of lugging around a six-pack when you're only thirsty for one.

// Only thirsty for one: import java.util.List; import java.util.Map; // But still carrying the six-pack: import java.util.*; // Sodas everywhere. Thirst... Nowhere!

When opinions diverge: Best practices

There's no hard-and-fast rule against wildcard imports. Coding standards differ across various institutions and project specifications. While Google's Java Style Guide advises against wildcards, not everyone shares the same standpoint.

The advice for budding developers is to stick to explicit imports for a clear understanding of dependencies. As you mature in managing the codebase, you might argue for less verbose import sections. Nonetheless, the maintainability and readability provided by explicit imports often outweigh the convenience of wildcard imports.

// Good practice: Specific imports. import java.util.List; import java.util.Map; // You know who's who.

Being explicit helps

  • Code navigation: Knowing exactly what classes are used where simplifies understanding the code.
  • Code reviews: Clear dependencies means easier code reviews and better quality assurance.
  • Avoid accidental usage: Preventative against risky bugs by avoiding unintentional adoption of classes from unused parts of a wildcard-imported package.

Potential risks of wildcards

  • Name clashes: Newly added classes in imported packages can tip-toe into an unexpected conflict.
  • Blind-spots in changes: During refactoring or updating libraries, explicit imports help identify the affected areas faster.
  • Compiler performance: Though negligible, in larger projects, wildcard imports can cause a performance dip while compiling.