Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I know that obj.func1().func2() is called method chaining, but what is the technical term for:

func1(func2(), func3());

Where return of a function is used as an argument to another.

share|improve this question
8  
obj.func1().func2() is called method chaining - Correction: It's called a train wreck. –  Yam Marcovic Nov 17 '11 at 10:18
    
That sounds very, very, very tricky unless you're absolutely sure that obj.func1() always returns an object that has func2() as a member function. –  Shadur Nov 17 '11 at 11:33
7  
@Yam Care to explain? It’s employed for great profit in fluent interfaces, amongst others. Are you referring to the Law of Demeter? If so, this doesn’t forbid the above pattern, and even in cases where it does there is a good case against the LoD. –  Konrad Rudolph Nov 17 '11 at 12:47
    
method chaining lies on one shelf with syntactic sugar, no need to make a fuss over it. –  Kos Nov 17 '11 at 17:21
1  
@PauliØsterø I thought that's what stack tracing is meant for. –  Chris C Nov 17 '11 at 21:32
show 4 more comments

4 Answers

up vote 35 down vote accepted

I don't think it's function composition. Function composition means taking two or more functions and turning them into a new function, like f . g . h in Haskell. Note that no function is called at this point.

Personally, I would refer to constructs like func1(func2(), func3()) as "nested function calls".

share|improve this answer
    
+1, it's referred to as "nested function calls" in all programming contexts I've seen. –  Izkata Nov 17 '11 at 15:20
    
+1, calling this "function composition" is deceptively wrong. –  Gian Nov 17 '11 at 16:37
    
+1. All f(g()) is doing is calling f() with an expression for the first argument, and it so happens the function call g() qualifies as an expression. But if there has to be a name for it, this is as close as it gets. –  Blrfl Nov 17 '11 at 20:34
add comment

In mathematics, it's called function composition. I don't think I've heard the term applied to programming, though. That may be because the usage is largely avoided for a few reasons. It can introduce strange bugs when the functions have side effects, due to compilers being free to evaluate func3 before func2. It's more difficult to debug because you can't set breakpoints on or print out intermediate results, and most people just plain find it harder to read.

share|improve this answer
2  
+1 for function composition -- that's the right term. –  good_computer Nov 17 '11 at 5:06
4  
It's called composition in functional programming as well. –  Chuck Nov 17 '11 at 5:25
8  
I don't think it's "largely avoided". IMO it's not uncommon to write code like printf("%d %d\n", strlen(a), strlen(b)); instead of using intermediary variables. –  user281377 Nov 17 '11 at 7:50
3  
I think the solution to the strange bugs when the functions have side-effects is not to avoid this, but instead to avoid writing functions that produce values and have side-effects. Similarly, regarding the debuggers, the solution should be to ask for a better debugger, one that doesn't force you to change your code to make the debugger work. –  R. Martinho Fernandes Nov 17 '11 at 7:55
7  
Whether the compiler free to evaluate func3 before func2 depends entirely on the language being used. In Java, for example, the evaluation order is strictly specified. I'd actually suspect that this is the case for most languages. –  Michael Borgwardt Nov 17 '11 at 8:02
show 4 more comments

As @Karl Bielefeldt points, it's called function composition in Math.

There is NO technical term for this thing in programming. And I think this is a Good thing, because it indicates that the operation is normal and Orthogonal.

Orthogonality in programming languages means that you can use an instruction/operation independent of it's context. For example, you can call a function/method in all of the following ways, and it would behave the same...

f1()(f2(), f3());

x = y + f4();

if ( f5() && !f6() ) doSomething();

f7() = f8() + f9(); // in C++ when a function returns a reference

x = f10() ? f11(f12(f13(x))) : f14();

You can read more on Orthogonality in Programming on Wikipedia, and there's a question on StackOverflow on this.

share|improve this answer
    
It's not indicating orthogonality but depending on that. –  Raffael Nov 17 '11 at 11:11
    
+1 you're just passing values, there's no magic in the OP's call –  vemv Nov 17 '11 at 15:20
add comment

I really am not that good with terms, but I just read an article a few days ago that referred to the term Higher order functions, and here's an abstract of the definition per Wikipedia:

http://en.wikipedia.org/wiki/Higher-order_function

In mathematics and computer science, higher-order functions, functional forms, or functionals are functions which do at least one of the following:

  • take one or more functions as an input
  • output a function

All other functions are first-order functions. In mathematics higher-order functions are also known as operators or functionals. The derivative in calculus is a common example, since it maps a function to another function.

so in this case, since that scenario does take at least one function as an input/param, it'd be considered a higher order function, i believe.

share|improve this answer
1  
No, calling a higher order function would look like func1(func2, func3). Note the absent parenthesis. –  FredOverflow Nov 18 '11 at 5:23
    
that makes sense Fred. one's the address of a function, and the other the result of executing that function. Thanks for pointing that out. –  silverCORE Nov 18 '11 at 6:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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