Garo Garabedyan's Divergent Thinking Blog

Object-Oriented Design Heuristics

with 2 comments

Heuristics refers to experience-based techniques for problem solving, learning, and discovery. Heuristic methods are used to come to an optimal solution as rapidly as possible. Part of this method is using a “rule of thumb”, an educated guess, an intuitive judgment, or common sense. A heuristic is a general way of solving a problem. In more precise terms, heuristics stand for strategies using readily accessible, though loosely applicable, information to control problem solving in human beings and machines. (Source: Wikipedia)

I have published a presentation I have found on a book called “60 Design Heuristics. Object-oriented Design Heuristics” by Arthur J. Riel.

Classes and Objects: All data should be hidden within its class (Only private attributes; and Not even protected!).  Protected methods are allowed. Users of a class must be dependent on its public interface, but a class should not be dependent on its users (Somebody calls my method, and I’m dependent on the other’s behaviour or state). Minimize the number of messages in the protocol of a class (Example: SetMinimumValue(int aValue); SetMaximimumValue(int aValue);, But SetLimits(int minValue, int maxValue)). Implement a minimal public interface which all classes understand (E.g. operations such as copy (deep versus shallow), equality testing, pretty printing, parsing from a ASCII description, etc). Do not put implementation details into the public interface of a class (such as common-code private functions, in Example: Class CBill generates a reference number when the object is created, but not any other time). Do not clutter the public interface of a class with things that users of that class are not able to use or are not interested
in using (Mixture of public and private methods that must be called in a certain order; Methods with parameters, values of which are not available to other classes). Classes should only exhibit nil or export coupling with other classes ( i.e. a class should only use operations in the public interface of another class or have nothing to do with that class; No friends or at least avoid them as much as possible). A class should capture one and only one key abstraction (One simple thing, major responsibility). Keep related data and behavior in one place (Classes methods modify data that belongs to that class). Spin off non-related information into another class (i.e. non-communicating behavior). Be sure the abstraction that you model are classes and not simply the roles objects play (Objects may switch roles runtime; State may indicate role).

Topologies of Object-Oriented Applications: Distribute system intelligence horizontally as uniformly as possible (I.e. the top level classes in a design should share the work uniformly).Do not create god classes/objects in your system (Be very suspicious of an abstraction whose name contains Driver, Manager, System, or Subsystem; Does this name indicate a class that really contains too much responsibilities?). Beware of classes that have many accessor methods defined in their public interface (Many of them imply that related data and behavior are not being kept in one place; Accessor methods access other objects or too much data that is encapsulated in this class). Beware of classes which have too much  non-communicating behavior (methods which operate on a proper subset of the data members of a class; God classes often exhibit lots of noncommunicating behavior). Applications which consist of an object oriented model interacting with a user interface (the model should never be dependent on the interface; The interface should be dependent on the model). Model the real world whenever possible (This heuristic is often violated for reasons of system, intelligence distribution, avoidance of god classes, and the keeping of related data and behavior in one place). Eliminate irrelevant classes from your design and Eliminate classes that are outside the system. Do not turn an operation into a class (Be suspicious of any class whose name is a verb or derived from a verb; Especially those which have only one piece of meaningful behavior (i.e. do not count sets, gets, and prints); Ask if that piece of meaningful behavior needs to be migrated to some existing or undiscovered class). Agent classes are often placed in the analysis model of an application (During design time, many agents are found to be irrelevant and should be removed).

The Relationships Between Classes and Objects: Minimize the number of classes with which another class collaborates (The less connections the better; E.g. Facade design pattern). Minimize the number of message sends between a class and its collaborator (By defining fewer methods, for example). Minimize the amount of collaboration between a class and its collaborator (I.e. the number of different messages sent). Minimize fanout in a class (I.e. the product of the number of messages defined by the class and the messages they send). If a class contains objects of another class (then the containing class should be sending messages to the contained objects, i.e. the containment relationship should always imply a uses relationship). Most of the methods defined on a class should be using most of the data members most of the time. Classes should not contain more objects than a developer can fit in his or her short term memory (A favorite value for this number is six). Distribute system intelligence vertically down narrow and deep  containment hierarchies. When implementing semantic constraints (It is best to implement them in terms of the class definition; Often this will lead to increasing number of classes in which case the constraint must be implemented in the behavior of the class, usually, but not necessarily, in the constructor). When implementing semantic constraints in the constructor of a class (place the constraint test in the constructor as far down a containment hierarchy as the domain allows). The semantic information on which a constraint is based (is best placed in a central third -party object when that information is volatile). The semantic information on which a constraint is based (is best decentralized among the classes involved in the constraint when that information is stable). A class must know what it contains, but it should never know who contains it (Some design patterns break this rule, for example, Chain of Responsibility (used often with Composite)). Objects which share lexical scope (i.e. those contained in the same containing class; should not have uses relationships between them).

The Inheritance Relationship: Inheritance should only be used to model a specialization hierarchy. Derived classes must have knowledge of their base class by definition (but base classes should not know anything about their derived classes; State pattern). All data in a base class should be private (i.e. do not use protected data). Theoretically, inheritance hierarchies should be deep, i.e. the deeper the better; Pragmatically, inheritance hierarchies should be no deeper than an average person can keep in their short term memory (A popular value for this depth is six). All abstract classes must be base classes; All base classes should be abstract classes. Factor the commonality (of data behavior, and/or interface); as high as possible in the inheritance hierarchy. If two or more classes only share common data (no common behavior) (then that common data should be placed in a class which will be contained by each sharing class). If two or more classes have common data and behavior (i.e. methods) (then those classes should each inherit from a common base class which captures those data and methods). If two or more classes only share common interface (i.e. messages, not methods) (then they should inherit from a common base class only if they will be used polymorphically). Explicit case analysis on the type of an object is usually an error (the designer should use polymorphism in most of these cases). Explicit case analysis on the value of an attribute is often an error (The class should be decomposed into an inheritance hierarchy where each value of the attribute is transformed into a derived class). Do not model the dynamic semantics of a class through the use of the inheritance relationship (An attempt to model dynamic semantics with a static semantic relationship will lead to a toggling of types at runtime). Do not turn objects of a class into derived classes of the class (Be very suspicious of any derived class for which there is only one instance). If you think you need to create new classes at runtime (take a step back and realize that what you are trying to create are objects; Now generalize these objects into a class). It should be illegal for a derived class to override a base class method with a NOP method (no-operation), I.e. a method which does nothing. Do not confuse optional containment with the need for inheritance (modelling optional containment with inheritance will lead to increasing number of classes). When building an inheritance hierarchy (try to construct reusable frameworks rather than reusable components).

Multiple Inheritance: If you have an example of multiple inheritance in your design (assume you have made a mistake and prove otherwise). Whenever there is inheritance in an object-oriented design ask yourself two questions 1) Am I a special type of the thing I’m inheriting from? And 2) Is the thing I’m inheriting from part of me?. Whenever you have found a multiple inheritance relationship in a objectoriented design (be sure that no base class is actually a derived class of another base class, i.e. accidental multiple inheritance).

The Association Relationship: When given a choice in an objectoriented design between a containment relationship and an association relationship, choose the containment relationship.

Class Specific Data and Behavior: Do not use global data or functions to perform bookkeeping information on the objects of a class (class variables or methods should be used instead).

Physical Object-Oriented Design: Object-oriented designers should never allow physical design criteria to corrupt their logical designs (However, very often physical design criteria is used in the decision making process at logical design time). Do not change the state of an object without going through its public interface (No public/protected data members, No friends).


Written by garabedyan

May 17, 2009 at 16:15

Posted in Uncategorized

Tagged with , , ,

2 Responses

Subscribe to comments with RSS.

  1. I found your blog on google and read a few of your other posts. I just added you to my Google News Reader. Keep up the good work. Look forward to reading more from you in the future.

    HD Wallpapers

    June 9, 2009 at 17:07

  2. […] Object-Oriented Design Heuristics May 2009 1 comment 3 […]

    2010 review of the blog «

    January 2, 2011 at 12:58

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s