TYPICAL REFACTORING
To review, code smells are a series of commonly known coding antipatterns. Because they are commonly known, and widespread, developers have been able to develop some common and well-known ways of dealing with and fixing these code smells. These are called refactoring patterns. This section focuses on some of the more common refactoring patterns and explains how and when to apply them to your code.
Extract Classes or Interfaces
There are many reasons why you may want to split a class into smaller, more focused classes or have a series of more granular interfaces extracted from it. If a class is very large, it is probably doing too many things. If a class violates the SRP, it is a candidate to be split. If there is a design or technical reason that the class must remain in one piece, or the class is small but is still doing too many different things, extracting interfaces is a good alternative. Another good time to consider extracting interfaces from a class is when the client cares about only a portion of the class's public interface. Consider the following class:
public class InvoiceService { public string CreateInvoice(Invoice invoice) {…} public string ProcessPayment(Invoice invoice, double amount) {…} public double GetAmountOwed(Invoice invoice) {…} public double GetTotalAmountInvoicedLastFY(Customer customer) {…} public double GetTotalAmountPaidLastFY(Customer customer) {…} }
Although this may not be considered a large class, it does perform several ...
Get Professional Test-Driven Development with C#: Developing Real World Applications with TDD now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.