Data validation for structs and classes?

  • Thread starter Thread starter Gustaf Liljegren
  • Start date Start date
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
 
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?

It should be a part of the class itself, according to the principle of
Encapsulation (one of the basic OOP principles).
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.

You should throw ArgumentOutOfRangeException when the class constructor is
supplied with values that do not fall into allowed range of values. To the
best of my knowledge, when an exception is thrown from a constructor, the
instance is not created.
 
Hi,

the instance *will* get created, that is, allocated on the heap. It just
won't get fully constructed because of the exception, if thrown. In that
case, you don't get a chance to reference the just-created object, which
will get garbage-collected at some later point in time.
I agree on validating the arguments within the constructor and throwing
exceptions if they don't.
(I answered a same question yesterday)

Greetings,

Bram.


Dmitriy Lapshin said:
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?

It should be a part of the class itself, according to the principle of
Encapsulation (one of the basic OOP principles).
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.

You should throw ArgumentOutOfRangeException when the class constructor is
supplied with values that do not fall into allowed range of values. To the
best of my knowledge, when an exception is thrown from a constructor, the
instance is not created.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Gustaf Liljegren said:
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
 
Back
Top