On 27/07/2014, 7:29 PM, MacQueen, Don wrote:
> As long as people are sharing their preferences . . .
> 
> 
> I find return() useful in a scenario like the following:
> 
> Myfun <- function() {
>   {a few lines of code}
>   if (condition) return(whatever)
>   {Many, many lines of code}
>   Results
> }
> 
> Which I find preferable to
> 
> Myfun <- function() {
>   { a few lines of code}
>   if (condition) {
>     Results <- something
>   } else {
>     {Many, many lines of code}
>     Results <- something.else
>   }
>   Results
> }
> 

I tend to agree with you, but wanted to point out a third possibility:


 Myfun <- function() {
   { a few lines of code}
   if (condition) {
     something
   } else {
     {Many, many lines of code}
     something.else
   }
}

In some sense this is the most "R-like", but I like it the least.

Duncan Murdoch
> 
> It is the presence of those many lines of code which separate the opening
> and closing brackets after the else that make the former easier to read
> and understand (again in my opinion).
> 
> I guess this is more along the lines of exception handling.
> 
> Also note that this is something of a special case; I donĀ¹t in general
> advocate using return().
>

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to