1 / 33

Refactoring

Refactoring. Advanced Programming in Java. Mehdi Einali. Tale of Messy code. Once upon a time …. A team start a project Project got many attention and team has to add new features in short time Programmer with overtime task: “I will fix this later”. After a while.

lyonse
Download Presentation

Refactoring

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Refactoring AdvancedProgramming in Java MehdiEinali

  2. Tale of Messy code

  3. Once upon a time … • A team start a project • Project got many attention and team has to add new features in short time • Programmer with overtime task: “I will fix this later”

  4. After a while • Changes slowed down by messy code • As productivity decreases more programmer assigned to project • New programmer with messy code results in more messy code

  5. rebellion • Eventually the team rebels. • A new tiger team is selected • Best technologies has been chosen • Now the two teams are in a race • This race can go on for a very long time • Tiger team is now under pleasure of comparison with old low feature but working version • Messy code again and again once upon a time

  6. refactoring

  7. Refactoring • A disciplined way to restructure code in order to improve code quality without changing its behavior • A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.

  8. Refactoring • Refactoring is the process of changing a software system • In such a way that it does not alter the external behavior of the code • But improves its internal structure • It is a disciplined way to clean up code • It minimizes the chances of introducing bugs • When you refactor, you are improving the design of the code after it has been written.

  9. Refactoring • By continuously improving the design of code, we make it easier and easier to work with Joshua Kerievsky, Refactoring to Patterns

  10. Example • Duplicate Code • What are the drawbacks? • What is the solution? • Refactoring: • Finding a “Bad Smell” • Changing the code to remove the bad smell • Some well-known bad smells are reported

  11. Bad Smell • A bad smell in code • Any symptom in the source code that possibly indicates a deeper problem. • The term is coined by Kent Beck.

  12. Bad Smells • Duplicated Code • Long Method • Large Class • Long Parameter List • Divergent Change • …

  13. Refactoring Techniques • Extract Method • Move • Method • Variable • Class • Extract Class • Rename • Method • Variable • Class • Pull Up • Push Down

  14. IDE Support • Refactoring techniques are widely supported by IDEs

  15. The Two Hats • Kent Beck's metaphor of two hats • Divide your time between two distinct activities • adding function • refactoring

  16. Why Should I Refactor? • Improves the Design of Software • Makes Software Easier to Understand • Helps You Find Bugs • Helps You Program Faster • Refactoring makes your code more maintainable

  17. When Should You Refactor? • The Rule of Three: • Refactor When You Add Function • Refactor When You Need to Fix a Bug • Refactor As You Do a Code Review

  18. Scanner s = new Scanner(System.in); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); int a1 = s.nextInt(); System.out.print("Enter the length: "); int a2 = s.nextInt(); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); int b1 = s.nextInt(); System.out.print("Enter the length: "); int b2 = s.nextInt(); int x = a1*a2; int y = b1*b2; if(x == y) System.out.println("Equal"); Find bad smells! Refactor the Code!

  19. Scanner scanner = new Scanner(System.in); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); int width1 = scanner.nextInt(); System.out.print("Enter the length: "); int length1 = scanner.nextInt(); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); int width2 = scanner.nextInt(); System.out.print("Enter the length: "); int length2 = scanner.nextInt(); int area1 = width1*length1; int area2 = width2*length2; if(area1 == area2) System.out.println("Equal"); Rename…

  20. class Rectangle{ privateintlength , width; publicintgetLength() { returnlength; } publicvoidsetLength(int length) { this.length = length; } publicintgetWidth() { returnwidth; } publicvoidsetWidth(int width) { this.width = width; } public Rectangle(int length, int width) { this.length = length; this.width = width; } } Extract Class…

  21. Scanner scanner = new Scanner(System.in); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); int width = scanner.nextInt(); System.out.print("Enter the length: "); int length = scanner.nextInt(); Rectangle rectangle1 = new Rectangle(length, width); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); width = scanner.nextInt(); System.out.print("Enter the length: "); length = scanner.nextInt(); Rectangle rectangle2 = new Rectangle(length, width); int area1 = rectangle1.getWidth()*rectangle1.getLength(); int area2 = rectangle2.getWidth()*rectangle2.getLength(); if(area1 == area2) System.out.println("Equal");

  22. class Rectangle{ ... publicint area(){ returnlength * width; } } … int area1 = rectangle1.area(); int area2 = rectangle2.area(); Extract Method…

  23. privatestatic Rectangle readRectangle(Scanner scanner) { int width; int length; System.out.println("Rectangle Info."); System.out.print("Enter the width: "); width = scanner.nextInt(); System.out.print("Enter the length: "); length = scanner.nextInt(); Rectangle rectangle2 = new Rectangle(length, width); return rectangle2; } Extract Method…

  24. Refactored Code Scanner scanner = new Scanner(System.in); Rectangle rectangle1 = readRectangle(scanner); Rectangle rectangle2 = readRectangle(scanner); int area1 = rectangle1.area(); int area2 = rectangle2.area(); if(area1 == area2) System.out.println("Equal");

  25. Clean code

  26. Make it hard for bugs to hide Clean code does one thing well

  27. Never obscure the designer’s intent Reads like well-written prose

  28. Provides one way rather than many ways for doing one thing

  29. Each routine you read turn out to be pretty much what you expect

  30. (Conclusion)Clean code is • Make if hard for bugs to hide • Clean code does one thing well • Reads like well-written prose • Never obscure the designer’s intent • Provides one way rather than many ways for doing one thing • Each routine you read turn out to be pretty much what you expect

  31. notes • Clean Programming is some thing like martial art • Combination of technique and art. • The Art of Computer Programming by Knuth • Have different school of thoughts • Clean Programming is skill • Good learning results in good use forever • Changing bad learning is hard • Like Driving!

  32. Reference • Refactoring: improving the design of existing code, Martin Fowler, Kent Beck,JohnBrant, William Opdyke, Don Roberts(1999) • Clean code,A handbook of agile software craftmanship,Robert C Martin,2008, Prentice Hall

More Related