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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With