To better illustrate the different types of refactorings, the following examples use JustCode, a Visual Studio developer productivity tool which is a part of the Just family of products. Applying those examples is easy. Just download a free 30 day trial of JustCode and give it a try.

Note that this page will be regularly updated with more practical examples. If you would like to contribute with your own example or you have an idea how to make this page more helpful, just send us an email. Other useful resources and product-related tutorials can be found in our JustCode Resources page. If you want to learn more about the process of refactoring, go back to the Introduction to Refactoring page.

Refactoring Examples

  • Abstraction Refactorings

    Abstraction refactorings increase the ability for code to represent its meaning while abstracting away implementation details.

    Encapsulate Field

    Classes should not give direct access to their fields. Encapsulate Field refactors fields to property methods, allowing for an abstraction of the implementation details.

    Listing 1 – Class giving direct access to fields

    After applying the Encapsulate Field Refactoring, there is now a level of isolation between the class’ fields and calling classes.

    Listing 2 – Class with Encapsulated Fields

    JustCode Steps: “Convert field to auto-implemented property” and “Convert to property with backing field” refactorings

    Extract Interface Refactoring

    Extracting a class’ interface helps to follow the Dependency Inversion Principle by abstracting the implementation details away from using the class.

    Listing 3 – Interface for class in Listing 2

    JustCode Steps: “Extract Interface…”

    Convert Interface to Abstract Class

    Interfaces do not contain implementation details, and are the ultimate abstraction. Sometimes, some level of shared implementation is required, and abstract classes provide the ability for core implementations.

    Listing 4 – IMyClass Interface converted to an Abstract Class

    JustCode Steps: “Convert Interface to abstract class”

    Pull members to Base Types

    Moves a member from a derived class into its base class.

    Listing 5 – Class with added method

    Listing 6 – Method added to abstract base class

    JustCode Steps: “Pull Members to Base Type…”

  • Collaboration Refactorings

    Extract Class from Parameters

    This creates a class from the parameters passed into a method.

    Listing 7 – Method with parameters

    Listing 8 – Updated method

    Listing 9 – Class created from the parameters

    JustCode Steps: “Extract Class from Parameters”

    Move or Delete Parameter

    Changing the order of method/constructor parameters can help with readability and usability of code. Deleting parameters trims the interface to make code easier to work with.

    Listing 10 – Moving a parameter

    JustCode Steps: “Move or Delete Parameters” -> , to move, to remove

    Introduce Parameter

    Adds an additional parameter into a method/constructor signature. This can be a brand new parameter, or an object that is instantiated in a method.

    JustCode Steps: “Introduce Parameter”