Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java chained/nested method calls

Tags:

java

json

I'm working on a JSONObject with multiple sub-JSONObjects. Here is the way I fill the content :

myJson.getJSONObject(CAT_NAME).put(VAR_NAME, var)
                          .put(VAR_NAME2, var2)
                          .put(...);

A friend told me that it is a very bad practice to use "nested function/method calls" and that I should do it this way :

myJson.getJSONObject(CAT_NAME).put(VAR_NAME, var);
myJson.getJSONObject(CAT_NAME).put(VAR_NAME2, var2);
myJson.getJSONObject(CAT_NAME).put(...);

From my view, my way is more like an chained method call than a nested one. And I don't like the second way because it force the program to get the same object again and again when the put() method already returns it.

Is my case is a "nested function calls" case ? Is this dangerous or bad for any reason ? And what are those reasons ?

edit : I don't feel like my question is duplicated. The other question involves chained methods but it mainly talks about c# interfaces.

like image 340
Rox Teddy Avatar asked Aug 13 '15 13:08

Rox Teddy


People also ask

Can method calls be nested Java?

Java does not support “directly” nested methods. Many functional programming languages support method within method. But you can achieve nested method functionality in Java 7 or older version by define local classes, class within method so this does compile.

Is it possible to invoke chained methods in Java if so how will you invoke?

Under this procedure, we have to write the object reference once and then call the methods by separating them with a (dot.). Method chaining in Java is a common syntax to invoke multiple methods calls in OOPs. Each method in chaining returns an object.

What is a nested method call?

Nested (or inner, nested) functions are functions that we define inside other functions to directly access the variables and names defined in the enclosing function. Nested functions have many uses, primarily for creating closures and decorators.

What is method chaining Why is it bad?

The drawback to self-referential method chaining is that you communicate that multiple method calls are required to do something, and that each call builds off the last. If this is not true, then method chaining could be communicating the wrong thing to other programmers.


2 Answers

Is my case is a "nested function calls" case ?

No that is method chaining (Builder pattern).

Is this dangerous or bad for any reason ?

No. Your friend is wrong. Not at all bad practice in your case. It's quite Ok since you are building a Json.

like image 94
Suresh Atta Avatar answered Oct 12 '22 03:10

Suresh Atta


Using method chaining will actually be more efficient than the alternative you provided because myJson.getJSONObject(..) is only called once in the first case, whereas you call it many times in the second. There is a much more significant cost for calling getJSONObject(..) than there is for reusing the original object returned by it.

The correct way to accomplish this without using method chaining would be more like this:

JSONObject obj = myJson.getJSONObject(CAT_NAME);
obj.put(VAR_NAME, var);
obj.put(VAR_NAME2, var2);
obj.put(...);

Personally, I prefer to not use method chaining because I think it looks better, but ultimately it's your preference and the code here would have basically the same performance as your first chunk of code.

like image 34
Daniel Centore Avatar answered Oct 12 '22 02:10

Daniel Centore