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

Where to enforce requirements

Name: Anonymous 2010-09-18 23:32

Requirement: the name variable in class X has a maximum length of 10 characters.

Should class X even know about this requirement, or should the requirement be enforced solely by the business logic class?

Name: Anonymous 2010-09-19 2:55

It depends on the circumstances.

If it is not physically possible for the name class to have a length greater than ten (I.E: it represents some specific, documented naming standard which specifies all names must be less than ten characters) then this would go inside class X. It would enforce strict requirements in the setter for the name, as well as in the constructor. In fact, in this case, you should probably be making a specific class for the name property and encapsulating the login in there (much like you would a phone number class, or a ISBN class).

If however, this is simply a requirement necessitated by external influence and not really chosen for a specific design purpose (Say, the GUI does not have space for names longer than ten characters, a legacy database stores names in VARCHAR(10) or similar) then you would not put the logic in X. The class X will still support names longer than ten characters, but the classes that need a ten character name will need to perform appropriate transformations. (i.e.: the GUI could truncate it, insert a dash and move the rest to a newline, the database could maintain a separate map from unique 10 character 'id' values to true names.

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