Transform Return Type aka Wrap Return Value

Some tools have this, Resharper includes it in the “Transform Parameters” refactoring. IntelliJ has an additional plugin that offers to Wrap Return Value. If you don’t have those tools then these steps achieve the same thing with only ‘Extract Function’ and ‘Inline Function.’

Identify

Usually you want this refactoring when a method has multiple return values that and either you’re only getting one of them, or you’re putting them in an awkward collection type before returning them.

Prepare

I usually find this one goes easier if I inline the method first, unless it’s called from several places.

Implement

Clear

Try to remove the declaration of the local variables and instead use default values in the constructor of your new class.

Follow up

Look for other methods you could extract and move to the new type. Look for Feature Envy on its fields.

Prompted by Code Smells

Relevant Learning Hours

Sources

Back to All Refactorings