jeudi 13 octobre 2016

Programming practices when it comes to checking the validity of data in class properties? Where should it be checked?

I'm writing a class that will be initialized as an array and takes an IP address in a string property in each instance.

Should the error checking to make sure that the IP is a valid, as in each octet is from 0-256 and not like 578.948.643.343, be built into the class so that when ever it's initialized or when ever the property is set, it is checked in there, or should the class be kept tidy and the error checking be done by the caller before the property is even set, such as on a WinForm?

My thinking is that doing the error checking in the class itself would add more clutter. From a design pattern perspective, should this be built into the class or be done on the winform initializing the class?

Aucun commentaire:

Enregistrer un commentaire