Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



Object Construction - Constructing for Success - Part Duex

A few months ago I posted about my thoughts on Object Construction (found here).  In the post I talk about constructing object with ALL the required values to make the object valid. 

This post is just a quick follow up to that.  The other day I was refactoring some code and needed to add a new property to it.  The new property was a required one and the object would not be considered valid without it being populated.  Because I have my constructor require all needed properties, I simply added the parameter to the constructor's signature and re-compiled. 

Because this is now part of my constructor I instantly knew where I needed to update my code to add this call (yes I know I could have found this other ways, but this is the simplest if you ask me).  Had my constructor NOT required the values, I would have had to search for every use of the object and perform a set on my new property.  This is doable, but is more error prone.  With my luck I would have missed something.

Anyway, the point of this was that if you put requirements on your objects it will make for simpler and easier refactoring later.

Till next time,



Comments

Eber Irigoyen said:

have been using that technique since the good old Delphi days =o)

# October 24, 2007 12:33 PM

Derik Whittaker said:

@Eber,

Then you know exactly what i am talking about.  Sad thing is, some people i know DONT want to construct objects that are valid.  They like having to set each property one by one.

:(

# October 24, 2007 12:45 PM

joeydotnet said:

In some situations, encapsulating the construction process in a Factory can make it easier to change how an object gets a constructed later on.  This works great if the construction process itself starts to get complex.  This removes the reliance upon the constructor and abstracts it behind, even a Factory interface.  

Just another perspective...

# October 24, 2007 2:45 PM

Eber Irigoyen said:

It's easy to be a lazy developer

# October 24, 2007 2:57 PM

Derik Whittaker said:

@Joey,

You are correct.  in fact in my first post I mentioned the factory option as well.

# October 24, 2007 3:25 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# October 25, 2007 9:38 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News