Thursday, March 10, 2016

Refactoring - Remove the paramenter

This refactoring is so simple, we'll not even give an example to prove it.

With functionality change, some of the parameters of a function become obsolete and not used.

When that is the case, of course, they need to be removed.

Easier said than done in some cases, especially when the functions are part of a library used by other parties.

In that case, we can keep the function with the extra non used parameter, deprecated it, and create another function with the correct parameters.

At least the new users of the function will know to use the correct version.

Let's assume the following function:

func testFunction(param1:Int, param2: Int, unusedParam: Int) {
}

After few iterations, unusedParam is not used anymore. We can remove it right away, but this will break few other programs that are using our function. 

So instead of doing that, we can mark it as deprecated and create a new, refactored version as follows:

@available(*, deprecated=1.0, message="unusedParam is not used anymore")
func testFunction(param1:Int, param2: Int, unusedParam: Int) {
    testFunction(param1, param2: param2)
}

func testFunction(param1:Int, param2: Int) {
    //move here the code from the previous function
}


No comments:

Post a Comment