Skip to main content
Engineering LibreTexts

11.2: Refactorings

  • Page ID
    32429
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Encapsulate Field

    Also Known As: Abstract Instance Variable [Rob99].

    Intent There is a public field. Make it private and provide accessors.

    Source: Refactoring: Improving the Design of Existing Code [FBB+99].

    Referenced from: Eliminate Navigation Code.

    Extract Method

    Intent You have a code fragment that can be grouped together. Turn the fragment into a method whose name explains the purpose of the method.

    Source: Refactoring: Improving the Design of Existing Code [FBB+99].

    Referenced from: Refactor to Understand, Visualize Code as Dotplots, Move Behavior Close to Data.

    Move Method

    Intent A method is, or will be, using or used by more features of another class than the class on which it is defined. Create a new method with a similar body in the class it uses most. Either turn the old method into a simple delegation, or remove it altogether.

    Source: Refactoring: Improving the Design of Existing Code [FBB+99].

    Referenced from: Refactor to Understand, Move Behavior Close to Data.

    Rename Attribute

    Intent Rename an instance variable and update all references to it.

    Source: Practical Analysis for Refactoring [Rob99].

    Referenced from: Refactor to Understand.

    Rename Method

    Intent The name of a method does not reveal its purpose. Change the name of the method.

    Source: Refactoring: Improving the Design of Existing Code [FBB+99].

    Referenced from: Refactor to Understand.

    Replace Conditional with Polymorphism

    Intent You have a conditional that chooses different behavior depending on the type of an object. Move each leg of the conditional to an overriding method in a subclass. Make the original method abstract.

    Source: Refactoring: Improving the Design of Existing Code [FBB+99].

    Referenced from: Transform Client Type Checks.


    This page titled 11.2: Refactorings is shared under a CC BY-SA license and was authored, remixed, and/or curated by Serge Demeyer, Stéphane Ducasse, Oscar Nierstrasz.

    • Was this article helpful?