Thursday, March 3, 2016

Refactoring - Pull up field

This is a simple refactoring applied when there are two subclasses with a common field. Take the field and move it to the parent class.

Consider the following class structure:

class BankAccount {
}

class CheckingAccount: BankAccount {
    var balance: Float = 0
}

class SavingAccount: BankAccount {
    var balance: Float = 0
}

The common balance field can be moved to the parent:

class BankAccount {
    var balance: Float = 0
}

class CheckingAccount: BankAccount {
}

class SavingAccount: BankAccount {
}

This is less code to write and some common functionality can be also moved up.

The purpose of this refactoring is to have as little duplication as possible,

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.