Advanced Programming
SUT • Spring 2019
By continuously improving the design of code, we make it easier and easier to work with
Joshua Kerievsky, Refactoring to Patterns
A bad smell in code
Any symptom in the source code that possibly indicates a deeper problem.
The term is coined by Kent Beck.
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
Duplicated Code
Long Method
Large Class
Long Parameter List
…
Extract Method
Move
Method
Variable
Class
Extract Class
Rename
Method
Variable
Class
Pull Up
Push Down
Refactoring techniques are widely supported by IDEs
Practice it in Intelij
The Rule of Three:
Refactor When You Add Function
Refactor When You Need to Fix a Bug
Refactor As You Do a Code Review
public class Main {
public void main(String[] args){
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!
public class Main {
public void main(String[] args){
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…
public 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…
public class Main {
public static void main(String[] args){
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");
}
}
Extract Class…
public class Rectangle {
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 rectangle = new Rectangle(length, width);
return rectangle;
}
}
Extract Method…
public class Main{
public static void main(String[] args){
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");
}
}
Final Refactor ...