Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Data Sanitation

Name: Anonymous 2011-05-02 18:32

Quick opinion question for you:

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

Name: Anonymous 2011-05-02 18:43

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: Anonymous 2011-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.

Name: Anonymous 2011-05-02 18:48

You don't mutate state.

Name: Anonymous 2011-05-02 18:51

>>4
The OP's concern is also valid if/when considering initialization.

Name: Anonymous 2011-05-02 19:00

>>5
The constructor should just glue up the data structure with its inputs, so (make-structure (sanitize data)).

This way you can reuse sanitize elsewhere.

Name: Anonymous 2011-05-02 19:23

SANITIZE MY ANUS

Name: Anonymous 2011-05-02 20:12

>>7
You know, it doesn't sound as good when the words work together like that ...

Name: Anonymous 2011-05-02 22:05

>>8
fuck you faggot

Name: Anonymous 2011-05-02 22:56

"sanitize" is another one of those enterprise buzzwords that managers like to throw around.

Name: Anonymous 2011-05-03 0:06

>>10
I take it you don't shower

Name: Anonymous 2011-05-03 2:15

>>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: Anonymous 2011-05-03 2:47

>>1
I use association lists, instead of classes
map sanitize Object
is enough

Name: Anonymous 2011-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: Anonymous 2011-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

Name: Anonymous 2011-05-03 3:05

>>15
First example may leads to problems and need of refactoring in future.
If you don't see it, it's your problem.

Name: Anonymous 2011-05-03 3:07

Your life may leads to death and need of burying in future.
If you don't see it, it's your problem.

Name: Anonymous 2011-05-03 3:27

former

Don't change these.
Name: Email:
Entire Thread Thread List