Method has an empty body
ID |
objectivec.maintainability.empty_function_body |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Dead Code |
Language |
Objective-C |
Description
Reports an instance or class method definition whose body holds nothing at all, whether the braces are written on the signature line or below it. Method declarations in an @interface have no body and are not reported, nor is a body carrying a statement or an explanatory comment.
Rationale
An empty method gives a reader no way to tell an unfinished stub from a deliberate no-op, and the two are wrong in different ways. A stub accepts every call silently and does none of the work its selector promises, so the missing effect surfaces far from here — a view that never refreshes, a cache that is never invalidated. A genuinely empty override is legitimate in Cocoa, where protocols and superclasses expect a selector to exist, but with nothing inside the braces to say so it reads exactly like the unfinished case.
The following code illustrates the pattern detected by this rule:
@interface Widget ()
- (void)prepare;
- (void)teardown;
@end
@implementation Widget
// FLAGGED: Method has an empty body
- (void)prepare { }
@end
Remediation
Implement the method, or delete it along with the call sites that reach it when the work turns out not to be needed. Where an empty body is required — an optional protocol method the class must expose, a template method with nothing to add — write a comment inside the braces saying so, which both documents the decision and stops this rule reporting it.