Message chain smell

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
(add Message chain smell)
 
Line 1: Line 1:
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:
+
The message chain smell arises when a particular class is highly coupled to other classes in 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();
+
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.
 
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.
+
However, sometimes delegations are needed, and thus delegation chains with a couple of links are often considered to be harmless. The argument as to how many links a chain can reasonably have is often related to other factors. Consider the other design maxims mentioned below for more guidance related to designs with highly coupled chained delegations.
  
 
== Refactoring Techniques ==  
 
== Refactoring Techniques ==  

Revision as of 01:47, 17 July 2009

The message chain smell arises when a particular class is highly coupled to other classes in 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 delegation chains with a couple of links are often considered to be harmless. The argument as to how many links a chain can reasonably have is often related to other factors. Consider the other design maxims mentioned below for more guidance related to designs with highly coupled chained delegations.

Refactoring Techniques

See Also

Personal tools