Swaim's Object Oriented Programming Prime Directive:


In Star Trek one of the most important Starfleet directives is the Prime Directive which states that starships can visit and observe new worlds but they are not to interact with these worlds so as to alter their state of progress in technology.
I have modified the idea a little and applied it to object oriented programming. An object can be altered by "outside influences" which change its state. However, instead of the program (the "outside influencer") being responsible for keeping the object in a "correct" state, the object itself is responsible for maintaining its own internal integrety. This allows the program to concentrate on using the object without worrying about the correctness of the data the object contains. The Prime Directive in Object Oriented Programming is stated this way:

An object must never allow itself to be placed in an invalid state.

An invalid state is one where the values of the objects attributes are not consistant with the data type. For example, a date class should always represent a legal date. A date object holding the 32nd day of the 14th month is in an invalid state.

Keeping an object in a valid state is entirely the responsibility of the class methods. Any program using the class should never have to worry over whether an object is in a valid state. There are several things you can do when writing classes to insure that a valid state is maintained for all objects of the class: