rebeccaMyKid
V2EX  ›  问与答

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

  •  
  •   rebeccaMyKid · May 31, 2016 · 1868 views
    This topic created in 3817 days ago, the information mentioned may be changed or developed.

    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 replies  •  2016-05-31 21:13:35 +08:00
    kikyous
    kikyous
    May 31, 2016❤️1
    1
    return 能省就省
    ChiangDi
    ChiangDi
    May 31, 2016 via Android❤️1
    2
    lisp 继承过来的吧,最后一个表达式作为返回值
    msg7086
    msg7086
    May 31, 2016❤️1
    3
    因为如果你遵照最佳实践的话,你会发现最末的 return 是毫无必要的,徒增无用代码量。
    (因为一个方法一般不会超过 10 行。)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1024 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 19:12 · PVG 03:12 · LAX 11:12 · JFK 14:12
    ♥ Do have faith in what you're doing.