Method cohesion
Methods on their own can also suffer from low-cohesion. One symptom of a method with low-cohesion is size. Methods with low-cohesion are often large. A large method is generally doing more than it needs to. As we saw with the Large Method Code Smell in Chapter 2, refactoring is as simple as breaking the method up into multiple methods. Each method should take on a single responsibility. Another symptom of a class that suffers from low-cohesion is one that has many parameters. A method that takes many parameters is probably doing too many things. Unfortunately, how to refactor depends on what is trying to be accomplished.
One example of too many arguments is often constructors. For example:
/// <summary>/// Example of a class with low-cohesion/// </summary>public class Invoice{ public string GivenName { get; private set; } public string SurName { get; private set; } public string Street { get; private set; } public string City { get; private set; } public string Province { get; private set; } public string Country { get; private set; } public string PostalCode { get; private set; } public Invoice(IEnumerable<InvoiceLineItem> invoiceLineItems, string givenName, string surName, string street, string city, string province, string country, string postalCode, Func<float, float, float> calculateGrandTotalCallback) { GivenName = givenName; SurName = surName; Street = street; City = city; Province = province; Country = country; PostalCode = postalCode; } //...}
The Invoice class has taken on the extra responsibility of managing customer information (given name, surname, street, and so on) and thus one of its constructors has many parameters.