是更好地使用局部变量或链方法内联吗? [英] Is it better to use local variables or chain methods inline?

查看:129
本文介绍了是更好地使用局部变量或链方法内联吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一系列方法调用,每个用于下一次调用的值,我应该将它们存储在局部变量中,例如:

  DynamicForm filledForm = Form.form()。bindFromRequest(); 
String shareIdStr = filledForm.get(data [shareId]);
UUID shareId = UUID.fromString(shareIdStr);
Share share = Share.find.byId(shareId);

或作为单个调用链,如下所示:

 分享share = Share.find.byId(UUID.fromString(Form.form()。bindFromRequest()。get(data [shareId]))) 

在这种情况下,再次使用的唯一值是 code>。也许答案是在中间的某个地方,或者是完全不同的东西。

不链接方法

>

ADV


  1. 提高可读性。 / p>


  2. 提供重新使用的机会。


  3. 调试变得更容易,即在特定调用时设置断点很容易。


DisADV


  1. 增加代码的长度


  2. blockquote>

    链接方法


    ADV


    1. 减少创建多个temp的需要。


    2. 语法糖

    3. 减少要写入的行数。


    DisADV


    1. 降低代码的可读性。



    2. 调试整个调用链非常困难。



    If I have a series of method invocations, the value of each used for the next call, should I store them in local variables, like so:

    DynamicForm filledForm = Form.form().bindFromRequest();
    String shareIdStr = filledForm.get("data[shareId]");
    UUID shareId = UUID.fromString(shareIdStr);
    Share share = Share.find.byId(shareId);
    

    or as a single invocation chain, like so:

    Share share = Share.find.byId(UUID.fromString(Form.form().bindFromRequest().get("data[shareId]")));
    

    In this case, the only value that is used again is share. Perhaps the answer is somewhere in-between, or is something completely different. What's your opinion?

    解决方案

    Not chaining Methods :

    ADV

    1. Enhances readability.

    2. Gives an opportunity for re-usage.

    3. Pin pointing exceptions (if any) becomes easier.

    4. Debugging becomes easier, i.e. setting breakpoints on specific invocation is easy.

    DisADV

    1. Increases length( I wont say size :) ) of code.

    2. IDE warnings (if any).

    Chaining Methods

    ADV

    1. Reduces the need for creating multiple temp. variables.

    2. Is a syntactic sugar

    3. Reduces the number of lines to be written.

    DisADV

    1. Reduces readability of code.

    2. Commenting becomes difficult (if any) for particular methods called.

    3. Debugging the whole chain of invocation becomes very difficult.

    这篇关于是更好地使用局部变量或链方法内联吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆