Click here to Skip to main content
16,012,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was reading a book today called "Programming C# 5.0" by O'Reilly and was reading in more depth about constructors, I found this;

"You are likely to surprise developers who use your class if you write a constructor that does
something non-trivial, such as adding data to a database or sending a message over the
network."


Would this be correct?

I'm a newish C# programmer but was surprised by this (unless I have been learning in a completely wrong way) and was interested on the thought on this.

When I look at many examples on CodeProject I find alot of code takes place in the constructors (which would make sense to me?).

Thanks for your time!

It appears I had confused a Constructor with a Method...very silly mistake. I thank all of you for your time for replying.
Posted
Updated 27-Jun-13 9:26am
v2
Comments
[no name] 27-Jun-13 14:46pm    
"alot of code takes place in the constructors", such as?
"which would make sense to me", why?
"Would this be correct?", yes it would certainly surprise me.
CHill60 27-Jun-13 14:54pm    
Yep - it would surprise me too. The clue is in the name "Constructor" - it's not "ConstructorAndStartDoingStuffUnprompted" - Always use things for what they were intended - in this case doing just enough to ensure that the resulting object has been created in the way that is required. "a lot of code" in constructors may just be doing things like reading standing data (nb reading NOT writing) although I prefer to have an Initialise routine that is explicitly called
SteveBaldwin21 27-Jun-13 15:04pm    
Ah I see, this made sense.

I did misunderstand what the book said completely, or at least how I understood it was intended.

So just playing on example of the database write scenario; what would the negative effect of calling a constructor to write data.

CHill60 27-Jun-13 15:30pm    
Well one thought that springs immediately to mind is "why is it taking so long just to create this object". Then ... my system just crashed because there's a database problem and where on earth was I connecting to the database??? Confusion can reign. And what if someone decides to create a class inheriting from this one, but doesn't realise that it's writing to the database ... best to have explicit functions for stuff like this (Edit - removed the quote - it was the wrong one!)
SteveBaldwin21 27-Jun-13 16:17pm    
Thank you CHill60.

1 solution

As others have already noted, the hint is in the name. Constructors should do what their name suggest: initialize the object, that is put the object in well known, reliable state, no less, no more. Doing less is catastrophic, doing more is bad.
 
Share this answer
 
Comments
SteveBaldwin21 27-Jun-13 15:06pm    
"Doing less is catastrophic, doing more is bad."

I would be very grateful if you could take the time and elaborate on this.

Thank you!
CPallini 27-Jun-13 15:45pm    
I assume you don't need elaboration on 'doing less'.
Doing more is bad because:
1. Could be useless. Maybe the object consumer doesn't need the 'extra functionaly'.
2. Could be wasteful. Maybe the object consumer doesn't like the result of the 'extra functionality' and has to perform some work to withdraw the modifications.
3. Could be dangerous. the 'extra functionality' could throw an exception, maybe interfering with proper object initialization.
4. (no more thoughts, at the moment... :-)
SteveBaldwin21 27-Jun-13 16:17pm    
Thank you for the bullet points very informative.

I had confused a constructor with a method and I caused myself much confusion; This really helped me understand Constructors though.

Thanks all for the replies.
CPallini 28-Jun-13 10:22am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900