When you're instantiating an object based on unsafe input from a form in a loosely-typed language, do you leave the sanitation up to the form handler, or to the object?
Ex:
p = new Pokemon();
p.name = sanitize(form.name);
vs. p = new Pokemon();
p.setName(form.name); // sanitizing code in the method
Adding to this: the object may also be instantiated from safe input in other points of the program. It will directly access the properties when doing this:
p.name = safeInput.name; return p;
Name:
Anonymous2011-05-02 18:47
To help you find an answer:
what happens if, when sanitizing in the method, you stumble upon invalid input? You can signal the error to the enclosing code with the appropriate mechanism: let's just throw some exception in this case. Should this be handled by the enclosing code? How? Can you let the exception bubble up higher up?
Personally I'd rather handle the error at the site of input because it's usually too late at the site of construction. But remember that's due to the design of GUIs/frameworks/whatever.
>>6
I agree with this.
Also, if you want to pass safe data to the object at a later point (cloning a valid object for example) there is no reason for the sanitasion logic to be in the mutator method.
Name:
Anonymous2011-05-03 2:47
>>1
I use association lists, instead of classes map sanitize Object
is enough
Name:
Anonymous2011-05-03 2:55
>>1
IMHO first example breaks encapsulation, because external functions need to know, what data may be valid or not for Pokemon class. Should this information be part of delivered interface?
Name:
Anonymous2011-05-03 2:59
>>14
It's what I call "mental masturbation", when you engage is some pointless intellectual exercise that has no possible meaning. -- Linus Torvalds