Refactoring

Advanced Programming

SUT • Spring 2019

Refactoring

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.

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.

Refactoring

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

Joshua Kerievsky, Refactoring to Patterns

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

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.

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

Bad Smells

  • Duplicated Code

  • Long Method

  • Large Class

  • Long Parameter List

Refactoring Techniques

  • Extract Method

  • Move

    • Method

    • Variable

    • Class

  • Extract Class

  • Rename

    • Method

    • Variable

    • Class

    • Pull Up

  • Push Down

IDE Support

  • Refactoring techniques are widely supported by IDEs

  • Practice it in Intelij

The Two Hats

  • Kent Beck's metaphor of two hats
  • Divide your time between two distinct activities
    • adding function
    • refactoring

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

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

Example

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!

Example

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…

Example

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…

Example

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…

Example

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…

Refactored Code

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

References

  • Refactoring: improving the design of existing code, Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts (1999)

Title Text

Refactoring

By Behnam Hatami

Refactoring

Refactoring / Advanced Programming Course @ SUT, Spring 2019

  • 900