问题描述
来自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.
是否是Ruby方式在方法中显式地输出 return
?我已经看到它做和没有,但有一个正确的方法吗?是否可能有正确的时间 例如:
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.
例如,可能有一个像这样的函数,它将一个传递给一个实例变量。
For example one might have 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
这是一个返回值的函数吗?这是很难说的开发商的意思,因为它分配的实例变量,AND返回指定的值。
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
现在,如果任何需要返回值的函数被破坏了 。如果没有什么期望返回值,那就好了。显然,如果代码链上更远的地方,调用这个的东西期望返回值,它会失败,因为它没有回到它的期望。
Now the function is broken if anything expects a returned value. If nothing expects a returned value, it's fine. Clearly if somewhere farther 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.
因此,在我们的小演示函数的情况下,假设我们想要返回一个值,它将是这样写的。 ..
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语句。
Alternately, 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
但是为什么还要忽略单词return?为什么不把它放在那里,使它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中显式返回是好的风格吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!