Problem: Methods are difficult or impossible to test because they require too much state and change too many properties
Pattern: During development a rule is adopted that anything which alters the state of an object, or performs manipulation of data, is written as a static method that operates only on data passed as a parameter. Instance methods are then written that pass all required values as parameters to these static methods, then set instance members based only on what is returned.
Example: In the old code, a method that's difficult to test might look like this:
return this.ProcessedData.Length == this.InternalData.Length;
By following "Everything is a Parameter", refactored code might look like this:
List<StrawberryValue> results = new List<StrawberryValue>();
StrawberryData compensated = CompensateByFactor(datum, CompensationFactors, this.MagicValueQ, this.MajorConstant);
if (compensated.Value > 0)
if (results.Count() != this.InternalData.Length)
throw new Exception("Could not compensate for all values");
this.ProcessedData = results;
Discussion: In the example, the new ProcessData() is practically a blueprint for the unit test of CompensateByFactor, which has not only inherited all of the business logic, but also takes everything it operates on as a parameter.