Thursday, January 28, 2016

Refactoring - Inline temp

This refactoring is the reverse of the Explaining variable.

You added a new variable to make the code clearer. But after few changes the variable is not really useful.

Consider the following piece of code:


class User {
    var isAdmin: Bool = true
    //...
}

let user = User()

var userIsAdmin = user.isAdmin

if userIsAdmin {
    //do something

}

It is obvious that userIsAdmin is not really needed. We can easily remove it and have the following code:

class User {
    var isAdmin: Bool = true
    //...
}

let user = User()

if user.isAdmin {
    //do something
}

No comments:

Post a Comment

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