Download presentation
Presentation is loading. Please wait.
Published byJulius Randall Modified over 9 years ago
1
www.javacup.ir Sadegh Aliakbary
2
Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP is clearly noted as the source in the used case. JAVACUP shall not be liable for any errors in the content, or for any actions taken in reliance thereon. Please send your feedback to info@javacup.irinfo@javacup.ir 2JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
3
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. 3 Martin Fowler JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
4
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. 4 Martin Fowler JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
5
Refactoring By continuously improving the design of code, we make it easier and easier to work with 5 Joshua Kerievsky, Refactoring to Patterns JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
6
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 6JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
7
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. 7JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
8
Bad Smells If it stinks, change it! Kent Beck and Martin Fowler. Bad smells in code Bad smells are source of problems Remove bad smells How? By Refactoring 8JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
9
Bad Smells Duplicated Code Long Method Large Class Long Parameter List … 9JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
10
Refactoring Techniques Extract Method Move Method Variable Class Extract Class Rename Method Variable Class Pull Up Push Down 10JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
11
IDE Support Refactoring techniques are widely supported by IDEs Practice it in Eclipse 11JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
12
The Two Hats Kent Beck's metaphor of two hats Divide your time between two distinct activities adding function refactoring 12JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
13
Why Should I Refactor? Refactoring Improves the Design of Software Refactoring Makes Software Easier to Understand Refactoring Helps You Find Bugs Refactoring Helps You Program Faster Refactoring makes your code more maintainable 13JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
14
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 14JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
15
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"); 15 Find bad smells! Refactor the Code! JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
16
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"); 16 Rename… JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
17
class Rectangle{ private int length, width; public int getLength() { return length; } public void setLength(int length) { this.length = length; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public Rectangle(int length, int width) { this.length = length; this.width = width; } 17 Extract Class… JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
18
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"); 18JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
19
class Rectangle{... public int area(){ return length * width; } … int area1 = rectangle1.area(); int area2 = rectangle2.area(); 19 Extract Method… JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
20
private static 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; } 20 Extract Method… JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
21
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"); 21JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
22
Reference Refactoring: improving the design of existing code, Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts (1999) 22JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
23
23JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.