是否在函数中显式调用 return [英] Explicitly calling return in a function or not

查看:46
本文介绍了是否在函数中显式调用 return的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不久前我被斥责了 来自 R 核心团队的 Simon Urbanek(我相信)推荐用户在函数末尾显式调用 return(尽管他的评论已被删除):

A while back I got rebuked by Simon Urbanek from the R core team (I believe) for recommending a user to explicitly calling return at the end of a function (his comment was deleted though):

foo = function() {
  return(value)
}

相反,他建议:

foo = function() {
  value
}

可能在这种情况下是必需的:

Probably in a situation like this it is required:

foo = function() {
 if(a) {
   return(a)
 } else {
   return(b)
 }
}

他的评论阐明了为什么不调用 return 除非绝对需要是一件好事,但这已被删除.

His comment shed some light on why not calling return unless strictly needed is a good thing, but this was deleted.

我的问题是:为什么不调用 return 更快或更好,因此更可取?

My question is: Why is not calling return faster or better, and thus preferable?

推荐答案

问题是:为什么不(明确地)调用 return 更快或更好,从而更可取?

R 文档中没有声明做出这样的假设.
主页 ?'function' 说:

There is no statement in R documentation making such an assumption.
The main page ?'function' says:

function( arglist ) expr
return(value)

不调用返回是不是更快?

function()return() 都是原始函数,function() 本身返回最后评估的值,即使不包括 return() 函数.

Both function() and return() are primitive functions and the function() itself returns last evaluated value even without including return() function.

return() 调用为 .Primitive('return') 并将最后一个值作为参数将执行相同的工作,但需要多调用一次.所以这个(通常)不必要的 .Primitive('return') 调用可以绘制额外的资源.然而,简单的测量表明,由此产生的差异非常小,因此不能成为不使用显式回报的原因.下图是根据以这种方式选择的数据创建的:

Calling return() as .Primitive('return') with that last value as an argument will do the same job but needs one call more. So that this (often) unnecessary .Primitive('return') call can draw additional resources. Simple measurement however shows that the resulting difference is very small and thus can not be the reason for not using explicit return. The following plot is created from data selected this way:

bench_nor2 <- function(x,repeats) { system.time(rep(
# without explicit return
(function(x) vector(length=x,mode="numeric"))(x)
,repeats)) }

bench_ret2 <- function(x,repeats) { system.time(rep(
# with explicit return
(function(x) return(vector(length=x,mode="numeric")))(x)
,repeats)) }

maxlen <- 1000
reps <- 10000
along <- seq(from=1,to=maxlen,by=5)
ret <- sapply(along,FUN=bench_ret2,repeats=reps)
nor <- sapply(along,FUN=bench_nor2,repeats=reps)
res <- data.frame(N=along,ELAPSED_RET=ret["elapsed",],ELAPSED_NOR=nor["elapsed",])

# res object is then visualized
# R version 2.15

上图在您的平台上可能略有不同.根据测量数据,返回对象的大小不会造成任何差异,重复次数(即使按比例放大)只是很小的差异,这在实际数据和真实算法中无法计算或使您的脚本运行速度更快.

The picture above may slightly difffer on your platform. Based on measured data, the size of returned object is not causing any difference, the number of repeats (even if scaled up) makes just a very small difference, which in real word with real data and real algorithm could not be counted or make your script run faster.

不调用返回是不是更好?

Return 是一个很好的工具,可以清楚地设计程序应该结束的代码叶子",跳出函数并返回值.

Return is good tool for clearly designing "leaves" of code where the routine should end, jump out of the function and return value.

# here without calling .Primitive('return')
> (function() {10;20;30;40})()
[1] 40
# here with .Primitive('return')
> (function() {10;20;30;40;return(40)})()
[1] 40
# here return terminates flow
> (function() {10;20;return();30;40})()
NULL
> (function() {10;20;return(25);30;40})()
[1] 25
> 

他使用什么风格取决于程序员的策略和编程风格,他可以不使用return(),因为它不是必需的.

It depends on strategy and programming style of the programmer what style he use, he can use no return() as it is not required.

R 核心程序员使用这两种方法,即.使用和不使用显式 return(),因为可以在基本"函数的源中找到.

R core programmers uses both approaches ie. with and without explicit return() as it is possible to find in sources of 'base' functions.

很多时候只使用 return()(无参数)在有条件地停止函数的情况下返回 NULL.

Many times only return() is used (no argument) returning NULL in cases to conditially stop the function.

不清楚它是否更好,因为使用 R 的标准用户或分析师看不到真正的区别.

It is not clear if it is better or not as standard user or analyst using R can not see the real difference.

我认为问题应该是:使用来自 R 实现的显式返回有什么危险吗?

或者,也许更好,编写函数代码的用户应该总是问:使用显式返回(或将要返回的对象作为代码分支的最后一片叶子)在功能代码?

Or, maybe better, user writing function code should always ask: What is the effect in not using explicit return (or placing object to be returned as last leaf of code branch) in the function code?

这篇关于是否在函数中显式调用 return的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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