在 Ruby 中显式返回是一种很好的风格吗? [英] Is it good style to explicitly return in Ruby?

查看:29
本文介绍了在 Ruby 中显式返回是一种很好的风格吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 Python 背景,在风格方面总是有一种正确的方法"(一种Pythonic"方式),我想知道 Ruby 是否存在同样的方法.我一直在使用我自己的风格指南,但我正在考虑发布我的源代码,我希望它遵守任何可能存在的不成文规则.

Coming from a Python background, where there is always a "right way to do it" (a "Pythonic" way) when it comes to style, I'm wondering if the same exists for Ruby. I've been using my own style guidelines but I'm thinking about releasing my source code, and I'd like it to adhere to any unwritten rules that might exist.

在方法中显式输入 return 是Ruby 方式"吗?我已经看到有和没有完成,但是有正确的方法吗?是否有合适的时间去做?例如:

Is it "The Ruby Way" to explicitly type out return in methods? I've seen it done with and without, but is there a right way to do it? Is there maybe a right time to do it? For example:

def some_func(arg1, arg2, etc)
  # Do some stuff...
  return value # <-- Is the 'return' needed here?
end

推荐答案

老问题(和已回答"),但我会投入两分钱作为答案.

Old (and "answered") question, but I'll toss in my two cents as an answer.

TL;DR - 您不必这样做,但在某些情况下它可以使您的代码更加清晰.

TL;DR - You don't have to, but it can make your code a lot more clear in some cases.

尽管不使用显式返回可能是Ruby 方式",但对于使用不熟悉代码或不熟悉 Ruby 的此功能的程序员来说,这会让人感到困惑.

Though not using an explicit return may be "the Ruby way", it's confusing to programmers working with unfamiliar code, or unfamiliar with this feature of Ruby.

这是一个有点人为的例子,但想象一下有一个像这样的小函数,它将传递的数字加一,并将其​​分配给一个实例变量.

It's a somewhat contrived example, but imagine having a little function like this, which adds one to the number passed, and assigns it to an instance variable.

def plus_one_to_y(x)
    @y = x + 1
end

这是一个返回值的函数,还是不是?很难说开发者的意思,因为它既分配了实例变量,又返回了分配的值.

Was this meant to be a function that returned a value, or not? It's really hard to say what the developer meant, as it both assigns the instance variable, AND returns the value assigned as well.

假设很久以后,另一个程序员(可能不太熟悉 Ruby 如何根据执行的最后一行代码返回)来了,想要放入一些打印语句进行日志记录,函数变成了这样……

Suppose much later, another programmer (perhaps not that familiar with how Ruby does returns based on last line of code executed) comes along and wants to put in some print statements for logging, and the function becomes this...

def plus_one_to_y(x)
    @y = x + 1
    puts "In plus_one_to_y"
end

现在函数被破坏了如果有任何东西需要返回值.如果没有什么期望返回值,那很好.显然,如果在代码链下游的某个地方,调用 this 的东西期望返回值,它将失败,因为它没有返回它期望的值.

Now the function is broken if anything expects a returned value. If nothing expects a returned value, it's fine. Clearly if somewhere further down the code chain, something calling this is expecting a returned value, it's going to fail as it's not getting back what it expects.

现在真正的问题是:有什么东西真的期待返回值吗?这是否打破了什么?将来会破坏某些东西吗?谁知道!只有对所有调用进行完整的代码审查才能让您知道.

The real question now is this: did anything really expect a returned value? Did this break something or not? Will it break something in the future? Who knows! Only a full code review of all calls will let you know.

所以至少对我来说,最佳实践方法是要么非常明确地返回重要的东西,要么在不重要的情况下根本不返回.

So for me at least, the best practice approach is to either be very explicit that you are returning something if it matters, or return nothing at all when it doesn't.

所以在我们的小演示函数的情况下,假设我们希望它返回一个值,它会写成这样......

So in the case of our little demo function, assuming we wanted it to return a value, it would be written like this...

def plus_one_to_y(x)
    @y = x + 1
    puts "In plus_one_to_y"
    return @y
end

而且任何程序员都非常清楚它确实返回了一个值,而且他们更难在没有意识到的情况下破坏它.

And it would be very clear to any programmer that it does return a value, and much harder for them to break it without realizing it.

或者,你可以这样写,省略 return 语句......

Alternatively, one could write it like this and leave out the return statement...

def plus_one_to_y(x)
    @y = x + 1
    puts "In plus_one_to_y"
    @y
end

但是为什么要省略返回这个词呢?为什么不把它放在那里,让它 100% 清楚发生了什么?它实际上不会影响您的代码的执行能力.

But why bother leaving out the word return? Why not just put it in there and make it 100% clear what's happening? It will literally have no impact on your code's ability to perform.

这篇关于在 Ruby 中显式返回是一种很好的风格吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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