Seen regularly in the code and quite often used myself mostly to save myself a few lines.
class AAA {
int val = 0;
}
AAA createSomething(){
return new AAA();
}
AAA someProcessing(AAA aaa, int someOtherParameters ) {
aaa.val += someOtherParameters;
return aaa;
}
AAA someMoreProcessing(AAA aaa, int someOtherParameters ) {
aaa.val ^= someOtherParameters;
return aaa;
}
so that you can daisy chain the otherwise purely imperative methods in one-liners like
AAA calculate( int param1, int param2 ) {
return someMoreProcessing(someProcessing(createSomething(),param1,param2);
}
as opposed to the classical way
AAA createSomething(){
return new AAA();
}
void someProcessing(AAA aaa, int someOtherParameters ) {
aaa.val += someOtherParameters;
}
void someMoreProcessing(AAA aaa, int someOtherParameters ) {
aaa.val ^= someOtherParameters;
}
AAA calculate( int param1, int param2 ) {
AAA aaa = createSomething();
someProcessing(aaa,param1);
someMoreProcessing(aaa,param2);
return aaa;
}
There is of course some potential for abuse if you go over board with the daisy chaining like return a(b(c(d(),e(),f(g())), h(i()),j()));, but let's assume you don't.
On the one hand it will save you some typing and local variables, on the other hand you then always have to return the value from each function, which is additional lines of code and space on the stack. So the costs/benefits depends on how many and how big methods you have and how often you will use the daisy chaining.
So the question is three-fold.
- does this have a name ?
- is it considered bad practice in general ? ( i.e. because of the aforementioned abuse potential )
- what is the performance cost/benefit of it ? ( my guess is, it depends heavily on the circumstances, but then, under which circumstances it improves performance and under which it reduces it ? )