Saturday, February 25, 2006

To use a single function to populate an objects data or to use individual properties?

I'm working on a project (which will be released to the public very soon) and I came across this dilemma, I had some code that looks like this:

myObject:=TSomeObject.Create;
myObject.Prop1:=value1;
myObject.Prop2:=value2;

but I had also previously created a method to assign all of the properties at once, so I could use this code as well

myObject:=TSomeObject.Create;
myObject.SetData(value1, value2);

now, the second code is shorter, but which code should I use?

I analized different factors that I usually worry about

- testability
- readability
- maintenance (I think there is a word "maintenancability" but is way too complicated)
- error prone (I'm sure there is no word for "error pronability" ;P)

In this case I decided to use the SetData method first of all because
- having values in all of the properties was critical,
- if I had to add properties to this object I could add them to the SetData function and the compiler would immediately report back all the places where I needed to change my code to accomodate for the new property
- if I used the code to assign values to each property, I would have to search my code for places where the object is instantiated to see if the new property is required there

of course this doesn't apply to all the cases, but if you think you are going to be adding properties to a class in the future, and you want to make sure that the value is provided for the new property, breaking your code this way might be the way to go

1 comment:

Ethaniel said...

> I think there is a word "maintenancability" but is way too complicated

“Maintainability”? ;)