Software Maintenance - Refactoring

Software Engineering
1.0x

Software Maintenance - Refactoring

Created 3 years ago

Duration 0:07:00
lesson view count 327
Select the file type you wish to download
Slide Content
  1. Software Maintenance: Refactoring

    Slide 1 - Software Maintenance: Refactoring

    • Emerson Murphy-Hill
    • Creative Commons Attribution 4.0 License.
    • Material Created by NCSU Software Engineering Faculty.
  2. Bad Smells

    Slide 2 - Bad Smells

    • “If it stinks, change it.” -- Grandma Beck, discussing child-rearing philosophy
    • Duplicate code
    • Classes, Methods that are too long
    • Switch statements, instead of inheritance
    • Over-dependence of primitive types, instead of more domain-specific types
    • Comments
    • Feature Envy
    • . . .
  3. Extract Method

    Slide 3 - Extract Method

    • You have a code fragment that can be grouped together
    • Turn the fragment into a method whose name explains the purpose of the method
    • void printOwing(double amount) { printBanner();
    • //print details
    • System.out.println (“name” + _name); System.out.println (“amount” + amount);}
    • void printOwing(double amount) { printBanner(); printDetails(amount);}
    • void printDetails(double amount); {
    • System.out.println (“name” + _name); System.out.println (“amount” + amount);}
  4. Extract Method - 2

    Slide 4 - Extract Method - 2

    • Increases the chances of reuse
    • Allows higher-level methods to read more like a series of comments
    • Steps:
    • Create a new method and name it after the intention of the method (what it does)
    • Copy extracted source code into new target method. Replace extracted code with method call
    • Scan extracted code for references to variable local to source method  make parameters
    • Local variable modified by extracted code?
    • One? Return as result
    • More? Can’t extract as it stands
  5. Introduce Parameter Object - 1

    Slide 5 - Introduce Parameter Object - 1

    • You have a group of parameters that naturally go together.
    • Replace them with an object.
  6. Introduce Parameter Object – 2

    Slide 6 - Introduce Parameter Object – 2

    • Steps:
    • Create a new class to represent the group of parameters you are replacing.
    • Modify the callers.
    • By compiling you can see if you missed any callers.
    • Once you have made this transition, look for behavior (possible methods) that can be moved into this new class.
  7. Refactoring Summary

    Slide 7 - Refactoring Summary

    • Important for maintaining proper structure of you code.
    • Even if you start with a wonderful design, it will degrade through time.
    • Complexity
    • Readability
    • Need to do it in small baby steps.
    • Definitely one refactoring at a time.
    • Make a change, see what the compiler and your unit tests tell you about the change you just made. (Goal – no regressions!)
    • Ensure everything works fine before proceeding to the next refactoring.