Constructor Choice - General Recommendations?

  • Thread starter Thread starter Bob Johnson
  • Start date Start date
B

Bob Johnson

Please note that this question is NOT about the merits of Microsoft
certification (no need to get into that here).

While taking a MS certification exam, I came across a question where it was
obvious that two out of the five choices (a multiple choice question) would
provide a workable solution to the scenario. The other chioces obviously
incorrect. The *only* difference between the two alternatives is that they
each used different constructor arguments for creating an instance of a bcl
object (the particular object is not relevant here).

Specifically, one valid alternative answer used the default constructor
(i.e. no arguments supplied - which is is valid for the class in question);
and then the very next line of code set a property of the object. The other
valid alternative answer supplied the property value as a constructor
argument (also a valid overload of the constructor for the class in
question).

To be clear, here's some p-code:

string s = "hi there";

// valid option 1
BaseClassObject obj = new BaseClassObject();
obj.SomeProperty = s; // very next line of code

// valid option 2
BaseClassObject obj = new BaseClassObject(s);

Is it generally recommended to go with either option 1 or option 2? By
"generally recommended" I'm wondering if there is some guideline that would
dictate which option I go with?

Please note that the question scenario provided absolutely no clues or
help - nothing like "accomplish xyz with as few lines as possible" - which
would have made option 2 the "correct" answer. And it didn't ask for two
ways to accomplish xyz. The question asked "which is the BEST way to do
xyz?"

Thoughts? Perspective?

And yes, I passed this exam over a year ago, but this has been bothering me
ever since.

Thanks!
 
[...]
string s = "hi there";

// valid option 1
BaseClassObject obj = new BaseClassObject();
obj.SomeProperty = s; // very next line of code

// valid option 2
BaseClassObject obj = new BaseClassObject(s);

Is it generally recommended to go with either option 1 or option 2? By
"generally recommended" I'm wondering if there is some guideline that
would
dictate which option I go with?
[...]

Heh...and I thought _I_ was asking questions for which the answer didn't
really matter. :)

Seriously though, I see very little difference in either method, assuming
a read/write property. Obviously the #1 option isn't possible if the
property isn't writeable. But beyond that, since the property setter is
likely to be inlined (assuming it's reasonably simple), it's not like
you'd avoid an extra function call with the #2 option.

In some cases, a property might be initialized in the default constructor
with a value other than the natural default for whatever is storing the
property value, or the property might be more complex where setting it is
more expensive than just copying some data from one place to another. In
those cases, I could see the #2 option being more efficient. But I would
say that in the most common case, there's no practical difference at all
between the two methods. At least in my experience, most commonly
properties are simply wrappers on scalar data types.

I suppose it's probably more likely that properties are initialized to
defaults other than the natural default for the type, so there's a slight
advantage to not having to set the property twice (once in the default
constructor, once after creating the object), but if your code's
performance is sensitive to that difference, you've got a bigger problem
than how you construct the object instance.

That said, you were taking a test. Multiple choice tests are notoriously
rife with errors of ambiguity, and part of taking a test is being skilled
at the art of test taking, as opposed to whatever the test is actually
supposed to test. So in this case, it seems to me that because there isa
theoretical argument in favor using option #2, that is likely to be the
"best" answer that the test is looking for.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top