V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
rebeccaMyKid
V2EX  ›  问与答

为什么在 ruby 里, use implicit return 是 最佳实践?

  •  
  •   rebeccaMyKid · 2016-05-31 16:44:53 +08:00 · 1263 次点击
    这是一个创建于 3035 天前的主题,其中的信息可能已经有所发展或是发生改变。

    It ’ s best practice to use the implicit return, and only include return explicitly when you ’ re returning a line that ’ s not at the end of the method.

    def three_plus_two
      return 3 + 2
    end
    

    best practice:

    def three_plus_two
      3 + 2
    end
    
    3 条回复    2016-05-31 21:13:35 +08:00
    kikyous
        1
    kikyous  
       2016-05-31 16:48:36 +08:00   ❤️ 1
    return 能省就省
    ChiangDi
        2
    ChiangDi  
       2016-05-31 16:50:29 +08:00 via Android   ❤️ 1
    lisp 继承过来的吧,最后一个表达式作为返回值
    msg7086
        3
    msg7086  
       2016-05-31 21:13:35 +08:00   ❤️ 1
    因为如果你遵照最佳实践的话,你会发现最末的 return 是毫无必要的,徒增无用代码量。
    (因为一个方法一般不会超过 10 行。)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2448 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 06:26 · PVG 14:26 · LAX 23:26 · JFK 02:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.