3

Typically I implement classes (C#, C++) via many private functions that serve no purpose other than to separate concerns & logic for readability, maintainability, and scope. For example, if I see a function that is 100 lines long and has 3 comments in it separating it out into virtual 1/3 section chunks, that to me is 3 functions. So I break that larger function out into 3 smaller ones and now that original function only calls 3 functions.

I don't know the name for this paradigm. At first I thought it might be functional programming or modular programming, but that doesn't seem to be the case. Can anyone help me figure out what this paradigm is called? Additionally, a link to a wikipedia article I could read that talks about this exact use case would be great (Of course if you tell me the name of the paradigm I could easily look this up myself).

Thanks in advance.

void.pointer
  • 24,859
  • 31
  • 132
  • 243
  • This is one of the most common things I have to do to "legacy" code. Unfortunately, I can't find this as a built in refactoring option in Netbeans (7), and I don't remember it being in Eclipse, either. – Roboprog Jun 30 '13 at 03:52

5 Answers5

1

It's basically just "Refactoring".

http://en.wikipedia.org/wiki/Code_refactoring

Brandon Moretz
  • 7,512
  • 3
  • 33
  • 43
1

Refactoring is changing the code w/o changing its behavior. When you break code into more methods, it's called Refactoring to Method. When you take those methods and put their behaviors into many classes (which can help maintain single responsibilities per object/class), it's called Refactoring to Objects.

lance
  • 16,092
  • 19
  • 77
  • 136
1

The actual refactoring practice of creating a new, smaller method from existing code in a oversized method is called Extract Method.

Example

Original code

void printOwing() {
    printBanner();

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

After performing Extract Method

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

void printDetails (double outstanding) {
    System.out.println ("name:  " + _name);
    System.out.println ("amount " + outstanding);
}

If the code you are trying to pull out of a method doesn't even belong in the class to begin with, then you can similarly use Extract Class.

Martin Fowler, one of the premier software craftsman in the industry wrote a fantastic book on the concepts of refactoring that you can find here. This book will give you step-by-step recipes to tackle a large majority of the refactorings you will ever need.

Also, JetBrains makes some really good IDEs for almost any language that have some really nice refactoring tools built in. They also have a plugin for Visual Studio called ReSharper that provides some of the same features and benefits.

Robert Greiner
  • 29,049
  • 9
  • 65
  • 85
  • Can't find this in current IDE :-( What I want is to be able to highlight a range of lines, then select "extract *function*", with *all* inputs as input parameters, and *all* result values as multi-return or a return paramater object (depending on language), rather than data division, er, member field updates/side-effects. If I want to move some of the input params and/or return values into the "data division", I can later. (yeah, feelin' the hate for Java as COBOL, and the utter loss of the cultural ability to write pure functions) – Roboprog Jun 30 '13 at 03:56
0

The practice is called refactoring.

Refactoring came from the mathematics idea of factors. So effectively you are splitting your method up to get the same result.

Edit I do this from the get-go. I'll write a load of code and look on how I can remove duplication at ver small increments. It is part of the TDD mantra.

red. green. refactor to remove duplication.

Johnno Nolan
  • 29,228
  • 19
  • 111
  • 160
  • It's not refactoring if you implement a class from the get-go like this, for example. I'm looking for a paradigm, not a practice. – void.pointer May 31 '11 at 18:32
0

The breaking up of large functions into smaller ones is usually called Refactoring, but the act of combining smaller functions together is called Composition.

Composition is particularly powerful when your programming language supports functions as first-class entities, meaning you can pass a function as an argument to another function, which returns a new function that didn't exist until runtime.

Chris Wenham
  • 23,679
  • 13
  • 59
  • 69