Is it a good idea (from a design POV) to nest constructor calls for overloaded New or Factory style methods? This is mostly for simple constructors, where each overload builds on the previous one.
MyClass( arg1 ) { _arg1 = arg1; _otherField = true; _color="Blue" } MyClass( arg1, arg2) : this(arg1) { _arg2 = arg2 } MyClass( arg1, arg2, arg3) : this(arg1, ar2) { _arg3 = arg3; }
Or with factory methods:
static NewInstance(arg1 ) { _arg1 = arg1; } static NewInstance(arg1, arg2) { f = NewInstance(arg1); f._arg2 = arg2; } //... and so on
I can see a few drawbacks on both sides
So, is doing this a good idea, or does it set me up for something I'm just not seeing as a problem. For some reason I feel uneasy doing it, mostly because it divides up the responsibility for initializing.
Edit: @Jon Skeet: I see now why this was bothering me so much. I was doing it backwards! I wrote the whole thing and didn't even notice, it just smelled. Most other cases I have (that I wrote), do it the way you recommend, but this certainly isn't the only one that I have done like this. I do notice that the more complicated ones I did properly, but the simple ones I seem to have gone sloppy. I love micro edits. I also like acronymns!
The factory method is a smart way to create objects in Java and provides several advantages over the traditional approach of creating objects using constructors in Java. It can also improve the quality of code by making the code more readable, less coupled, and improves performance by caching.
A factory constructor is a constructor that can be used when you don't necessarily want a constructor to create a new instance of your class. This might be useful if you hold instances of your class in memory and don't want to create a new one each time (or if the operation of creating an instance is costly).
A static constructor is called automatically. It initializes the class before the first instance is created or any static members declared in that class (not its base classes) are referenced. A static constructor runs before an instance constructor.
To call one constructor from another within the same class (for the same object instance), C# uses a colon followed by the this keyword, followed by the parameter list on the callee constructor's declaration. In this case, the constructor that takes all three parameters calls the constructor that takes two parameters.
I think it's reasonable to chain constructors together, but I do it the other way - the version with fewer parameters calls the version with more parameters. That way it makes it very clear what's happening, and all the real "logic" (beyond the default values) is in a single place. For example:
public Foo(int x, int y) { this.x = x; this.y = y; precomputedValue = x * y; } private static int DefaultY { get { return DateTime.Now.Minute; } } public Foo(int x) : this(x, DefaultY) { } public Foo() : this(1, DefaultY) { }
Note that if you have lots of constructor overloads, you may wish to move to static factory methods instead - that usually makes the code clearer, as well as allowing multiple methods to take the same set of parameters, e.g.
public static XmlDocument FromText(string xml) public static XmlDocument FromFile(string filename)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With