Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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,


Posted 10-24-2007 9:07 AM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

Eber Irigoyen wrote re: Object Construction - Constructing for Success - Part Duex
on 10-24-2007 12:33 PM

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

Derik Whittaker wrote re: Object Construction - Constructing for Success - Part Duex
on 10-24-2007 12:45 PM

@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.

:(

joeydotnet wrote re: Object Construction - Constructing for Success - Part Duex
on 10-24-2007 2:45 PM

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...

Eber Irigoyen wrote re: Object Construction - Constructing for Success - Part Duex
on 10-24-2007 2:57 PM

It's easy to be a lazy developer

Derik Whittaker wrote re: Object Construction - Constructing for Success - Part Duex
on 10-24-2007 3:25 PM

@Joey,

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

DotNetKicks.com wrote Object Construction - Constructing for Success - Part Duex
on 10-25-2007 9:38 AM

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

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)