So been doing a lot of reading/research into clean code/design, OOAD, refactoring, TDD, etc. Just trying to improve my designs to be easier to extend and maintain. One thing that is come up quite often is avoiding doing "real work" in a constructor.
I won't post the resources here but names like Misko Hevery, Bob Martin, etc. seem to recommend avoiding doing much work in the constructor (quick Google search turns up a pleathora of articles). My question is not so much HOW to make this happen, but WHY? Why would I choose to use a static factory method? Why does this improve testability (as they say)?
An example of what I'm talking about is here:
public class ConstructorObject
{
public int Value { get; }
public ConstructorObject()
{
// do work (e.g. calculate divisors, GCD for array, etc.) to calculate value
Value = value;
}
}
public class FactoryMethodObject
{
public int Value { get; }
public static Create()
{
// do work (e.g. calculate divisors, GCD for array, etc.) to calculate value
return new FactoryMethodObject(value);
}
private FactoryMethodObject(int value)
{
Value = value;
}
}
To me, these appear to provide the same advantages and disadvantages. So I'm trying to figure out the WHY... Thanks!