Message chain smell

From CSSEMediaWiki
Revision as of 12:49, 6 October 2008 by Johannes Pagwiwoko (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The message chains smell is a smell where a particular class is highly coupled to other classes in a chain-like delegations. To illustrate this smell, suppose we have Class A who needs data from Class E. To retrieve this data, object A firstly needs to retrieve object E from object D from object C from object B. Thus we have something like this:

 a.getB().getC().getD().getE().getTheData();

The problem which lies here is that as A tried to access the data at Class E, A becomes unnecessarily coupled to class B, C, D along the way; when it needs only to get the data from E.

However, sometimes delegations are needed, and thus, a couple number of chaining is considered to be somewhat harmless. It is arguable as to how many chains is too many. Consider other design maxims mentioned below before proceeding with implementing a highly coupled, chained delegations.

Refactoring Techniques

See Also

Personal tools