Response for a Class (RFC)
v1.3.0
short name: RFC
effort: 15m
default threshold: 40
Response for a class is a quantitative metric that measures the number of uniquely invoked methods plus the number of methods in the class/mixin/enum/extension/extension type.
Classes with a high RFC are more complex and harder to understand, test and maintain.
Additional resources: Chidamber-Kemerer metrics paper.
Why Be Cautious
High response for a class can lead to:
- Increased Complexity: Higher RFC means more possible responses, making it hard to predict class behavior.
- Difficulty in Testing: Testing becomes difficult as there are more potential pathways to verify.
How to Address High Response for a Class?
Consider splitting the class into smaller classes, marking the primary class interact with only one interface (for example, via a facade or service class).
Config Example
analysis_options.yaml
dcm:
metrics:
response-for-class:
threshold: 55
To set multiple threshold or other common config options, refer to the Configuring Metrics page.
Example
info
To view what contributes to the metric value, generate an HTML report.
❌ Bad: High Response for a Class
// RFC: 10
class OrderProcessor {
final PaymentService paymentService;
final ShippingService shippingService;
final NotificationService notificationService;
final DiscountService discountService;
OrderProcessor(
this.paymentService,
this.shippingService,
this.notificationService,
this.discountService,
);
void processOrder(Order order) {
if (discountService.hasDiscount(order)) {
paymentService.applyDiscount(order);
}
paymentService.process(order);
shippingService.scheduleShipping(order);
notificationService.sendConfirmation(order);
}
void cancelOrder(Order order) {
paymentService.refund(order);
shippingService.cancelShipping(order);
notificationService.sendCancellation(order);
}
}
✅ Good: Low Response for a Class
// RFC: 6
class OrderProcessor {
final PaymentService paymentService;
final ShippingFacade shippingFacade;
OrderProcessor(this.paymentService, this.shippingFacade);
void processOrder(Order order) {
paymentService.process(order);
shippingFacade.handleShipping(order);
}
void cancelOrder(Order order) {
paymentService.refund(order);
shippingFacade.cancelShipping(order);
}
}
// RFC: 7
class ShippingFacade {
final ShippingService shippingService;
final NotificationService notificationService;
final DiscountService discountService;
ShippingFacade(
this.shippingService,
this.notificationService,
this.discountService,
);
void handleShipping(Order order) {
if (discountService.hasDiscount(order)) {
shippingService.scheduleShipping(order);
notificationService.sendConfirmation(order);
}
}
void cancelShipping(Order order) {
shippingService.cancelShipping(order);
notificationService.sendCancellation(order);
}
}