Replace Command with Function - Instance to Static

This is one of the refactorings in Martin Fowler’s book. The basic idea is to change a method that needs an instance of a class with a freestanding function. In languages like Java and C# you’d probably call it ‘instance to static’. This is the inverse of Replace Function with Command

Examine

Usually the prompt for this refactoring is unnecessary complexity - a plain function can be simpler than constructing an object and calling a method on it.

Prepare

Identify the command you want to turn into a function, and find all the places it is used.

Implement

Clear

Check again whether the new function is in the right place.

Follow up

Check whether the function has feature envy on any of its arguments. Perhaps it should be moved and become a method on that class - in which case you can use the inverse of this refactoring - Replace Function with Command.

Prompted by Code Smells

Relevant Learning Hours

Sources

Back to All Refactorings