PDA

View Full Version : A simple question about setters



Dark_Tower
13th March 2006, 09:38
Hi, I'm programming a class that handles a certain types of parameters. And also can save/load them from disk. Well for each parameter I have it's corresponding setter and getter method all similar to the following:


double ProcParamsHandler::sizeH()
{
return m_sizeh;
}

void ProcParamsHandler::setSizeH(double newSizeH)
{
if (newSizeH < 0 || newSizeH > MAX_SIZE_H)
newSizeH = INVALID_SIZE;
m_sizeH = newSizeH;
}
I also have implemented a method to set the value for all the parameters (like the setter above) all in just a simple call. Well my question is: what's the common way to implement this "global" setter: calling its setter for each parameter or putting directly the code of its corresponding setter for each parameter?

Thanks.

Michiel
13th March 2006, 10:03
It's most elegant to use the setter methods. It prevents code duplication and makes the code easier to read. The performance hit for the test will be negligible.

Also, I'd throw an exception in case of bad input, not return a 'special' value. It's something you might consider.