Top Banner
Refactoring Mehdi Einali Advanced Programming in Java 1
33

Refactoring Mehdi Einali Advanced Programming in Java 1.

Jan 19, 2016

Download

Documents

Hillary Oliver
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Refactoring Mehdi Einali Advanced Programming in Java 1.

1

Refactoring

Mehdi Einali

Advanced Programming in Java

Page 2: Refactoring Mehdi Einali Advanced Programming in Java 1.

2

Tale of Messy code

Page 3: Refactoring Mehdi Einali Advanced Programming in Java 1.

3

Once upon a time …A team start a project

Project got many attention and team has to add new features in short time

“I will fix this later”

Page 4: Refactoring Mehdi Einali Advanced Programming in Java 1.

4

After a whileChanges slowed down by messy code

As productivity decreases more programmer assigned to project

New programmer with messy code results in more messy code

Page 5: Refactoring Mehdi Einali Advanced Programming in Java 1.

5

rebellionEventually the team rebels.

A new tiger team is selectedBest 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

Page 6: Refactoring Mehdi Einali Advanced Programming in Java 1.

6

refactoring

Page 7: Refactoring Mehdi Einali Advanced Programming in Java 1.

7

RefactoringA disciplined way to restructure code

in order to improve code qualitywithout 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.

Page 8: Refactoring Mehdi Einali Advanced Programming in Java 1.

8

RefactoringRefactoring 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 structureIt is a disciplined way to clean up code It minimizes the chances of introducing bugsWhen you refactor, you are improving the design of the code after it has been written.

Page 9: Refactoring Mehdi Einali Advanced Programming in Java 1.

9

Refactoring

By continuously improving the design of code, we make it easier and easier to work with

Joshua Kerievsky, Refactoring to Patterns

Page 10: Refactoring Mehdi Einali Advanced Programming in Java 1.

10

ExampleDuplicate CodeWhat 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

Page 11: Refactoring Mehdi Einali Advanced Programming in Java 1.

11

Bad SmellA bad smell in codeAny symptom in the source code that possibly indicates a deeper problem.The term is coined by Kent Beck.

Page 12: Refactoring Mehdi Einali Advanced Programming in Java 1.

12

Bad SmellsDuplicated CodeLong MethodLarge ClassLong Parameter ListDivergent Change…

Page 13: Refactoring Mehdi Einali Advanced Programming in Java 1.

13

Refactoring TechniquesExtract MethodMove

MethodVariableClass

Extract ClassRename

MethodVariableClass

Pull UpPush Down

Page 14: Refactoring Mehdi Einali Advanced Programming in Java 1.

14

IDE SupportRefactoring techniques are widely supported by IDEs

Page 15: Refactoring Mehdi Einali Advanced Programming in Java 1.

15

The Two HatsKent Beck's metaphor of two hatsDivide your time between two distinct activities

adding functionrefactoring

Page 16: Refactoring Mehdi Einali Advanced Programming in Java 1.

16

Why Should I Refactor?Improves the Design of SoftwareMakes Software Easier to UnderstandHelps You Find BugsHelps You Program Faster

Refactoring makes your code more maintainable

Page 17: Refactoring Mehdi Einali Advanced Programming in Java 1.

17

When Should You Refactor?

The Rule of Three:Refactor When You Add FunctionRefactor When You Need to Fix a BugRefactor As You Do a Code Review

Page 18: Refactoring Mehdi Einali Advanced Programming in Java 1.

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!

Page 19: Refactoring Mehdi Einali Advanced Programming in Java 1.

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…

Page 20: Refactoring Mehdi Einali Advanced Programming in Java 1.

20

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;

}}

Extract Class…

Page 21: Refactoring Mehdi Einali Advanced Programming in Java 1.

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");

Page 22: Refactoring Mehdi Einali Advanced Programming in Java 1.

22

class Rectangle{...public int area(){

return length * width;}

}

…int area1 = rectangle1.area();int area2 = rectangle2.area();

Extract Method…

Page 23: Refactoring Mehdi Einali Advanced Programming in Java 1.

23

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;

}

Extract Method…

Page 24: Refactoring Mehdi Einali Advanced Programming in Java 1.

24

Refactored CodeScanner 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");

Page 25: Refactoring Mehdi Einali Advanced Programming in Java 1.

25

Clean code

Page 26: Refactoring Mehdi Einali Advanced Programming in Java 1.

26

Clean code does one thing well

Make if hard for bugs to hide

Page 27: Refactoring Mehdi Einali Advanced Programming in Java 1.

27

Reads like well-written prose

Never obscure the designer’s intent

Page 28: Refactoring Mehdi Einali Advanced Programming in Java 1.

28

Provides one way rather than many ways for doing one thing

Page 29: Refactoring Mehdi Einali Advanced Programming in Java 1.

29

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

Page 30: Refactoring Mehdi Einali Advanced Programming in Java 1.

30

(Conclusion)Clean code isMake if hard for bugs to hideClean code does one thing wellReads like well-written prose

Never obscure the designer’s intentProvides one way rather than many ways for doing one thingEach routine you read turn out to be pretty much what you expect

Page 31: Refactoring Mehdi Einali Advanced Programming in Java 1.

31

notesClean 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 hardLike Driving!

Page 32: Refactoring Mehdi Einali Advanced Programming in Java 1.

32

ReferenceRefactoring: 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

Page 33: Refactoring Mehdi Einali Advanced Programming in Java 1.

33