There are several reasons for a method not to have a method body:
- It is an unintentional omission and should be fixed to prevent unexpected behavior in production.
- It is not yet, or never will be, supported. In this case an
UnsupportedOperationException
should be thrown.
- The method is an intentionally-blank override. In this case a nested comment should explain the reason for the blank override.
Noncompliant Code Example
public function doSomething() {
}
public function doSomethingElse() {
}
Compliant Solution
public function doSomething() {
// Do nothing because of X and Y.
}
public function doSomethingElse() {
throw new UnsupportedOperationException();
}
Exceptions
Empty methods in abstract classes
abstract class Animal {
public function speak() {} // default implementation ignored
}