Avoid Empty Methods in Java
What is it?
This practice is triggered by the presence of empty methods in your Java code, which can be misleading and reduce code readability and maintainability.
Why apply it?
Empty methods can confuse developers into thinking they implement specific functionality, leading to maintenance and comprehension issues. They should either perform some operation, throw an exception, or include a comment explaining their purpose.
How to fix it?
Ensure that methods either contain functionality, throw an exception when they aren't implemented, or include a comment confirming the intentional absence of code.
Examples
Example 1:
Negative
The negative example contains empty methods without functionality, exceptions, or explanatory comments.
public class ExampleClass {
public void process() {
}
@Override
public void notImplemented() {
}
@Override
public void emptyOnPurpose() {
}
}
Example 2:
Positive
The positive example provides functionality within the method or includes a comment explaining why the method is intentionally left empty.
public class ExampleClass {
public void process() {
// Method that performs some processing
System.out.println("Processing...");
}
@Override
public void notImplemented() {
throw new UnsupportedOperationException("notImplemented() cannot be performed yet");
}
@Override
public void emptyOnPurpose() {
// Intentionally left blank due to subclass requirements
}
}
Negative
The negative example shows an unimplemented method without any explanation or exception throwing.
public class Calculation {
public double add(double a, double b) {
return a + b;
}
public double notImplementedYet() {
}
}
Example 3:
Positive
The positive example assures the method throws an exception if it's not yet implemented.
public class Calculation {
public double add(double a, double b) {
return a + b; // performs addition
}
public double notImplementedYet() {
throw new UnsupportedOperationException("Feature not yet implemented");
}
}