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.
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this communityI 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".
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.
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.
printf("%d %d\n", strlen(a), strlen(b));
instead of using intermediary variables.
Nov 17, 2011 at 7:50
func1(func2(func3(x)))
. Or technically, it refers to the functions themselves: if g(x) = f1(f2(f3(x)))
, then g
(not g(x)
) is the composition of f1
, f2
, and f3
. While I could see a way in which you could call the structure in the question "function composition," it strikes me as a very unusual use of the term, in a mathematical sense.
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.
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.
func1(func2, func3)
. Note the absent parenthesis.
Nov 18, 2011 at 5:23
func2
and func3
could be higher-order functions themselves and return a new function when called.
Jan 9, 2020 at 11:33
obj.func1().func2() is called method chaining
- Correction: It's called a train wreck.obj.func1()
always returns an object that hasfunc2()
as a member function.