G
Gustaf Liljegren
Here's a very general programming question. I often wonder where to put the
data validation when adding data to a struct or class, using its
constructor.
Suppose for example I got a class called Account with the variable Balance
(float) and this variable shall not be negative. Shall I put this test to
the code that creates the class and call its constructor, or shall it be
part of the class itself?
If it should be part of the class, how do I handle the situation when there
is a negative number submitted? In that case I don't want the Account to be
created or updated. If data validation should *not* be part of the class,
that seem to make the class very fragile.
Gustaf
data validation when adding data to a struct or class, using its
constructor.
Suppose for example I got a class called Account with the variable Balance
(float) and this variable shall not be negative. Shall I put this test to
the code that creates the class and call its constructor, or shall it be
part of the class itself?
If it should be part of the class, how do I handle the situation when there
is a negative number submitted? In that case I don't want the Account to be
created or updated. If data validation should *not* be part of the class,
that seem to make the class very fragile.
Gustaf