October 8, 2017

...Learn TDD with Codemanship

Manual Refactoring : Introduce Parameter

Oftentimes I find myself extracting methods to remove duplication that start with hardcoded literals that need to be parameterised to make the new method reusable.

To introduce a parameter we need to do it in a single step, before we can run the tests again.

In this simple example, I want to parameterise the newly extracted factory method create_quote so that I can reuse it on the other two tests.

First, add a parameter to the method signature for the price per square metre.

Then cut the literal value for price inside the method and replace it with a reference to the new parameter.

Now paste the literal value we replaced as a parameter value in the method call. (If the method is being called in multiple places, you'll need to repeat this everywhere it's called.)


Rinse and repeat for room width and length, running the tests after completing each refactoring.

For an extra flourish, let's inline the local variable carpet_quote inside that method to simplify.


Now we can reuse this method in the other two tests.

Posted 3 years, 2 months ago on October 8, 2017