Preserve Whole Object

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
m (Reverted edits by Ebybymic (Talk); changed back to last version by Aidan Bebbington)
 
Line 1: Line 1:
>== Motivation ==
+
== Motivation ==
  
 
When an object passes several data values from a single object as parameters in a method call things can get messy. The problem is that if the object calling the method needs new data values later, then all calls to this method will need to be changed to accommodate this. This problem can be avoided by passing the whole object that you require data from. The called object can then ask for whatever it wants from the whole object. Preserve Whole Object also often makes code more readable.
 
When an object passes several data values from a single object as parameters in a method call things can get messy. The problem is that if the object calling the method needs new data values later, then all calls to this method will need to be changed to accommodate this. This problem can be avoided by passing the whole object that you require data from. The called object can then ask for whatever it wants from the whole object. Preserve Whole Object also often makes code more readable.
  
 
Note that this violates [[Tell, don't ask]]
 
Note that this violates [[Tell, don't ask]]
 
----
 
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 
----
 
=[http://ywejoxecy.co.cc UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY]=
 
----
 
=[http://ywejoxecy.co.cc CLICK HERE]=
 
----
 
</div>
 
  
 
== Example ==
 
== Example ==

Latest revision as of 03:23, 25 November 2010

Motivation

When an object passes several data values from a single object as parameters in a method call things can get messy. The problem is that if the object calling the method needs new data values later, then all calls to this method will need to be changed to accommodate this. This problem can be avoided by passing the whole object that you require data from. The called object can then ask for whatever it wants from the whole object. Preserve Whole Object also often makes code more readable.

Note that this violates Tell, don't ask

Example

You need to get several values from an object and pass these values as parameters when calling a method. Send the whole object instead!

frontLeftWheel = jag.getFrontLeft();
frontRightWheel = jag.getFrontRight();
frontWheels = setFrontWheels(frontLeftWheel, frontRightWheel);

turns into a tidier...

frontWheels = setFrontWheels(jag);

See also

Personal tools