0
Refactoring: A Brief Introduction
Refactoring <ul><li>Refactoring (noun) : a change made to the internal structure of software to make it easier to understa...
An Example with NUnit #region Using using NUnit.Framework; #endregion namespace com.sds.prime { [TestFixture] public class...
An Example with NUnit <ul><li>using System; </li></ul><ul><li>namespace com.sds.prime </li></ul><ul><li>{ </li></ul><ul><l...
Run Test with NUnit plugin
Run Test with NUnit plugin <ul><li>------ Test started: Assembly: PrimeNumber.exe ------ </li></ul><ul><li>1 succeeded, 0 ...
An Example with NUnit #region Using using NUnit.Framework; #endregion namespace com.sds.prime { [TestFixture] public class...
An Example with NUnit <ul><li>using System; </li></ul><ul><li>namespace com.sds.prime </li></ul><ul><li>{ </li></ul><ul><l...
Run Test with NUnit plugin
Run Test with NUnit plugin <ul><li>------ Test started: Assembly: PrimeNumber.exe ------ </li></ul><ul><li>1 succeeded, 0 ...
An Example with NUnit #region Using using NUnit.Framework; #endregion namespace com.sds.prime { [TestFixture] public class...
An Example with NUnit <ul><li>using System; </li></ul><ul><li>namespace com.sds.prime </li></ul><ul><li>{ </li></ul><ul><l...
Run Test with NUnit plugin
Run Test with NUnit plugin <ul><li>------ Test started: Assembly: PrimeNumber.exe ------ </li></ul><ul><li>1 succeeded, 0 ...
An Example with NUnit #region Using using NUnit.Framework; #endregion namespace com.sds.prime { [TestFixture] public class...
An Example with NUnit <ul><li>public int getNext(int a_numberOfPrimes) </li></ul><ul><li>{ </li></ul><ul><li>if (a_numberO...
An Example with NUnit <ul><li>private bool isPrime(int a_potentialPrime) </li></ul><ul><li>{ </li></ul><ul><li>bool isPrim...
Run Test with NUnit plugin
Run Test with NUnit plugin <ul><li>------ Test started: Assembly: PrimeNumber.exe ------ </li></ul><ul><li>1 succeeded, 0 ...
An Example with NUnit <ul><li>public int getNext(int a_numberOfPrimes) </li></ul><ul><li>{ </li></ul><ul><li>if (a_numberO...
An Example with NUnit <ul><li>private bool isPrime(int a_potentialPrime) </li></ul><ul><li>{ </li></ul><ul><li>bool isPrim...
Run Test with NUnit plugin
Run Test with NUnit plugin <ul><li>------ Test started: Assembly: PrimeNumber.exe ------ </li></ul><ul><li>1 succeeded, 0 ...
Testing Boundary Conditions <ul><li>As developers, we generally code with the core of the requirement in mind.  We test th...
Refactoring <ul><li>Now back to Refactoring. </li></ul><ul><li>An Example … </li></ul>
Smells <ul><li>A  smell  is a warning sign about a potential problem. </li></ul>
Code Smells <ul><li>Common smells in our code </li></ul><ul><li>Comments </li></ul><ul><li>Long Methods </li></ul><ul><li>...
Smell:Comments <ul><li>When a comment explains a block of code, use  Extract Method  to pull the block out into a properly...
Smell:Long Methods <ul><li>Use  Extract Method  to break up the method into smaller pieces. </li></ul><ul><li>Payoff : Imp...
Smell:Large Class <ul><li>Use  Extract Class  if you can identify a new class that has part of this class’s responsibiliti...
Smell:Long Parameter List <ul><li>If the parameter value can be obtained from another object this one already knows use  R...
Smell:Inappropriate Intimacy <ul><li>If two independent classes are entangled, use  Move Method  and  Move Field  to put t...
Design Patterns <ul><li>Design patterns are used in Object Oriented (OO) programming to create structured, adaptive classe...
Template Pattern <ul><li>MyClass::someMethod() { </li></ul><ul><li>aaa aaa aaa aaa </li></ul><ul><li>bb bb bb bb bb  </li>...
Template Pattern <ul><li>Using copy and paste to create new code from existing code results in redundancies </li></ul>aaa ...
Template Pattern <ul><li>MyClass::someMethod() { </li></ul><ul><li>methodForStep1() </li></ul><ul><li>cccc cccc cccc  </li...
Template Pattern <ul><li>BaseClass::someMethod () { </li></ul><ul><li>methodForStep1() </li></ul><ul><li>cccc cccc cccc  <...
Prime Number Template <ul><li>We could easily abstract our Prime class using the Template pattern so that we can handle di...
Resources <ul><li>“ Test Patterns Explained” by Alan Shalloway, James R. Trott </li></ul><ul><li>“ Refactoring” by Martin ...
Upcoming SlideShare
Loading in...5
×

Refactoring

498

Published on

Published in: Technology, Design
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
498
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Refactoring"

  1. 1. Refactoring: A Brief Introduction
  2. 2. Refactoring <ul><li>Refactoring (noun) : a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior. </li></ul><ul><li>Refactor (verb) : to restructure existing software by applying a series of refactorings without changing its observable behavior. </li></ul>
  3. 3. An Example with NUnit #region Using using NUnit.Framework; #endregion namespace com.sds.prime { [TestFixture] public class PrimeTest { [Test] public void testMethod() { Prime prime = new Prime(); } } }
  4. 4. An Example with NUnit <ul><li>using System; </li></ul><ul><li>namespace com.sds.prime </li></ul><ul><li>{ </li></ul><ul><li>public class Prime </li></ul><ul><li>{ </li></ul><ul><li>public Prime() </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  5. 5. Run Test with NUnit plugin
  6. 6. Run Test with NUnit plugin <ul><li>------ Test started: Assembly: PrimeNumber.exe ------ </li></ul><ul><li>1 succeeded, 0 failed, 0 skipped, took 0.05 seconds. </li></ul><ul><li>---------------------- Done ---------------------- </li></ul>
  7. 7. An Example with NUnit #region Using using NUnit.Framework; #endregion namespace com.sds.prime { [TestFixture] public class PrimeTest { [Test] public void testMethod() { Prime prime = new Prime(); Assert.IsTrue(prime.getNext(1) == 1); } } }
  8. 8. An Example with NUnit <ul><li>using System; </li></ul><ul><li>namespace com.sds.prime </li></ul><ul><li>{ </li></ul><ul><li>public class Prime </li></ul><ul><li>{ </li></ul><ul><li>public Prime() </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul><ul><li>public int getNext(int a_numberOfPrimes) </li></ul><ul><li>{ </li></ul><ul><li>return 1; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  9. 9. Run Test with NUnit plugin
  10. 10. Run Test with NUnit plugin <ul><li>------ Test started: Assembly: PrimeNumber.exe ------ </li></ul><ul><li>1 succeeded, 0 failed, 0 skipped, took 0.05 seconds. </li></ul><ul><li>---------------------- Done ---------------------- </li></ul>
  11. 11. An Example with NUnit #region Using using NUnit.Framework; #endregion namespace com.sds.prime { [TestFixture] public class PrimeTest { [Test] public void testMethod() { Prime prime = new Prime(); Assert.IsTrue(prime.getNext(1) == 1); Assert.IsTrue(prime.getNext(2) == 2); } } }
  12. 12. An Example with NUnit <ul><li>using System; </li></ul><ul><li>namespace com.sds.prime </li></ul><ul><li>{ </li></ul><ul><li>public class Prime </li></ul><ul><li>{ </li></ul><ul><li>public Prime() </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul><ul><li>public int getNext(int a_numberOfPrimes) </li></ul><ul><li>{ </li></ul><ul><li>if (a_numberOfPrimes== 1) return 1; </li></ul><ul><li>if (a_numberOfPrimes == 2) return 2; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  13. 13. Run Test with NUnit plugin
  14. 14. Run Test with NUnit plugin <ul><li>------ Test started: Assembly: PrimeNumber.exe ------ </li></ul><ul><li>1 succeeded, 0 failed, 0 skipped, took 0.05 seconds. </li></ul><ul><li>---------------------- Done ---------------------- </li></ul>
  15. 15. An Example with NUnit #region Using using NUnit.Framework; #endregion namespace com.sds.prime { [TestFixture] public class PrimeTest { [Test] public void testMethod() { Prime prime = new Prime(); Assert.IsTrue(prime.getNext(1) == 1); Assert.IsTrue(prime.getNext(2) == 2); Assert.IsTrue(prime.getNext(10) == 23); } } }
  16. 16. An Example with NUnit <ul><li>public int getNext(int a_numberOfPrimes) </li></ul><ul><li>{ </li></ul><ul><li>if (a_numberOfPrimes== 1) return 1; </li></ul><ul><li>if (a_numberOfPrimes == 2) return 2; </li></ul><ul><li>int primeCounter = 2; </li></ul><ul><li>for (int candidate=3; candidate < 1000; candidate++) </li></ul><ul><li>{ </li></ul><ul><li>if (isPrime(candidate)) </li></ul><ul><li>{ </li></ul><ul><li>primeCounter++; </li></ul><ul><li>if (primeCounter == a_numberOfPrimes) </li></ul><ul><li>return candidate; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
  17. 17. An Example with NUnit <ul><li>private bool isPrime(int a_potentialPrime) </li></ul><ul><li>{ </li></ul><ul><li>bool isPrime = true; </li></ul><ul><li> </li></ul><ul><li>for (int potentialDivisor = 2; </li></ul><ul><li>potentialDivisor < a_potentialPrime; </li></ul><ul><li>potentialDivisor++) </li></ul><ul><li>{ </li></ul><ul><li>if (a_potentialPrime % potentialDivisor == 0) </li></ul><ul><li>{ </li></ul><ul><li>isPrime = false; </li></ul><ul><li>break; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>return isPrime; </li></ul><ul><li>} </li></ul>
  18. 18. Run Test with NUnit plugin
  19. 19. Run Test with NUnit plugin <ul><li>------ Test started: Assembly: PrimeNumber.exe ------ </li></ul><ul><li>1 succeeded, 0 failed, 0 skipped, took 0.05 seconds. </li></ul><ul><li>---------------------- Done ---------------------- </li></ul>
  20. 20. An Example with NUnit <ul><li>public int getNext(int a_numberOfPrimes) </li></ul><ul><li>{ </li></ul><ul><li>if (a_numberOfPrimes== 1) return 1; </li></ul><ul><li>if (a_numberOfPrimes == 2) return 2; </li></ul><ul><li>int primeCounter = 2; </li></ul><ul><li>for (int candidate=3; candidate < 1000; candidate+2) </li></ul><ul><li>{ </li></ul><ul><li>if (isPrime(candidate)) </li></ul><ul><li>{ </li></ul><ul><li>primeCounter++; </li></ul><ul><li>if (primeCounter == a_numberOfPrimes) </li></ul><ul><li>return candidate; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
  21. 21. An Example with NUnit <ul><li>private bool isPrime(int a_potentialPrime) </li></ul><ul><li>{ </li></ul><ul><li>bool isPrime = true; </li></ul><ul><li>int halfWay = a_potentialPrime / 2; </li></ul><ul><li>for (int potentialDivisor = 2; </li></ul><ul><li>potentialDivisor < halfWay; </li></ul><ul><li>potentialDivisor++) </li></ul><ul><li>{ </li></ul><ul><li>if (a_potentialPrime % potentialDivisor == 0) </li></ul><ul><li>{ </li></ul><ul><li>isPrime = false; </li></ul><ul><li>break; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>return isPrime; </li></ul><ul><li>} </li></ul>
  22. 22. Run Test with NUnit plugin
  23. 23. Run Test with NUnit plugin <ul><li>------ Test started: Assembly: PrimeNumber.exe ------ </li></ul><ul><li>1 succeeded, 0 failed, 0 skipped, took 0.05 seconds. </li></ul><ul><li>---------------------- Done ---------------------- </li></ul>
  24. 24. Testing Boundary Conditions <ul><li>As developers, we generally code with the core of the requirement in mind. We test the ‘common’ scenarios and mark it complete. However, most of our code breaks around our boundary conditions. </li></ul>
  25. 25. Refactoring <ul><li>Now back to Refactoring. </li></ul><ul><li>An Example … </li></ul>
  26. 26. Smells <ul><li>A smell is a warning sign about a potential problem. </li></ul>
  27. 27. Code Smells <ul><li>Common smells in our code </li></ul><ul><li>Comments </li></ul><ul><li>Long Methods </li></ul><ul><li>Large Class </li></ul><ul><li>Long Parameter List </li></ul><ul><li>Inappropriate Intimacy </li></ul><ul><li>Duplicated Code </li></ul><ul><li>Conditional Complexity </li></ul><ul><li>A list of descriptions can be found here </li></ul><ul><li>http://www. codinghorror .com/ blog /archives/000589.html </li></ul>
  28. 28. Smell:Comments <ul><li>When a comment explains a block of code, use Extract Method to pull the block out into a properly named method. </li></ul><ul><li>When a comment explains what a method does, use Rename Method to give the method a more descriptive name. </li></ul><ul><li>Payoff : Improves communication, may expose duplication. </li></ul>
  29. 29. Smell:Long Methods <ul><li>Use Extract Method to break up the method into smaller pieces. </li></ul><ul><li>Payoff : Improves communication, may expose duplication, often helps new classes and abstractions emerge. </li></ul>
  30. 30. Smell:Large Class <ul><li>Use Extract Class if you can identify a new class that has part of this class’s responsibilities. </li></ul><ul><li>Use Extract Subclass if you can divide responsibilities between the class and a new subclass. </li></ul><ul><li>Use Extract Interface if you can identify subsets of features that clients use. </li></ul><ul><li>Payoff : Improves communication, may expose duplication. </li></ul>
  31. 31. Smell:Long Parameter List <ul><li>If the parameter value can be obtained from another object this one already knows use Replace Parameter with Method . </li></ul><ul><li>If the data is not from one logical object, you may be able to group them using Introduce Parameter Object . </li></ul><ul><li>Payoff : Improves communication, may expose duplication, often reduces size. </li></ul>
  32. 32. Smell:Inappropriate Intimacy <ul><li>If two independent classes are entangled, use Move Method and Move Field to put the right pieces in on the right class. </li></ul><ul><li>If the tangled part seems to be a missing class, use Extract Class and Hide Delegate to introduce the new class. </li></ul><ul><li>Payoff : Reduces duplication, often improves communication, may reduce size. </li></ul>
  33. 33. Design Patterns <ul><li>Design patterns are used in Object Oriented (OO) programming to create structured, adaptive classes. Refactoring ultimately results in some kind of design pattern. </li></ul>
  34. 34. Template Pattern <ul><li>MyClass::someMethod() { </li></ul><ul><li>aaa aaa aaa aaa </li></ul><ul><li>bb bb bb bb bb </li></ul><ul><li>cccc cccc cccc </li></ul><ul><li>d d d d d d d d d </li></ul><ul><li>eee eee eee eee </li></ul><ul><li>fffff ff fffff ff </li></ul><ul><li>ggg gggg ggg </li></ul><ul><li>h hh h hh h hh h </li></ul><ul><li>iiii iiii iiii iiii </li></ul><ul><li>} </li></ul>Design Patterns Explained , ‘ Chapter 19 : The Template Method Pattern’ , by Alan Shalloway and James R. Trott
  35. 35. Template Pattern <ul><li>Using copy and paste to create new code from existing code results in redundancies </li></ul>aaa aaa aaa aaa bb bb bb bb bb cccc cccc cccc d d d d d d d d d eee eee eee eee fffff ff fffff ff ggg gggg ggg h hh h hh h hh h iiii iiii iiii iiii AAA A AAAA A XXX XX XX X BB BB B cccc cccc cccc DDD D DDD D EE EEEE E E fffff ff fffff ff GGG G G GGGG HHH HHH HH H iiii iiii iiii iiii Design Patterns Explained , ‘ Chapter 19 : The Template Method Pattern’ , by Alan Shalloway and James R. Trott
  36. 36. Template Pattern <ul><li>MyClass::someMethod() { </li></ul><ul><li>methodForStep1() </li></ul><ul><li>cccc cccc cccc </li></ul><ul><li>methodForStep3() </li></ul><ul><li>fffff ff fffff ff </li></ul><ul><li>methodForStep5() </li></ul><ul><li>iiii iiii iiii iiii </li></ul><ul><li>} </li></ul>Design Patterns Explained , ‘ Chapter 19 : The Template Method Pattern’ , by Alan Shalloway and James R. Trott methodForStep1() { aaa aaa aaa aaa bb bb bb bb bb } methodForStep3() { d d d d d d d d d eee eee eee eee } methodForStep5() { ggg gggg ggg h hh h hh h hh h }
  37. 37. Template Pattern <ul><li>BaseClass::someMethod () { </li></ul><ul><li>methodForStep1() </li></ul><ul><li>cccc cccc cccc </li></ul><ul><li>methodForStep3() </li></ul><ul><li>fffff ff fffff ff </li></ul><ul><li>methodForStep5() </li></ul><ul><li>iiii iiii iiii iiii </li></ul><ul><li>} </li></ul>Design Patterns Explained , ‘ Chapter 19 : The Template Method Pattern’ , by Alan Shalloway and James R. Trott MyClass { methodForStep1() { aaa aaa aaa aaa bb bb bb bb bb } methodForStep3() { d d d d d d d d d eee eee eee eee } methodForStep5() { ggg gggg ggg h hh h hh h hh h } } MySecondClass { methodForStep1() { AAA A AAAA A XXX XX XX X BB BB B } methodForStep3() { DDD D DDD D EE EEEE E E } methodForStep5() { GGG G G GGGG HHH HHH HH H } }
  38. 38. Prime Number Template <ul><li>We could easily abstract our Prime class using the Template pattern so that we can handle different algorithms of the Prime number generator. </li></ul>
  39. 39. Resources <ul><li>“ Test Patterns Explained” by Alan Shalloway, James R. Trott </li></ul><ul><li>“ Refactoring” by Martin Fowler, www.refactoring.com </li></ul><ul><li>“ Pragmatic Unit Testing in C# with Nunit” by Andrew Hunt, David Thomas </li></ul><ul><li>“ The Pragmatic Programmer” by Andrew Hunt, David Thomas </li></ul><ul><li>Code Smells, http://www.codinghorror.com/blog/archives/000589.html </li></ul><ul><li>Xunit Frameworks, http://www.opensourcetesting.org/unit_misc.php </li></ul><ul><li>TestDriven VS Plugin, http://www.testdriven.net/ </li></ul><ul><li>[email_address] </li></ul><ul><li>www.sds-consulting.com </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.

×