Monday, February 29, 2016

Refactoring - parameterize method

When you have similar methods that are doing the same thing with the exception of a value, create one common method and pass the value.

Let's consider the following (ugly) class for a product:

class Product {
    var price: Float = 0
    
    func increasePriceBy5Percent() {
        price = price * 1.05
    }

    func increasePriceBy10Percent() {
        price = price * 1.1
    }

    func decreasePriceBy5Percent() {
        price = price * 0.95
    }
    
    func decreasePriceBy10Percent() {
        price = price * 0.9
    }
}

Let's take a test product and apply the methods:

let product = Product()
product.price = 100

product.increasePriceBy10Percent()
product.increasePriceBy5Percent()
product.decreasePriceBy10Percent()
product.decreasePriceBy5Percent()

We observe that the methods perform the same actions with the exception of the percentage value.

We can easily create just one method and receive the percentage as a parameter:

class Product {
    var price: Float = 0
       
    func applyPercentage(percentage:Float) {
        price = price * (1 + percentage)
    }
}

Our method calls become now:

product.applyPercentage(0.1)
product.applyPercentage(0.05)
product.applyPercentage(-0.1)
product.applyPercentage(-0.05)

Not only the code is cleaner, but it allows us more freedom. For example we can now apply an increase of 20%.

Friday, February 26, 2016

Refactoring: Lazy initialized attribute

Apply the type of refactoring when you have a number of objects that are initialized at the beginning of the program and that take some time to be created. This is causing some delay.

Instead of initializing them at the start, do it only when they are needed.

Let's consider the following example.

We have an object called SlowClass that takes some time to be created.

class SlowClass {
    init(index:Int){
        //this takes a long time
    }
}

We also have a repository that needs 1000 instances of this object

Prevent that

Thursday, February 25, 2016

Refactoring: Introduce Null object

Some of the objects in our programs can have null values (nil in Swift).

The functionality is different if we know the value for those objects or not, so we might need to check in multiple places for that. 

Let's consider the following code:

Wednesday, February 24, 2016

Refactoring in Swift: Introduce named parameter

Functions in Swift allow two types of parameters: named and unnamed.

When calling the function, for the named parameters their name has to be provided. For the unnamed, just the value of the parameter is sufficient.

Sometimes it makes sense to have unnamed parameters.

Pair programming

Tuesday, February 23, 2016

Refactoring - Introduce Assertion

An assertion in Swift is a run time check for a particular condition to be true. If that is the case, the execution of the program continues. If not, the code execution stops and the program is terminated.

Assertions are disabled when the app is built for production, so the users will not see their app crashing.

One can use assertion for verifying a condition is fulfilled in the case when the current implementation excepts it to be always fulfilled.