0
Move Accumulation to Collecting Parameter Refactoring to Patterns By Joshua Kerievsky
<ul><li>Problem </li></ul><ul><li>You have a single bulky method that accumulates information to a local variable.   </li>...
Collecting Parameter  Pattern <ul><li>Smalltalk Best Practice Patterns  </li></ul><ul><ul><li>by Kent Beck. </li></ul></ul...
Before & After
Motivation <ul><li>To decompose large method to Composed Method </li></ul><ul><li>Result is accumulated in a data structur...
Benefits  <ul><li>Helps transform bulky methods into smaller, simpler, easier-to-read methods. </li></ul><ul><li>Can make ...
Starting Point of Code <ul><li>class TagNode...  </li></ul><ul><ul><li>public String toString() {  </li></ul></ul><ul><ul>...
Step 1 <ul><li>class TagNode...  </li></ul><ul><ul><li>public String toString() {  </li></ul></ul><ul><ul><li>StringBuffer...
Step 2 <ul><li>result += &quot;<&quot; + tagName + &quot; &quot; + attributes + &quot;>&quot;;  </li></ul><ul><li>is extra...
Step 3A <ul><li>public String toString() </li></ul><ul><li>...  </li></ul><ul><li>Iterator it = children.iterator();  </li...
Step 3B <ul><li>public String toString() {  </li></ul><ul><li>StringBuffer result = new StringBuffer(&quot;&quot;); </li><...
Step 3C <ul><li>private void appendContentsTo(StringBuffer result) {  </li></ul><ul><li>writeOpenTagTo(result);  </li></ul...
Step 4A <ul><li>private void appendContentsTo(StringBuffer result) {  </li></ul><ul><li>writeOpenTagTo(result); </li></ul>...
Step 4B <ul><li>private void appendContentsTo(StringBuffer result) {  </li></ul><ul><li>writeOpenTagTo(result); </li></ul>...
Generic Collecting Parameter <ul><li>     public static TResult Aggregate<TSource, TAccumulate, TResult>( </li></ul><ul><l...
Upcoming SlideShare
Loading in...5
×

Move Accumulation To Collecting Parameter

1,817

Published on

Published in: Technology, Career
1 Comment
1 Like
Statistics
Notes
  • Slide 15 is completely off the track: func is different from Aggregate.apply(). The gist of this whole presentation is that you write a function appendContentsTo and you use it for BOTH the parent and all its children!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
1,817
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Move Accumulation To Collecting Parameter"

  1. 1. Move Accumulation to Collecting Parameter Refactoring to Patterns By Joshua Kerievsky
  2. 2. <ul><li>Problem </li></ul><ul><li>You have a single bulky method that accumulates information to a local variable. </li></ul><ul><li>Solution </li></ul><ul><li>Accumulate results to a Collecting Parameter that gets passed to extracted methods. </li></ul>
  3. 3. Collecting Parameter Pattern <ul><li>Smalltalk Best Practice Patterns </li></ul><ul><ul><li>by Kent Beck. </li></ul></ul><ul><li>Thinking in C++ Vol 2 - Practical Programming </li></ul><ul><ul><li>by Bruce Eckel </li></ul></ul>
  4. 4. Before & After
  5. 5. Motivation <ul><li>To decompose large method to Composed Method </li></ul><ul><li>Result is accumulated in a data structure, rather than being combined at the end. </li></ul>
  6. 6. Benefits <ul><li>Helps transform bulky methods into smaller, simpler, easier-to-read methods. </li></ul><ul><li>Can make resulting code run faster. </li></ul>
  7. 7. Starting Point of Code <ul><li>class TagNode... </li></ul><ul><ul><li>public String toString() { </li></ul></ul><ul><ul><li>String result = new String(); </li></ul></ul><ul><ul><li>result += &quot;<&quot; + tagName + &quot; &quot; + attributes + &quot;>&quot;; </li></ul></ul><ul><ul><li>Iterator it = children.iterator(); </li></ul></ul><ul><ul><li>while (it.hasNext()) { </li></ul></ul><ul><ul><li>TagNode node = (TagNode)it.next(); </li></ul></ul><ul><ul><li>result += node.toString(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>if (!value.equals(&quot;&quot;)) </li></ul></ul><ul><ul><li>result += value; </li></ul></ul><ul><ul><li>result += &quot;</&quot; + tagName + &quot;>&quot;; return result; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  8. 8. Step 1 <ul><li>class TagNode... </li></ul><ul><ul><li>public String toString() { </li></ul></ul><ul><ul><li>StringBuffer result = new StringBuffer(&quot;&quot;); </li></ul></ul><ul><ul><li>result += &quot;<&quot; + tagName + &quot; &quot; + attributes + &quot;>&quot;; </li></ul></ul><ul><ul><li>Iterator it = children.iterator(); </li></ul></ul><ul><ul><li>while (it.hasNext()) { </li></ul></ul><ul><ul><li>TagNode node = (TagNode)it.next(); </li></ul></ul><ul><ul><li>result += node.toString(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>if (!value.equals(&quot;&quot;)) </li></ul></ul><ul><ul><li>result += value; </li></ul></ul><ul><ul><li>result += &quot;</&quot; + tagName + &quot;>&quot;; return result; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  9. 9. Step 2 <ul><li>result += &quot;<&quot; + tagName + &quot; &quot; + attributes + &quot;>&quot;; </li></ul><ul><li>is extracted to: private void writeOpenTagTo(StringBuffer result) { result.append(&quot;<&quot;); </li></ul><ul><li>result.append(name); </li></ul><ul><li>result.append(attributes.toString()); </li></ul><ul><li>result.append(&quot;>&quot;); </li></ul><ul><li>} </li></ul><ul><li>… </li></ul><ul><li>StringBuffer result = new StringBuffer(&quot;&quot;); </li></ul><ul><li>writeOpenTagTo(result); </li></ul><ul><li>... </li></ul>
  10. 10. Step 3A <ul><li>public String toString() </li></ul><ul><li>... </li></ul><ul><li>Iterator it = children.iterator(); </li></ul><ul><li>while (it.hasNext()) { </li></ul><ul><ul><li>TagNode node = (TagNode)it.next(); </li></ul></ul><ul><ul><li>result += node.toString(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>… . </li></ul><ul><li>public String toString() </li></ul><ul><li>... </li></ul><ul><li>Iterator it = children.iterator(); </li></ul><ul><li>while (it.hasNext()) { </li></ul><ul><ul><li>TagNode node = (TagNode)it.next(); </li></ul></ul><ul><ul><li>node.toString(result); // can't do this because toString() doesn't take arguments. </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>… . </li></ul>
  11. 11. Step 3B <ul><li>public String toString() { </li></ul><ul><li>StringBuffer result = new StringBuffer(&quot;&quot;); </li></ul><ul><li>appendContentsTo(result); </li></ul><ul><li>return result.toString(); </li></ul><ul><li>} </li></ul><ul><li>private void appendContentsTo(StringBuffer result) { </li></ul><ul><li>writeOpenTagTo(result); </li></ul><ul><li>... </li></ul><ul><li>} </li></ul>
  12. 12. Step 3C <ul><li>private void appendContentsTo(StringBuffer result) { </li></ul><ul><li>writeOpenTagTo(result); </li></ul><ul><li>writeChildrenTo(result); </li></ul><ul><li>... </li></ul><ul><li>} </li></ul><ul><li>private void writeChildrenTo(StringBuffer result) { </li></ul><ul><li>Iterator it = children.iterator(); </li></ul><ul><li>while (it.hasNext()) { </li></ul><ul><li>TagNode node = (TagNode)it.next(); </li></ul><ul><li>node.appendContentsTo(result); // now recursive call will work </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  13. 13. Step 4A <ul><li>private void appendContentsTo(StringBuffer result) { </li></ul><ul><li>writeOpenTagTo(result); </li></ul><ul><li>writeChildrenTo(result); </li></ul><ul><li>if (!value.equals(&quot;&quot;)) </li></ul><ul><li>result += value; </li></ul><ul><li>result += &quot;</&quot; + tagName + &quot;>&quot;; </li></ul><ul><li>} </li></ul>
  14. 14. Step 4B <ul><li>private void appendContentsTo(StringBuffer result) { </li></ul><ul><li>writeOpenTagTo(result); </li></ul><ul><li>writeChildrenTo(result); </li></ul><ul><li>writeValueTo(result); </li></ul><ul><li>writeEndTagTo(result); </li></ul><ul><li>} </li></ul><ul><li>private void writeValueTo(StringBuffer result) { </li></ul><ul><li>if (!value.equals(&quot;&quot;)) </li></ul><ul><li>result.append(value); </li></ul><ul><li>} </li></ul><ul><li>private void writeEndTagTo(StringBuffer result) { </li></ul><ul><li>result.append(&quot;</&quot;); </li></ul><ul><li>result.append(name); </li></ul><ul><li>result.append(&quot;>&quot;); </li></ul><ul><li>} </li></ul>
  15. 15. Generic Collecting Parameter <ul><li>    public static TResult Aggregate<TSource, TAccumulate, TResult>( </li></ul><ul><li>        this IEnumerable<TSource> source, </li></ul><ul><li>        TAccumulate seed, </li></ul><ul><li>        Func<TAccumulate, TSource, TAccumulate> func, </li></ul><ul><li>        Func<TAccumulate, TResult> resultSelector </li></ul><ul><li>    ) </li></ul><ul><li>    { </li></ul><ul><li>        TAccumulate accumulator = seed; </li></ul><ul><li>        foreach (TSource elem in source) </li></ul><ul><li>        { </li></ul><ul><li>            accumulator = func(accumulator, elem); </li></ul><ul><li>        } </li></ul><ul><li>        return resultSelector(accumulator); </li></ul><ul><li>    } </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×