Programmer’s Solemn Obligation
A programmer’s solemn obligation is to ensure the integrity of an object’s state.
An object’s state has integrity when every one of the object’s fields contains a valid value.
How to ensure integrity
Every public class has a public interface and a private implementation. The only way a programmer can ensure the integrity of an object’s state is by controlling access to the object’s instance fields; this control requires that instance fields be private.
If instance fields are private, access and mutation must be provided via methods as described in the public interface. An accessor provides access to the state of an object. A mutator allows an object’s state to be modified in a way which preserves the integrity of the object.
For testing purposes, programmers should code a
toString method to provide a string representation of an object’s state. In most cases,
equals methods should also be coded.
[This page last updated 2020-12-23 at 12h13 Toronto local time.]
© 2007–2021 Hersch, Bear & Company Limited. All rights reserved. “Grammar Authority,” “grammarauthority.com,” “touque.ca,” and “Canada’s Thinking Cap” are trademarks of Hersch, Bear & Company Limited. All other trademarks and copyrights are the property of their respective owners.