Refactoring - a discipline to write a better code

2,149 views

Published on

Refactoring is not only a way to improve the existing code, but also a discipline to write a better code

  • We wholesle many styles of bodybuilding.They are very comfotable and easy to macth with all kinds of clothes.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Refactoring - a discipline to write a better code

  1. 1. Refactoring – A way to write a better code<br />Andrew Chaa<br />Part 1<br />
  2. 2. Questions, Slides, and Samples<br />Please feel free to ask questions anytime<br />
  3. 3. Outline<br />Code that smells<br />If it stinks, change it (Kent Beck’s Grandma)<br />Way to improve codes<br />
  4. 4. What can you expect?<br />“Clean code that works” - Ron Jeffries<br />Higher quality software<br />Methodical approach to software development<br />
  5. 5. History of Refactoring<br />Kent Beck and War Cunningham in smalltalk<br />Martin Folwer’s famous book of Refactoring.<br />Supported by various tools now such as VS 2005/8, ReSharper, Eclipse …<br />
  6. 6. Bad Smells in Code<br />Duplicated Code<br />Long Method<br />Large Class<br />Long Parameter List<br />Divergent Change<br />Switch Statement<br />Temporary Field<br />And many more…<br />
  7. 7. Composing Methods<br />Extract Method<br />Inline Method<br />Inline Temp<br />Replace Temp with Query<br />Introduce Explaining Variable<br />Split Temporary Variable<br />Remove Assignments to Parameters<br />Replace Method with Method Object<br />Substitute Algorithm<br />
  8. 8. void printOwing() {<br />printBanner();<br /> //print details<br />System.out.println (&quot;name: &quot; + _name);<br />System.out.println (&quot;amount &quot; + getOutstanding());<br />}<br />void printOwing() {<br />printBanner();<br />printDetails(getOutstanding());<br />}<br />void printDetails (double outstanding) {<br />System.out.println (&quot;name: &quot; + _name);<br />System.out.println (&quot;amount &quot; + outstanding);<br />}<br />Extracting Method<br />
  9. 9. Inline Method<br />intgetRating() {<br /> return (moreThanFiveLateDeliveries()) ? 2 : 1;<br />}<br />booleanmoreThanFiveLateDeliveries() {<br /> return _numberOfLateDeliveries &gt; 5;<br />}<br />intgetRating() {<br /> return (_numberOfLateDeliveries &gt; 5) ? 2 : 1;<br />}<br />
  10. 10. Remove Temp<br />double basePrice = anOrder.basePrice();<br />return (basePrice &gt; 1000)<br />return (anOrder.basePrice() &gt; 1000)<br />
  11. 11. Replace Temp with Query<br />double basePrice = _quantity * _itemPrice;<br />if (basePrice &gt; 1000)<br /> return basePrice * 0.95;<br />else<br /> return basePrice * 0.98;<br />if (basePrice() &gt; 1000)<br /> return basePrice() * 0.95;<br />else<br /> return basePrice() * 0.98;<br />...<br />double basePrice() {<br />return _quantity * _itemPrice;<br />}<br />
  12. 12. Introduce Explaining Variable<br />if ( (platform.toUpperCase().indexOf(&quot;MAC&quot;) &gt; -1) &&<br /> (browser.toUpperCase().indexOf(&quot;IE&quot;) &gt; -1) &&<br />wasInitialized() && resize &gt; 0 )<br /> {<br /> // do something<br /> }<br />final booleanisMacOs = platform.toUpperCase().indexOf(&quot;MAC&quot;) &gt; -1;<br />final booleanisIEBrowser = browser.toUpperCase().indexOf(&quot;IE&quot;) &gt; -1;<br />final booleanwasResized = resize &gt; 0;<br />if (isMacOs && isIEBrowser && wasInitialized() && wasResized)<br />{<br /> // do something<br />}<br />
  13. 13. Split Temporary Variable<br />double temp = 2 * (_height + _width);<br />System.out.println (temp);<br />temp = _height * _width;<br />System.out.println (temp);<br />final double perimeter = 2 * (_height + _width);<br />System.out.println (perimeter);<br />final double area = _height * _width;<br />System.out.println (area);<br />
  14. 14. Remove Assignments to Parameters<br />int discount (intinputVal, int quantity, intyearToDate) {<br /> if (inputVal &gt; 50) inputVal -= 2;<br />int discount (intinputVal, int quantity, intyearToDate) {<br />int result = inputVal;<br /> if (inputVal &gt; 50) result -= 2;<br />
  15. 15. Replace Method with Method Object<br />class Order...<br /> double price() {<br /> double primaryBasePrice;<br /> double secondaryBasePrice;<br /> double tertiaryBasePrice;<br /> // long computation;<br /> ...<br /> }<br />
  16. 16. Demo<br />Extract method, amountFor(Rental each)<br />Rename variable: thisAmount -&gt; result<br />Moving Method: to Rental, GetCharge<br />Change the reference: each.GetCharge();<br />Replace Temp with Query: thisAmount<br />Extract Method: frequentRenterPoints<br />Move Method: frequentRenterPoints<br />Remove Temp with Query: GetTotalCharge();<br />Remove Temp with Query: GetTotalFrequentRenterPoints();<br />
  17. 17. Substitute Algorithm<br />String foundPerson(String[] people){<br /> for (inti = 0; i &lt; people.length; i++) {<br /> if (people[i].equals (&quot;Don&quot;)) { return &quot;Don“;}<br /> if (people[i].equals (&quot;John&quot;)) { return &quot;John“;}<br /> if (people[i].equals (&quot;Kent&quot;)) {return &quot;Kent“;}<br /> }<br /> return &quot;&quot;;<br />}<br />String foundPerson(String[] people){<br /> List candidates = Arrays.asList(new String[] {&quot;Don&quot;, &quot;John&quot;, &quot;Kent&quot;});<br /> for (inti=0; i&lt;people.length; i++)<br /> if (candidates.contains(people[i]))<br /> return people[i];<br /> return &quot;&quot;;<br />}<br />
  18. 18. Resources<br />Refactoring catalogs: http://www.refactoring.com/catalog/index.html<br />Podcast about Refactoringhttp://www.se-radio.net/index.php?post_id=172668http://www.se-radio.net/index.php?post_id=205444<br />Refactoring (book)http://www.amazon.co.uk/Refactoring-Improving-Design-Existing-Technology/dp/0201485672/ref=sr_1_1?ie=UTF8&s=books&qid=1246371771&sr=8-1<br />

×