O'Reilly logo

Professional Test-Driven Development with C#: Developing Real World Applications with TDD by Jeff McWherter, James Bender

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required