简体   繁体   English

Java链接/嵌套方法调用

[英]Java chained/nested method calls

I'm working on a JSONObject with multiple sub-JSONObjects. 我正在研究一个带有多个子JSONObjects的JSONObject。 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. 我不喜欢第二种方式,因为当put()方法已经返回时,它会强制程序一次又一次地获取相同的对象。

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. 另一个问题涉及链式方法,但它主要讨论c#接口。

Is my case is a "nested function calls" case ? 我的情况是“嵌套函数调用”案例吗?

No that is method chaining (Builder pattern). 不,这是方法链 (Builder模式)。

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. 你正在建造一个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. 使用方法链接实际上比您提供的替代方法更有效,因为myJson.getJSONObject(..)仅在第一种情况下被调用一次,而您在第二种情况下多次调用它。 There is a much more significant cost for calling getJSONObject(..) than there is for reusing the original object returned by it. 调用getJSONObject(..)比重用它返回的原始对象要花费更多的成本。

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. 就个人而言,我更喜欢不使用方法链接,因为我认为它看起来更好,但最终它是你的偏好,这里的代码与你的第一块代码基本上具有相同的性能。

He PROBABLY considers it bad because it can be less readable (but that's a personal opinion and depends a lot on code formating, how well someone understands the specific APIs, is familiar with the concept of method chaining, etc. etc.). 他可能认为它很糟糕,因为它可读性较差(但这是个人意见,并且很大程度上取决于代码格式化,有人理解特定API的程度,熟悉方法链的概念等等)。

It's certainly not a generally bad thing to do though. 尽管如此,这当然不是一件坏事。 In fact a lot of APIs work exactly like that. 实际上很多API都是这样的。 Just look at StringBuilder in the Java standard API as a very commonly used example. 只需将Java标准API中的StringBuilder看作一个非常常用的示例。
As others already pointed out it's potentially more performant (depending on how the called methods are implemented) as well, but that's not a given. 正如其他人已经指出的那样,它可能更具性能(取决于被调用方法的实现方式),但这不是给定的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM