Slice

Emily Bache first learned this refactoring from Llewellyn Falco. This description was written in collaboration with Willem van den Ende

Examine

Look for a long function that you want to get under test, where there is a statement somewhere in the middle that makes it hard to test. Usually it refers to a dependency you don’t want to include in your unit test. This becomes a candidate for ‘slice’ if you cannot easily slide this function to the start or the end of the function in order to use Peel.

Prepare

Isolate the smallest possible part that you want to be able to slice out of this function. This will be the part that will be replaced by a mock in a test, so you want to make it as little logic as possible. You might want to extract a variable for the part you will slice so it’s separated from the surrounding code.

Implement

Clear

The new code may look more complex because of the lambda parameter. Improve the names of the parameter and any variables that use it to make the code as readable as possible.

Follow up

Prompted by Code Smells

Relevant Learning Hours

Sources

Back to All Refactorings