refactoring-types

Summary of refactorings

1. TABLE OF CONTENT

3. BAD SMELLS IN CODE

1. Duplicated code

Same code structure in more than one place.

2. Long Method

The longer a procedure is the more difficult is to understand.

3. Large Classes

When a class is trying to do too much, duplicated code cannot be far behind.

4. Long Parameter List

The are hard to understand, inconsistent and difficult to use.

5. Divergent Change

When one class is commonly changed in different ways for different reasons.

6. Shotgun Surgery

When every time you make a kind of change, you have to make a lot of little changes to a lot of different classes.

7. Feature Envy

A method that seems more interested in a class other than the one it actually is in.

8. Data Clumps

Bunches of data(fields, parameters...) that hang around together.

9. Primitive Obsession

Using primitives types instead of small objects.

10. Switch Statements

The same switch statement scattered about a program in different places. Use polymorphism.

11. Parallel Inheritance Hierarchies

Every time you make a subclass of one class, you also have to make a subclass of another.

12. Lazy Class

A class that isn't doing enough to pay for itself should be eliminated.

13. Speculative Generality

All sorts of hooks and special cases to handle things that aren't required.

14. Temporary Field

An instance variable that is set only in certain circumstances.

15. Message Chain

When a client asks one object for another object, which the client then asks for yet another object...

16. Middle Man

When an object delegates much of its functionality.

17. Inappropriate Intimacy

When classes access to much to another classes.

18. Alternative Classes with Different Interfaces

Classes with methods that look to similar.

19. Incomplete Library Class

When we need extra features in libraries.

20. Data Class

Don't allow manipulation in Data Classes. Use encapsulation and immutability.

21. Refused Bequest

Subclasses that don't make uses of parents methods.

22. Comments

Not all comments but the ones that are there because the code is bad.

6. COMPOSING METHODS

1. Extract Method

You have a code fragment that can be grouped together.

    void printOwing(double amount) {
        printBanner();
        //print details
        System.out.println ("name:" + _name);
        System.out.println ("amount" + amount);
    }

to

    void printOwing(double amount) {
        printBanner();
        printDetails(amount);
    }

    void printDetails (double amount) {
        System.out.println ("name:" + _name);
    System.out.println ("amount" + amount);
    }

Motivation

  • Increases the chances that other methods can use a method

  • Allows the higher-level methods to read more like a series of comments

    void printOwing(double previousAmount) {
        Enumeration e = _orders.elements();
        double outstanding = previousAmount * 1.2;
        printBanner();

        // calculate outstanding
        while (e.hasMoreElements()) {
            Order each = (Order) e.nextElement();
            outstanding += each.getAmount();
        }
        printDetails(outstanding);
    }

to

    void printOwing(double previousAmount) {
        printBanner();
        double outstanding = getOutstanding(previousAmount * 1.2);
        printDetails(outstanding);
    }

    double getOutstanding(double initialValue) {
        double result = initialValue;
        Enumeration e = _orders.elements();

        while (e.hasMoreElements()) {
            Order each = (Order) e.nextElement();
            result += each.getAmount();
        }
        return result;
    }

2. Inline Method

A method's body is just as clear as its name.

    int getRating() {
        return (moreThanFiveLateDeliveries()) ? 2 : 1;
    }

    boolean moreThanFiveLateDeliveries() {
        return _numberOfLateDeliveries > 5;
    }

to

    int getRating() {
        return (_numberOfLateDeliveries > 5) ? 2 : 1;
    }

Motivation

  • When Indirection is needless (simple delegation) becomes irritating.

  • If group of methods are badly factored and grouping them makes it clearer

3. Inline Temp

You have a temp that is assigned to once with a simple expression, and the temp is getting in the way of other refactorings.

    double basePrice = anOrder.basePrice();
    return (basePrice > 1000)

to

    return (anOrder.basePrice() > 1000)

Motivation

4. Replace Temp with Query

You are using a temporary variable to hold the result of an expression.

    double basePrice = _quantity * _itemPrice;
    if (basePrice > 1000)
        return basePrice * 0.95;
    else
        return basePrice * 0.98;

to

    if (basePrice() > 1000)
        return basePrice() * 0.95;
    else
        return basePrice() * 0.98;
    ...
    double basePrice() {
        return _quantity * _itemPrice;
    }

Motivation

  • Replacing the temp with a query method, any method in the class can get at the information.

  • Is a vital step before 1. Extract Method

5. Introduce Explaining Variable

You have a complicated expression

    if ( (platform.toUpperCase().indexOf("MAC") > -1) &&
        (browser.toUpperCase().indexOf("IE") > -1) &&
        wasInitialized() && resize > 0 )
    {
        // do something
    }

to

    final boolean isMacOs = platform.toUpperCase().indexOf("MAC") >-1;
    final boolean isIEBrowser = browser.toUpperCase().indexOf("IE") >-1;
    final boolean wasResized = resize > 0;
    if (isMacOs && isIEBrowser && wasInitialized() && wasResized) {
        // do something
    }

Motivation

  • When expressions are hard to read

6. Split Temporary Variable

You have a temporary variable assigned to more than once, but is not a loop variable nor a collecting temporary variable.

    double temp = 2 * (_height + _width);
    System.out.println (temp);
    temp = _height * _width;
    System.out.println (temp);

to

    final double perimeter = 2 * (_height + _width);
    System.out.println (perimeter);
    final double area = _height * _width;
    System.out.println (area);

Motivation

  • Variables should not have more than one responsibility.

  • Using a temp for two different things is very confusing for the reader.

7. Remove Assignments to Parameters

The code assign to a parameter

    int discount (int inputVal, int quantity, int yearToDate) {
        if (inputVal > 50) inputVal -= 2;

to

    int discount (int inputVal, int quantity, int yearToDate) {
        int result = inputVal;
        if (inputVal > 50) result -= 2;

Motivation

  • You can change the internals of object is passed but do not point to another object.

  • Use only the parameter to represent what has been passed.

8. Replace Method with Method Object

You have a long method that uses local variables in such a way that you cannot apply Extract Method.

    class Order...
        double price() {
            double primaryBasePrice;
            double secondaryBasePrice;
            double tertiaryBasePrice;
            // long computation;
            ...
        }

to

    class Order...
        double price(){
            return new PriceCalculator(this).compute()
        }
    }

    class PriceCalculato...
    compute(){
        double primaryBasePrice;
        double secondaryBasePrice;
        double tertiaryBasePrice;
        // long computation;
        return ...
    }

Motivation

  • When a method has lot of local variables and applying decomposition is not possible

This sample does not really needs this refactoring, but shows the way to do it.

    Class Account
        int gamma (int inputVal, int quantity, int yearToDate) {
            int importantValue1 = (inputVal * quantity) + delta();
            int importantValue2 = (inputVal * yearToDate) + 100;
            if ((yearToDate - importantValue1) > 100)
            importantValue2 -= 20;
            int importantValue3 = importantValue2 * 7;
            // and so on.
            return importantValue3 - 2 * importantValue1;
        }
    }

to

    class Gamma...
        private final Account _account;
        private int inputVal;
        private int quantity;
        private int yearToDate;
        private int importantValue1;
        private int importantValue2;
        private int importantValue3;

        Gamma (Account source, int inputValArg, int quantityArg, int yearToDateArg) {
            _account = source;
            inputVal = inputValArg;
            quantity = quantityArg;
            yearToDate = yearToDateArg;
        }

        int compute () {
            importantValue1 = (inputVal * quantity) + _account.delta();
            importantValue2 = (inputVal * yearToDate) + 100;
            if ((yearToDate - importantValue1) > 100)
            importantValue2 -= 20;
            int importantValue3 = importantValue2 * 7;
            // and so on.
            return importantValue3 - 2 * importantValue1;
        }

        int gamma (int inputVal, int quantity, int yearToDate) {
            return new Gamma(this, inputVal, quantity,yearToDate).compute();
        }

9. Substitute Algorithm

You want to replace an algorithm with one that is clearer.

    String foundPerson(String[] people){
        for (int i = 0; i < people.length; i++) {
            if (people[i].equals ("Don")){
                return "Don";
            }
            if (people[i].equals ("John")){
                return "John";
            }
            if (people[i].equals ("Kent")){
                return "Kent";
            }
        }
        return "";
    }

to

    String foundPerson(String[] people){
        List candidates = Arrays.asList(new String[] {"Don", "John","Kent"});
    for (int i = 0; i<people.length; i++)
        if (candidates.contains(people[i]))
            return people[i];
    return "";
    }

Motivation

  • Break down something complex into simpler pieces.

  • Makes easier apply changes to the algorithm.

  • Substituting a large, complex algorithm is very difficult; making it simple can make the substitution tractable.

7. Moving features between elements

10. Move method

A method is, or will be, using or used by more features of another class than the class on which it is defined.

Create a new method with a similar body in the class it uses most. Either turn the old method into a simple delegation, or remove it altogether.

    class Class1 {
        aMethod()
    }

    class Class2 {    }

to

    class Class1 {    }

    class Class2 {
        aMethod()
    }

Motivation

When classes do to much work or when collaborate too much and are highly coupled

11. Move field

A field is, or will be, used by another class more than the class on which it is defined. Create a new field in the target class, and change all its users.

    class Class1 {
        aField
    }

    class Class2 {    }

to

    class Class1 {    }

    class Class2 {
        aField
    }

Motivation

If a field is used mostly by outer classes and before 12. Extract Class

12. Extract Class

You have one class doing work that should be done by two. Create a new class and move the relevant fields and methods from the old class into the new class.

    class Person {
        name,
        officeAreaCode,
        officeNumber,
        getTelephoneNumber()
    }

to

    class Person {
        name,
        getTelephoneNumber()
    }

    class TelephoneNumber {
        areaCode,
        number,
        getTelephoneNumber()
    }

Motivation

Classes grow. Split them when:

  • subsets of methods seem to be together

  • subsets of data usually change together or depend on each other

13. Inline Class

A class isn't doing very much. Move all its features into another class and delete it.

    class Person {
        name,
        getTelephoneNumber()
    }

    class TelephoneNumber {
        areaCode,
        number,
        getTelephoneNumber()
    }

to

    class Person {
        name,
        officeAreaCode,
        officeNumber,
        getTelephoneNumber()
    }

Motivation

After refactoring normally there are a bunch of responsibilities moved out of the class, letting the class with little left.

14. Hide Delegate

A client is calling a delegate class of an object. Create methods on the server to hide the delegate.

    class ClientClass {
        //Dependencies
        Person person = new Person()
        Department department = new Department()
        person.doSomething()
        department.doSomething()
    }

to

    class ClientClass {
        Person person = new Person()
        person.doSomething()
    }

    class Person{
        Department department = new Department()
        department.doSomething()
    }

The solution

    class ClientClass{
        Server server = new Server()
        server.doSomething()
    }

    class Server{
        Delegate delegate = new Delegate()
        void doSomething(){
            delegate.doSomething()
        }
    }
    // The delegate is hidden to the client class.
    // Changes won't be propagated to the client they will only affect the server
    class Delegate{
        void doSomething(){...}
    }

Motivation

Key of encapsulation. Classes should know as less as possible of other classes.

> manager = john.getDepartment().getManager();

    class Person {
        Department _department;
        public Department getDepartment() {
            return _department;
        }
        public void setDepartment(Department arg) {
            _department = arg;
            }
        }

    class Department {
        private String _chargeCode;
        private Person _manager;
        public Department (Person manager) {
            _manager = manager;
        }
        public Person getManager() {
            return _manager;
        }
        ...

to

> manager = john.getManager();

    class Person {
        ...
        public Person getManager() {
            return _department.getManager();
        }
    }

15. Remove Middle Man

A class is doing too much simple delegation. Get the client to call the delegate directly.

    class ClientClass {
        Person person = new Person()
        person.doSomething()
    }

    class Person{
        Department department = new Department()
        department.doSomething()
    }

to

    class ClientClass {
        //Dependencies
        Person person = new Person()
        Department department = new Department()
        person.doSomething()
        department.doSomething()
    }

Motivation

When the "Middle man" (the server) does to much is time for the client to call the delegate directly.

16. Introduce Foreign Method

A server class you are using needs an additional method, but you can't modify the class. Create a method in the client class with an instance of the server class as its first argument.

    Date newStart = new Date(previousEnd.getYear(),previousEnd.getMonth(),previousEnd.getDate()+1);

to

    Date newStart = nextDay(previousEnd);

    private static Date nextDay(Date date){
        return new Date(date.getYear(),date.getMonth(),date.getDate()+1);
    }

Motivation

When there is a lack of a method in class that you use a lot and you can not change that class.

17. Introduce Local Extension

A server class you are using needs several additional methods, but you can't modify the class. Create a new class that contains these extra methods. Make this extension class a subclass or a wrapper of the original.

    class ClientClass(){

        Date date = new Date()
        nextDate = nextDay(date);

        private static Date nextDay(Date date){
            return new Date(date.getYear(),date.getMonth(),date.getDate()+1);
        }
    }

to

    class ClientClass() {
        MfDate date = new MfDate()
        nextDate = nextDate(date)
    }
    class MfDate() extends Date {
        ...
        private static Date nextDay(Date date){
            return new Date(date.getYear(),date.getMonth(),date.getDate()+1);
        }
    }

Motivation

When plenty of 16. Introduce Foreign Method need to be added to a class.

8. ORGANIZING DATA

18. Self Encapsulate Field

You are accessing a field directly, but the coupling to the field is becoming awkward. Create getting and setting methods for the field and use only those to access the field.

    private int _low, _high;
    boolean includes (int arg) {
        return arg >= _low && arg <= _high;
    }

to

    private int _low, _high;
    boolean includes (int arg) {
        return arg >= getLow() && arg <= getHigh();
    }
    int getLow() {return _low;}
    int getHigh() {return _high;}

Motivation Allows a subclass to override how to get that information with a method and that it supports more flexibility in managing the data, such as lazy initialization.

19. Replace Data Value with Object

You have a data item that needs additional data or behavior. Turn the data item into an object

    class Order...{
        private String _customer;
        public Order (String customer) {
            _customer = customer;
        }
    }

to

    class Order...{
        public Order (String customer) {
            _customer = new Customer(customer);
        }
    }

    class Customer {
        public Customer (String name) {
            _name = name;
        }
    }

Motivation Simple data items aren't so simple anymore.

20. Change Value to Reference

You have a class with many equal instances that you want to replace with a single object. Turn the object into a reference object.

    class Order...{
        public Order (String customer) {
            _customer = new Customer(customer);
        }
    }

    class Customer {
        public Customer (String name) {
            _name = name;
        }
    }

to

    //Use Factory Method
    class Customer...
        static void loadCustomers() {
            new Customer ("Lemon Car Hire").store();
            new Customer ("Associated Coffee Machines").store();
            new Customer ("Bilston Gasworks").store();
        }
        private void store() {
            _instances.put(this.getName(), this);
        }
        public static Customer create (String name) {
            return (Customer) _instances.get(name);
        }

Motivation Reference objects are things like customer or account. Each object stands for one object in the real world, and you use the object identity to test whether they are equal.

21. Change Reference to Value

You have a reference object that is small, immutable, and awkward to manage. Turn it into a value object

    new Currency("USD").equals(new Currency("USD")) // returns false

to

    // Add `equals` and `hashCode` to the Currency class
    class Currency{
        ...
        public boolean equals(Object arg) {
             if (! (arg instanceof Currency)) return false;
             Currency other = (Currency) arg;
            return (_code.equals(other._code));
        }

        public int hashCode() {
             return _code.hashCode();
         }
     }

     // Now you can do
    new Currency("USD").equals(new Currency("USD")) // now returns true

Motivation Working with the reference object becomes awkward. The reference object is immutable and small. Used in distributed or concurrent systems.

22. Replace Array with Object

You have an array in which certain elements mean different things. Replace the array with an object that has a field for each element

    String[] row = new String[3];
    row [0] = "Liverpool";
    row [1] = "15";

to

    Performance row = new Performance();
    row.setName("Liverpool");
    row.setWins("15");

Motivation Arrays should be used only to contain a collection of similar objects in some order.

23. Duplicate Observed Data

You have domain data available only in a GUI control, and domain methods need access. Copy the data to a domain object. Set up an observer to synchronize the two pieces of data Motivation To separate code that handles the user interface from code that handles the business logic.

24. Change Unidirectional Association to Bidirectional

You have two classes that need to use each other's features, but there is only a one-way link. Add back pointers, and change modifiers to update both sets

    class Order...
        Customer getCustomer() {
            return _customer;
        }
        void setCustomer (Customer arg) {
            _customer = arg;
        }
        Customer _customer;
    }

to

    class Order...
        Customer getCustomer() {
            return _customer;
        }
        void setCustomer (Customer arg) {
            if (_customer != null) _customer.friendOrders().remove(this);
            _customer = arg;
            if (_customer != null) _customer.friendOrders().add(this);
        }
        private Customer _customer;

        class Customer...
            void addOrder(Order arg) {
                arg.setCustomer(this);
            }
            private Set _orders = new HashSet();

            Set friendOrders() {
                /** should only be used by Order */
                return _orders;
            }
        }
    }

    // Many to Many
    class Order... //controlling methods
        void addCustomer (Customer arg) {
            arg.friendOrders().add(this);
            _customers.add(arg);
        }
        void removeCustomer (Customer arg) {
            arg.friendOrders().remove(this);
            _customers.remove(arg);
        }
    class Customer...
        void addOrder(Order arg) {
            arg.addCustomer(this);
        }
        void removeOrder(Order arg) {
            arg.removeCustomer(this);
        }
    }

Motivation When the object referenced needs access access to the object that refer to it.

25. Change Bidirectional Association to Unidirectional

You have a two-way association but one class no longer needs features from the other. Drop the unneeded end of the association Motivation If Bidirectional association is not needed, reduce complexity, handle zombie objects, eliminate interdependency

26. Replace Magic Number with Symbolic Constant

You have a literal number with a particular meaning. Create a constant, name it after the meaning, and replace the number with it

    double potentialEnergy(double mass, double height) {
        return mass * 9.81 * height;
    }

to

    double potentialEnergy(double mass, double height) {
        return mass * GRAVITATIONAL_CONSTANT * height;
    }
    static final double GRAVITATIONAL_CONSTANT = 9.81;

Motivation Avoid using Magic numbers.

27. Encapsulate Field

There is a public field. Make it private and provide accessors

    public String _name

to

    private String _name;
    public String getName() {return _name;}
    public void setName(String arg) {_name = arg;}

Motivation You should never make your data public.

28. Encapsulate Collection

A method returns a collection. Make it return a read-only view and provide add/remove methods

    class Person {
        Person (String name){
            HashSet set new HashSet()
        }
        Set getCourses(){}
        void setCourses(:Set){}
    }

to

    class Person {
        Person (String name){
            HashSet set new HashSet()
        }
        Unmodifiable Set getCourses(){}
        void addCourses(:Course){}
        void removeCourses(:Course){}
    }

Motivation

  • Encapsulated reduce the coupling of the owning class to its clients.

  • The getter should not return the collection object itself.

  • The getter should return something that prevents manipulation of the collection and hides unnecessary details.

  • There should not be a setter for collection, only operations to add and remove elements.

29. Remove Record with data class

You need to interface with a record structure in a traditional programming environment. Make a dumb data object for the record.

Motivation

  • Copying a legacy program

  • Communicating a structured record with a traditional programming API or database.

    30. Replace Type Code with Class

    A class has a numeric type code that does not affect its behavior. Replace the number with a new class.

      class Person{
          O:Int;
          A:Int;
          B:Int;
          AB:Int;
          bloodGroup:Int;
      }

    to

      class Person{
          bloodGroup:BloodGroup;
      }
    
      class BloodGroup{
          O:BloodGroup;
          A:BloodGroup;
          B:BloodGroup;
          AB:BloodGroup;
      }

    Motivation Statically type checking.

31. Replace Type Code with Subclasses

You have an immutable type code that affects the behavior of a class. Replace the type code with subclasses.

    class Employee...
        private int _type;

        static final int ENGINEER = 0;
        static final int SALESMAN = 1;
        static final int MANAGER = 2;

        Employee (int type) {
            _type = type;
        }
    }

to

    abstract int getType();
    static Employee create(int type) {
        switch (type) {
            case ENGINEER:
                return new Engineer();
            case SALESMAN:
                return new Salesman();
            case MANAGER:
                return new Manager();
            default:
                throw new IllegalArgumentException("Incorrect type code value");
        }
    }

Motivation

32. Replace Type Code with State/Strategy

You have a type code that affects the behavior of a class, but you cannot use subclassing. Replace the type code with a state object

    class Employee {
        private int _type;

        static final int ENGINEER = 0;
        static final int SALESMAN = 1;
        static final int MANAGER = 2;

        Employee (int type) {
            _type = type;
        }

        int payAmount() {
            switch (_type) {
                case ENGINEER:
                    return _monthlySalary;
                case SALESMAN:
                    return _monthlySalary + _commission;
                case MANAGER:
                    return _monthlySalary + _bonus;
                default:
                    throw new RuntimeException("Incorrect Employee");
                }
            }
        }
    }

to

    class Employee {

    private final EmployeeType employeeType;

    public Employee(int type) {
        this.employeeType = EmployeeType.newType(type);
    }

        int payAmount() {
            return employeeType.salary();
            // switch (employeeType) {
            //     case EmployeeType.ENGINEER:
            //         return _monthlySalary;
            //     case EmployeeType.SALESMAN:
            //         return _monthlySalary + _commission;
            //     case EmployeeType.MANAGER:
            //         return _monthlySalary + _bonus;
            //     default:
            //         throw new RuntimeException("Incorrect Employee");
            // }
        }
    }

    public abstract class EmployeeType {
    abstract int getTypeCode();
        abstract int salary();

    static final int ENGINEER = 0;
    static final int SALESMAN = 1;
    static final int MANAGER = 2;

    public static EmployeeType newType(int code) {
        switch (code) {
            case ENGINEER:
                return new Engineer();
            case SALESMAN:
                return new SaleMan();
            case MANAGER:
                return new Manager();
            default:
                throw new IllegalArgumentException("Incorrect Employee code");
        }
    }
    }


    public class Engineer extends EmployeeType {
        @Override
        int getTypeCode() {
            return ENGINEER;
        }

            @Override
            int salary() {
                    return _monthlySalary;
            }
    }
    public class Manager extends EmployeeType {
        @Override
        int getTypeCode() {
            return MANAGER;
        }

            @Override
            int salary() {
                    return _monthlySalary + _bonus;
            }
    }
    public class SaleMan extends EmployeeType {
        @Override
        int getTypeCode() {
            return SALEMAN;
        }

            @Override
            int salary() {
                    return _monthlySalary + _commission;
            }
    }

Motivation

  • Similar to 31. Replace Type Code with Subclasses, but can be used if the type code changes during the life of the object or if another reason prevents subclassing.

  • It uses either the state or strategy pattern

32. Replace Subclass with Fields

You have subclasses that vary only in methods that return constant data. Change the methods to superclass fields and eliminate the subclasses

    abstract class Person {
        abstract boolean isMale();
        abstract char getCode();
        ...
    }
    class Male extends Person {
            boolean isMale() {
            return true;
        }
        char getCode() {
            return 'M';
        }
    }
    class Female extends Person {
        boolean isMale() {
            return false;
        }
        char getCode() {
            return 'F';
        }
    }

to

    class Person{
        protected Person (boolean isMale, char code) {
            _isMale = isMale;
            _code = code;
        }
        boolean isMale() {
            return _isMale;
        }
        static Person createMale(){
            return new Person(true, 'M');
        }
        static Person createFemale(){
            return new Person(false, 'F');
        }
    }

Motivation

  • Subclasses that consists only of constant methods is not doing enough to be worth existing.

  • Remove such subclasses completely by putting fields in the superclass.

  • Remove the extra complexity of the subclasses.

9. SIMPLIFYING CONDITIONAL EXPRESSIONS

33. Decompose Conditional

You have a complicated conditional (if-then-else) statement. Extract methods from the condition, then part, and else parts

    if (date.before (SUMMER_START) || date.after(SUMMER_END))
        charge = quantity * _winterRate + _winterServiceCharge;
    else charge = quantity * _summerRate;

to

    if (notSummer(date))
        charge = winterCharge(quantity);
    else charge = summerCharge (quantity);

Motivation

  • Highlight the condition and make it clearly what you are branching on.

  • Highlight the reason for the branching

34. Consolidate Conditional Expression

You have a sequence of conditional tests with the same result. Combine them into a single conditional expression and extract it

    double disabilityAmount() {
    if (_seniority < 2) return 0;
    if (_monthsDisabled > 12) return 0;
    if (_isPartTime) return 0;
    // compute the disability amount

to

    double disabilityAmount() {
    if (isNotEligableForDisability()) return 0;
    // compute the disability amount

Motivation

  • Makes the check clearer by showing that you are really making a single check

  • Sets you up for 1. Extract Method.

  • Clarify your code (replaces a statement of what you are doing with why you are doing it.)

35. Consolidate Duplicate Conditional Fragments

The same fragment of code is in all branches of a conditional expression. Move it outside of the expression.

    if (isSpecialDeal()) {
        total = price * 0.95;
        send();
    }
    else {
        total = price * 0.98;
        send();
    }

to

    if (isSpecialDeal()) {
        total = price * 0.95;
    }
    else {
        total = price * 0.98;
    }
    send();

Motivation Makes clearer what varies and what stays the same.

36. Remove Control Flag

You have a variable that is acting as a control flag for a series of boolean expressions. Use a break or return instead

    void checkSecurity(String[] people) {
        boolean found = false;
            for (int i = 0; i < people.length; i++) {
                if (! found) {
                    if (people[i].equals ("Don")){
                        sendAlert();
                        found = true;
                    }
                    if (people[i].equals ("John")){
                        sendAlert();
                        found = true;
                    }
                }
        }
    }

to

    void checkSecurity(String[] people) {
        for (int i = 0; i < people.length; i++) {
            if (people[i].equals ("Don")){
                sendAlert();
                break; // or return
            }
            if (people[i].equals ("John")){
                sendAlert();
                break; // or return
            }
        }
    }

Motivation

  • Control flag are used to determine when to stop looking, but modern languages enforce the use of break and continue

  • Make the real purpose of the conditional much more clear.

37. Replace Nested Conditional with Guard Clauses

A method has conditional behavior that does not make clear the normal path of execution. Use guard clauses for all the special cases

    double getPayAmount() {
        double result;
        if (_isDead) result = deadAmount();
        else {
            if (_isSeparated) result = separatedAmount();
            else {
                if (_isRetired) result = retiredAmount();
                else result = normalPayAmount();
            };
        }
        return result;
    };

to

    double getPayAmount() {
        if (_isDead) return deadAmount();
        if (_isSeparated) return separatedAmount();
        if (_isRetired) return retiredAmount();
        return normalPayAmount();
    };

Motivation

  • If the condition is an unusual condition, check the condition and return if the condition is true.

  • This kind of check is often called a guard clause [Beck].

  • If you are using an if-then-else construct you are giving equal weight to the if leg and the else leg.

  • This communicates to the reader that the legs are equally likely and important.

  • Instead the guard clause says, "This is rare, and if it happens, do something and get out."

38. Replace Conditional with Polymorphism

You have a conditional that chooses different behavior depending on the type of an object. Move each leg of the conditional to an overriding method in a subclass. Make the original method abstract

    class Employee {
        private int _type;

        static final int ENGINEER = 0;
        static final int SALESMAN = 1;
        static final int MANAGER = 2;

        Employee (int type) {
            _type = type;
        }
        int payAmount() {
            switch (_type) {
                case ENGINEER:
                    return _monthlySalary;
                case SALESMAN:
                    return _monthlySalary + _commission;
                case MANAGER:
                    return _monthlySalary + _bonus;
                default:
                    throw new RuntimeException("Incorrect Employee");
                }
            }
        }
    }

to

    class Employee...
        static final int ENGINEER = 0;
        static final int SALESMAN = 1;
        static final int MANAGER = 2;

        void setType(int arg) {
            _type = EmployeeType.newType(arg);
        }
        int payAmount() {
            return _type.payAmount(this);
        }
    }

    class Engineer...
        int payAmount(Employee emp) {
            return emp.getMonthlySalary();
        }
    }

Motivation

  • Avoid writing an explicit conditional when you have objects whose behavior varies depending on their types.

  • Switch statements should be less common in object oriented programs

39. Introduce Null Object

You have repeated checks for a null value. Replace the null value with a null object

    if (customer == null) plan = BillingPlan.basic();
    else plan = customer.getPlan();

to

    class Customer {}

    class NullCusomer extends Customer {}

Motivation

  • The object, depending on its type, does the right thing. Null objects should also apply this rule.

  • Use Null Object Pattern is the little brother of Special Case Pattern.

40. Introduce Assertion

A section of code assumes something about the state of the program. Make the assumption explicit with an assertion

    double getExpenseLimit() {
        // should have either expense limit or a primary project
        return (_expenseLimit != NULL_EXPENSE) ?
            _expenseLimit:
            _primaryProject.getMemberExpenseLimit();
    }

to

    double getExpenseLimit() {
        Assert.isTrue (_expenseLimit != NULL_EXPENSE || _primaryProject    != null);
        return (_expenseLimit != NULL_EXPENSE) ?
            _expenseLimit:
            _primaryProject.getMemberExpenseLimit();
    }

Motivation

  • Assertions are conditional statements that are assumed to be always true.

  • Assertion failures should always result in unchecked exceptions.

  • Assertions usually are removed for production code.

  • As communication aids: they help the reader understand the assumptions the code is making.

  • As debugging aids: assertions can help catch bugs closer to their origin.

10. MAKING METHOD CALLS SIMPLER

41. Rename method

The name of a method does not reveal its purpose. Change the name of the method

    getinvcdtlmt()

to

    getInvoiceableCreditLimit

Motivation Methods names must communicate their intention.

42. Add Parameter

A method needs more information from its caller. Add a parameter for an object that can pass on this information

    getContact()

to

    getContact(:Date)

Motivation After changed a method you require more information.

43. Remove Parameter

A parameter is no longer used by the method body. Remove it

    getContact(:Date)

to

    getContact()

Motivation A parameter is no more needed.

44. Separate Query from Modifier

You have a method that returns a value but also changes the state of an object. Create two methods, one for the query and one for the modification

    getTotalOutStandingAndSetReadyForSummaries()

to

    getTotalOutStanding()
    SetReadyForSummaries()

Motivation Signaling methods with side effects and those without.

45. Parameterize Method

Several methods do similar things but with different values contained in the method body. Create one method that uses a parameter for the different values

    fivePercentRaise()
    tenPercentRaise()

to

    raise(percentage)

Motivation Removes duplicate code and increases flexibility.

46. Replace Parameter with Explicit Methods

You have a method that runs different code depending on the values of an enumerated parameter. Create a separate method for each value of the parameter

    void setValue (String name, int value) {
        if (name.equals("height")){}
            _height = value;
            return;
        }
        if (name.equals("width")){
            _width = value;
            return;
        }
        Assert.shouldNeverReachHere();
    }

to

    void setHeight(int arg) {
        _height = arg;
    }
    void setWidth (int arg) {
        _width = arg;
    }

Motivation

  • Avoid the conditional behavior

  • Gain compile time checking

  • Clearer Interface

47. Preserve Whole Object

You are getting several values from an object and passing these values as parameters in a method call. Send the whole object instead

    int low = daysTempRange().getLow();
    int high = daysTempRange().getHigh();
    withinPlan = plan.withinRange(low, high);

to

    withinPlan = plan.withinRange(daysTempRange());

Motivation

  • Make parameters list robust to changes

  • Make code more readeable

  • Remove possible duplicate code already done in the object passed

  • Negative: It creates a dependency between the parameter object and the called.

48. Replace Parameter with Method

An object invokes a method, then passes the result as a parameter for a method. The receiver can also invoke this method. Remove the parameter and let the receiver invoke the method

    int basePrice = _quantity * _itemPrice;
    discountLevel = getDiscountLevel();
    double finalPrice = discountedPrice (basePrice, discountLevel);

to

    int basePrice = _quantity * _itemPrice;
    double finalPrice = discountedPrice (basePrice);

Motivation

  • If a method can get a value that is passed in as parameter by another means, it should.

  • If the receiving method can make the same calculation (does not reference any of the parameters of the calling method)

  • If you are calling a method on a different object that has a reference to the calling object.

49. Introduce Parameter Object

You have a group of parameters that naturally go together. Replace them with an object

    class Customer{
        amountInvoicedIn (start : Date, end : Date)
        amountReceivedIn (start : Date, end : Date)
        amountOverdueIn (start : Date, end : Date)
    }

to

    class Customer{
        amountInvoicedIn (: DateRange)
        amountReceivedIn (: DateRange)
        amountOverdueIn (: DateRange)
    }

Motivation

  • Reduces the size of the parameter list

  • Make the code more consistent

50. Remove Setting Method

A field should be set at creation time and never altered. Remove any setting method for that field

    class Employee{
        setImmutableValue()
    }

to

    class Employee{
        ¯\_(ツ)_/¯
    }

Motivation Make your intention clear: If you don't want that field to change once is created, then don't provide a setting method (and make the field final).

51. Hide Method

A method is not used by any other class. Make the method private

    class Employee{
        public method()
    }

to

    class Employee{
        private method()
    }

Motivation Whenever a method is not needed outside its class it should be hidden

52. Replace Constructor with Factory Method

You want to do more than simple construction when you create an object. Replace the constructor with a factory method

    Employee (int type) {
        _type = type;
    }

to

    static Employee create(int type) {
        return new Employee(type);
    }

Motivation Create an object depending on its subclasses (types). Constructors can only return an instance of the object that is asked for so a Factory method is needed.

53. Encapsulate Downcast

A method returns an object that needs to be downcasted by its callers. Move the downcast to within the method

    Object lastReading() {
        return readings.lastElement();
    }

to

    Reading lastReading() {
        return (Reading) readings.lastElement();
    }

Motivation Provide as result type, the most specific type of the method signature. If the signature is to general, check the uses the clients do of that method and if coherent, provide a more specific one.

54. Replace Error Code with Exception

A method returns a special code to indicate an error. Throw an exception instead

    int withdraw(int amount) {
        if (amount > _balance)
            return -1;
        else {
            _balance -= amount;
            return 0;
        }
    }

to

    void withdraw(int amount) throws BalanceException {
        if (amount > _balance)
            throw new BalanceException();
        _balance -= amount;
    }

Motivation When a program that spots an error can't figure out what to do about it. It needs to let its caller know, and the caller may pass the error up the chain.

55. Replace Exception with Test

You are throwing a checked exception on a condition the caller could have checked first. Change the caller to make the test first

    double getValueForPeriod (int periodNumber) {
        try {
            return _values[periodNumber];
        } catch (ArrayIndexOutOfBoundsException e) {
            return 0;
        }
    }

to

    double getValueForPeriod (int periodNumber) {
        if (periodNumber >= _values.length)
            return 0;
        return _values[periodNumber];
}

Motivation

  • Do not overuse Exceptions they should be used for exceptional behavior (behavior that is unexpected).

  • Do not use them as substitute for conditional tests.

  • Check expected wrong conditions before before calling the operation.

11. DEALING WITH GENERALIZATION

56. Pull up field

Two subclasses have the same field. Move the field to the superclass

    class Salesman extends Employee{
        String name;
    }
    class Engineer extends Employee{
        String name;
    }

to

    class Employee{
        String name;
    }
    class Salesman extends Employee{}
    class Engineer extends Employee{}

Motivation

  • Removes the duplicate data declaration.

  • Allows to move behavior from the subclasses to the superclass.

57. Pull Up Method

You have methods with identical results on subclasses. Move them to the superclass

    class Salesman extends Employee{
        String getName();
    }
    class Engineer extends Employee{
        String getName();
    }

to

    class Employee{
        String getName();
    }
    class Salesman extends Employee{}
    class Engineer extends Employee{}

Motivation

  • Eliminates duplicated behavior.

58. Pull Up Constructor Body

You have constructors on subclasses with mostly identical bodies. Create a superclass constructor; call this from the subclass methods

    class Manager extends Employee...
        public Manager (String name, String id, int grade) {
        _name = name;
        _id = id;
        _grade = grade;
    }

to

    public Manager (String name, String id, int grade) {
        super (name, id);
        _grade = grade;
    }

Motivation

  • Constructors are not the same as methods.

  • Eliminates duplicated behavior.

59. Push Down Method

Behavior on a superclass is relevant only for some of its subclasses. Move it to those subclasses.

    class Employee{
        int getQuota();
    }
    class Salesman extends Employee{}
    class Engineer extends Employee{}

to

    class Salesman extends Employee{
        int getQuota();
    }
    class Engineer extends Employee{}

Motivation When a method makes only sense in the subclass.

60. Push Down Field

A field is used only by some subclasses. Move the field to those subclasses

    class Employee{
        int quota;
    }
    class Salesman extends Employee{}
    class Engineer extends Employee{}

to

    class Salesman extends Employee{
        int quota;
    }
    class Engineer extends Employee{}

Motivation When a field makes only sense in the subclass.

61. Extract Subclass

A class has features that are used only in some instances. Create a subclass for that subset of features

    class JobItem    {
        getTotalPrices()
        getUnitPrice()
        getEmployee()
    }

to

    JobItem    {
        getTotalPrices()
        getUnitPrice()
    }
    class class LabotItem extends JobItem    {
        getUnitPrice()
        getEmployee()
    }

Motivation When a class has behavior used for some instances of the class and not for others.

62. Extract Superclass

You have two classes with similar features. Create a superclass and move the common features to the superclass

    class Department{
        getTotalAnnualCost()
        getName()
        getHeadCount
    }
    class Employee{
        getAnnualCost()
        getName()
        getId
    }

to

    class Party{
        getAnnualCost()
        getName()
    }
    class Department {
        getAnnualCost()
        getHeadCount
    }
    class Employee {
        getAnnualCost()
        getId
    }

Motivation When two classes that do similar things in the same way or similar things in different ways.

63. Extract Interface

Several clients use the same subset of a class's interface, or two classes have part of their interfaces in common. Extract the subset into an interface

    class Employee {
        getRate()
        hasSpecialSkill()
        getName()
        getDepartment()
    }

to

    interface Billable    {
        getRate()
        hasSpecialSkill()
    }
    class Employee implements Billable    {
        getRate
        hasSpecialSkill()
        getName()
        getDepartment()
    }

Motivation

  • If only a particular subset of a class's responsibilities is used by a group of clients.

  • If a class needs to work with any class that can handle certain requests.

  • Whenever a class has distinct roles in different situations.

64. Collapse Hierarchy

A superclass and subclass are not very different. Merge them together

    class Employee{    }
    class Salesman extends Employee{    }

to

    class Employee{    }

Motivation When a subclass that isn't adding any value.

65. Form Template Method

You have two methods in subclasses that perform similar steps in the same order, yet the steps are different. Get the steps into methods with the same signature, so that the original methods become the same. Then you can pull them up

    class Site{}
    class ResidentialSite extends Site{
        getBillableAmount()
    }
    class LifelineSite extends Site{
        getBillableAmount()
    }

to

    class Site{
        getBillableAmount()
        getBaseAmount()
        getTaxAmount()
    }
    class ResidentialSite extends Site{
        getBaseAmount()
        getTaxAmount()
    }
    class LifelineSite extends Site{
        getBaseAmount()
        getTaxAmount()
    }

Motivation

  • Use Inheritance with polymorphism for eliminating duplicate behavior that is slightly different.

  • When there are two similar methods in a subclass, bring them together in a superclass.

66. Replace Inheritance with Delegation

A subclass uses only part of a superclasses interface or does not want to inherit data. Create a field for the superclass, adjust methods to delegate to the superclass, and remove the subclassing

    class Vector{
        isEmpty()
    }

    class Stack extends Vector {}

to

    class Vector {
        isEmpty()
    }

    class Stack {
        Vector vector
        isEmpty(){
            return vector.isEmpty()
        }
    }

Motivation

  • Using delegation makes clear that you are making only partial use of the delegated class.

  • You control which aspects of the interface to take and which to ignore.

Mechanics

67. Replace Delegation with Inheritance

You're using delegation and are often writing many simple delegations for the entire interface. Make the delegating class a subclass of the delegate

    class Person {
        getName()
    }

    class Employee {
        Person person
        getName(){
            return person.getName()
        }
    }

to

    class Person{
        getName()
    }
    class Employee extends Person{}

Motivation

  • If you are using all the methods of the delegate.

  • If you aren't using all the methods of the class to which you are delegating, you shouldn't use it.

  • Beware of is that in which the delegate is shared by more than one object and is mutable. Data sharing is a responsibility that cannot be transferred back to inheritance.

12. BIG REFACTORINGS

68. Tease Apart Inheritance

You have an inheritance hierarchy that is doing two jobs at once. Create two hierarchies and use delegation to invoke one from the other

    class Deal{}
    class ActiveDeal extends Deal{}
    class PassiveDeal extends Deal{}
    class TabularActiveDeal extends ActiveDeal{}
    class TabularPassiveDeal extends PassiveDeal{}

to

    class Deal{
        PresentationStyle presettationStyle;
    }
    class ActiveDeal extends Deal{}
    class PassiveDeal extends Deal{}

    class PresentationStyle{}
    class TabularPresentationStyle extends PresentationStyle{}
    class SinglePresentationStyle extends PresentationStyle{}

Motivation

  • Tangled inheritance leads to code duplication.

  • If every class at a certain level in the hierarchy has subclasses that begin with the same adjective, you probably are doing two jobs with one hierarchy.

Mechanics Note Identify the different jobs being done by the hierarchy. Create a two-dimensional(or x-dimensional) grid and label the axes with the different jobs.

Deal

Active Deal

Passive Deal

Tabular Deal

69. Convert Procedural Design to Objects

You have code written in a procedural style. Turn the data records into objects, break up the behavior, and move the behavior to the objects

    class OrderCalculator{
        determinePrice(Order)
        determineTaxes(Order)
    }
    class Order{}
    class OrderLine{}

to

    class Order{
        getPrice()
        getTaxes()
    }
    class OrderLine{
        getPrice()
        getTaxes()
    }

Motivation Use OOP (at least in JAVA)

70. Separate Domain from Presentation

You have GUI classes that contain domain logic. Separate the domain logic into separate domain classes

    class OrderWindow{}

to

    class OrderWindow{
        Order order;
}

Motivation

  • Separates two complicated parts of the program into pieces that are easier to modify.

  • Allows multiple presentations of the same business logic.

  • Its worth is proved

71. Extract Hierarchy

You have a class that is doing too much work, at least in part through many conditional statements. Create a hierarchy of classes in which each subclass represents a special case

    class BillingScheme{}

to

    class BillingScheme{}
    class BusinessBillingScheme extends BillingScheme{}
    class ResidentialBillingScheme extends BillingScheme{}
    class DisabilityBillingScheme extends BillingScheme{}

Motivation

  • A class as implementing one idea become implementing two or three or ten.

  • Keep the Single Responsibility Principle.

Last updated