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
}
gnat
  • 21,442
  • 29
  • 112
  • 288
bitmask
  • 879
  • 2
  • 8
  • 19
  • 1
    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 02 '12 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 02 '12 at 23:25

2 Answers2

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);
}
gnat
  • 21,442
  • 29
  • 112
  • 288
herby
  • 2,734
  • 1
  • 18
  • 25
  • 5
    *extraction* -- that's it! – bitmask Jul 02 '12 at 19:22
  • Extract Method. You should check out *Design Patterns*. – Larry Hector Jul 02 '12 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 02 '12 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*. – Larry Hector Jul 03 '12 at 12:37
  • Some IDE's support this pattern. I usually write big methods, then (in the case of Visual Studio IDE and C#) use the IDE's built-in extractor to select lines of that method to split into separate methods. It has the added benefit forcing you to think about the responsibilities. – Zimano Aug 18 '22 at 14:32
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.

Yusubov
  • 21,328
  • 6
  • 45
  • 71