Logo

dev-resources.site

for different kinds of informations.

Effective Java: Prefer Interfaces To Reflection

Published at
6/17/2021
Categories
java
effective
interface
architecture
Author
kylec32
Author
7 person written this
kylec32
open
Effective Java: Prefer Interfaces To Reflection

Java provides a very powerful capability in its reflection system. Reflection allows us to have extreme flexibility when calling classes, their constructors, and methods. These classes that we are interacting with may not even exist at the time that our reflexive code is written. This flexibility is not free, however; there are downsides to its use. Some of these are the following:

  • You lose out on compile time checks. Issues that would be exposed at compile-time when coded in non-reflexive style turn into runtime issues when coded via the reflexive apis.
  • Related to the above, static code analysis tools have trouble being helpful working with reflection code.
  • Reflexive code often is clumsy and hard to read.
  • Reflexive code has worse performance than non-reflexive code. In one benchmark, reflexive code proved to be 11x slower than a non-reflexive access.

There can be specific applications where reflexive coding practices can be preferred. Examples such as code analysis tools and dependency injection frameworks. Even that said, recently these types of tools have been switching away from using reflection to avoid the above downfalls.

Many of the times that we are reaching for reflection to solve a problem, what we are really after is the use of interfaces. By coding against an interface we still can code against code that has yet to be written, just like with reflection, and still gain some of the benefits of reflection. All of this without most of the downsides of the reflection APIs.

Reflection is an extremely powerful tool in the toolbelt of a Java developer. It is helpful to understand how it works and that it exists but most of the time is not the tool that we should be reaching for.

effective Article's
30 articles in total
Favicon
What is The Best Time to Learn New Things Effectively?
Favicon
Designing an Efficient Biomass Generator Set System
Favicon
Top 10 Essential Features for an Effective Knowledge Base
Favicon
Effective Java: Consider Serialization Proxies Instead of Serialized Instances
Favicon
Effective Java: For Instance Control, Prefer Enum types to readResolve
Favicon
Effective Java: Write readObject Methods Defensively
Favicon
Effective Java: Consider Using a Custom Serialized Form
Favicon
Effective Java: Implement Serializable With Great Caution
Favicon
Effective Java: Prefer Alternatives To Java Serialization
Favicon
Effective Java: Don't Depend on the Thread Scheduler
Favicon
Effective Java: Use Lazy Initialization Judiciously
Favicon
Effective Java: Document Thread Safety
Favicon
Effective Java: Prefer Concurrency Utilities Over wait and notify
Favicon
Effective Java: Prefer Executors, Tasks, and Streams to Threads
Favicon
Effective Java: Avoid Excessive Synchronization
Favicon
Effective Java: Synchronize Access to Shared Mutable Data
Favicon
Effective Java: Don't Ignore Exceptions
Favicon
Effective Java: Strive for Failure Atomicity
Favicon
Effective Java: Include Failure-Capture Information in Detail Messages
Favicon
Effective Java: Document All Exceptions Thrown By Each Method
Favicon
Effective Java: Throw Exceptions Appropriate To The Abstraction
Favicon
Effective Java: Favor The Use of Standard Exceptions
Favicon
Effective Java: Avoid Unnecessary Use of Checked Exceptions
Favicon
Effective Java: Use Checked Exceptions for Recoverable Conditions
Favicon
Effective Java: Use Exceptions for Only Exceptional Circumstances
Favicon
Effective Java: Adhere to Generally Accepted Naming Conventions
Favicon
Effective Java: Optimize Judiciously
Favicon
Effective Java: Use Native Methods Judiciously
Favicon
Effective Java: Prefer Interfaces To Reflection
Favicon
10 effective tips for New Developers

Featured ones: