Advanced Programming in Java Refactoring Mehdi Einali 1

  • Slides: 33
Download presentation
Advanced Programming in Java Refactoring Mehdi Einali 1

Advanced Programming in Java Refactoring Mehdi Einali 1

Tale of Messy code 2

Tale of Messy code 2

Once upon a time … A team start a project Project got many attention

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” 3

After a while Changes slowed down by messy code As productivity decreases more programmer

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 4

rebellion Eventually the team rebels. A new tiger team is selected Best technologies has

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 5

refactoring 6

refactoring 6

Refactoring A disciplined way to restructure code in order to improve code quality without

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 cheaper to modify without changing its observable behavior. 7

Refactoring is the process of changing a software system In such a way that

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. 8

Refactoring By continuously improving the design of code, we make it easier and easier

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

Example Duplicate Code What are the drawbacks? What is the solution? Refactoring: Finding a

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 10

Bad Smell A bad smell in code Any symptom in the source code that

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. 11

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

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

Refactoring Techniques Extract Method Move Method Variable Class Extract Class Rename Method Variable Class

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

IDE Support Refactoring techniques are widely supported by IDEs 14

IDE Support Refactoring techniques are widely supported by IDEs 14

The Two Hats Kent Beck's metaphor of two hats Divide your time between two

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

Why Should I Refactor? Improves the Design of Software Makes Software Easier to Understand

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 16

When Should You Refactor? The Rule of Three: Refactor When You Add Function Refactor

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 17

Scanner s = new Scanner(System. in); System. out. println("Rectangle Info. "); System. out. print("Enter

Scanner s = new Scanner(System. in); System. out. println("Rectangle Info. "); System. out. print("Enter the width: "); int a 1 = s. next. Int(); System. out. print("Enter the length: "); int a 2 = s. next. Int(); System. out. println("Rectangle Info. "); System. out. print("Enter the width: "); int b 1 = s. next. Int(); System. out. print("Enter the length: "); int b 2 = s. next. Int(); Find bad smells! int x = a 1*a 2; int y = b 1*b 2; Refactor the Code! if(x == y) System. out. println("Equal"); 18

Scanner scanner = new Scanner(System. in); System. out. println("Rectangle Info. "); System. out. print("Enter

Scanner scanner = new Scanner(System. in); System. out. println("Rectangle Info. "); System. out. print("Enter the width: "); int width 1 = scanner. next. Int(); System. out. print("Enter the length: "); int length 1 = scanner. next. Int(); System. out. println("Rectangle Info. "); System. out. print("Enter the width: "); int width 2 = scanner. next. Int(); System. out. print("Enter the length: "); int length 2 = scanner. next. Int(); Rename… int area 1 = width 1*length 1; int area 2 = width 2*length 2; if(area 1 == area 2) System. out. println("Equal"); 19

class Rectangle{ private int length , width; public int get. Length() { return length;

class Rectangle{ private int length , width; public int get. Length() { return length; } public void set. Length(int length) { this. length = length; } public int get. Width() { return width; } public void set. Width(int width) { this. width = width; } public Rectangle(int length, int width) { this. length = length; this. width = width; Extract } } 20 Class…

Scanner scanner = new Scanner(System. in); System. out. println("Rectangle Info. "); System. out. print("Enter

Scanner scanner = new Scanner(System. in); System. out. println("Rectangle Info. "); System. out. print("Enter the width: "); int width = scanner. next. Int(); System. out. print("Enter the length: "); int length = scanner. next. Int(); Rectangle rectangle 1 = new Rectangle(length, width); System. out. println("Rectangle Info. "); System. out. print("Enter the width: "); width = scanner. next. Int(); System. out. print("Enter the length: "); length = scanner. next. Int(); Rectangle rectangle 2 = new Rectangle(length, width); int area 1 = rectangle 1. get. Width()*rectangle 1. get. Length(); int area 2 = rectangle 2. get. Width()*rectangle 2. get. Length(); if(area 1 == area 2) System. out. println("Equal"); 21

class Rectangle{. . . public int area(){ return length * width; } } …

class Rectangle{. . . public int area(){ return length * width; } } … int area 1 = rectangle 1. area(); int area 2 = rectangle 2. area(); Extract Method… 22

private static Rectangle read. Rectangle(Scanner scanner) { int width; int length; System. out. println("Rectangle

private static Rectangle read. Rectangle(Scanner scanner) { int width; int length; System. out. println("Rectangle Info. "); System. out. print("Enter the width: "); width = scanner. next. Int(); System. out. print("Enter the length: "); length = scanner. next. Int(); Rectangle rectangle 2 = new Rectangle(length, width); return rectangle 2; } Extract Method… 23

Refactored Code Scanner scanner = new Scanner(System. in); Rectangle rectangle 1 = read. Rectangle(scanner);

Refactored Code Scanner scanner = new Scanner(System. in); Rectangle rectangle 1 = read. Rectangle(scanner); Rectangle rectangle 2 = read. Rectangle(scanner); int area 1 = rectangle 1. area(); int area 2 = rectangle 2. area(); if(area 1 == area 2) System. out. println("Equal"); 24

Clean code 25

Clean code 25

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

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

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

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

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

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

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

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

(Conclusion)Clean code is Make if hard for bugs to hide Clean code does one

(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 30

notes Clean Programming is some thing like martial art Combination of technique and art.

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! 31

Reference Refactoring: improving the design of existing code, Martin Fowler, Kent Beck, John Brant,

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

33

33