8

I know there was a technical term for this. I'm just can't remember what it was.

If the title needs clarification, here is what I mean; If this is the old code:

Result foobar(Param1,Param2,Param3) {
  code that does abc
  code that does xyz
  code that does asdf
  more code that does something
}

and it's changed into:

SomeResult do_xyz(SomeParams) {
  code that does xyz
}
Result foobar() {
  code that does abc
  do_xyz(args);
  code that does asdf
  more code that does something
}
2
  • I'd have called it 'Tidying up'. I can't believe someone decided to call this something special, they must have needed filler for their book.
    – James
    Jul 2, 2012 at 23:15
  • @James: 'tidying up' is a lot more generic. It makes sense to give things that you need to talk about often names, so you can talk about them more efficiently. That is the only reason for patterns.
    – bitmask
    Jul 2, 2012 at 23:25

2 Answers 2

21

Technical term for this is Extract Method

See http://www.refactoring.com/catalog/extractMethod.html

Turn the fragment into a method whose name explains the purpose of the method.

void printOwing() {
    printBanner();

    //print details
    System.out.println ("name:    " + _name);
    System.out.println ("amount    " + getOutstanding());
}

                                                                                                         http://www.refactoring.com/catalog/arrow.gif

void printOwing() {
    printBanner();
    printDetails(getOutstanding());
}

void printDetails (double outstanding) {
    System.out.println ("name:    " + _name);
    System.out.println ("amount    " + outstanding);
}
4
  • 5
    extraction -- that's it!
    – bitmask
    Jul 2, 2012 at 19:22
  • Extract Method. You should check out Design Patterns. Jul 2, 2012 at 20:04
  • 9
    @LarryHector: I'm not sure it's (GoF) Design Patterns. There is thing called Template Method there that is a bit similar, and of course DP is fundamental read, but Extract Method as well as lots of other refactorings are in fact in other piece of literature and that is Martin Fowler's Refactoring.
    – herby
    Jul 2, 2012 at 20:16
  • 1
    @herby: You are correct, it is defined in Fowler's Refactoring, thanks for the correction. There is also a good section on the how/why of many of the refactoring methods in Michael Feathers' Working with Legacy Code. Jul 3, 2012 at 12:37
8

The process itself is called Refactoring the code, and method is called Extract Method , and it is a good match for applying SRP (single responsibility principle) to the code.

4
  • Is "Refacturing" a typo? (honest question!)
    – bitmask
    Jul 2, 2012 at 20:06
  • @bitmask It most probably is
    – marktani
    Jul 2, 2012 at 20:07
  • Yes, it is a typo, as it may seem from the links, it was intended to be "Refactoring"
    – Yusubov
    Jul 2, 2012 at 20:08
  • 5
    Refactoring is less specific. Extract Method is one of many, many kinds of refactoring. And neither one is implicitly related to SRP, although that is one of many reasons you might want to refactor.
    – pdr
    Jul 2, 2012 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.