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
}



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().

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 7/27/14, 1:55 PM, "Bert Gunter" <gunter.ber...@gene.com> wrote:

>Just an (ignorable) opinion....
>
>I'm not sure I would agree on the exception handling view, but maybe
>it often boils down to:
>
>Do you prefer:
>
>a) function(...)
>{
>if(cond1) {do one} else{
>if(cond2) {do two}} else {
>if(cond3) {do three}}
>results
>}
>
>## versus
>
>b) function(...)
>{
>if(cond1) {do one; return(one)}
>if(cond2) {do two; return(two)}
>do three; return(three)
>}
>
>
>Personally, I find the logic of the first clearer than the second, but
>others may disagree. Or may disagree with my premise altogether.
>
>I would imagine programming sites have discussed these issues
>extensively, and that would probably be a better place to look for
>thoughts anyway.
>
>Cheers,
>Bert
>
>Bert Gunter
>Genentech Nonclinical Biostatistics
>(650) 467-7374
>
>"Data is not information. Information is not knowledge. And knowledge
>is certainly not wisdom."
>Clifford Stoll
>
>
>
>
>On Sun, Jul 27, 2014 at 11:37 AM, Jeff Newmiller
><jdnew...@dcn.davis.ca.us> wrote:
>> Well, he did say it was his opinion. Goto has been pretty effectively
>>eliminated from modern programming languages, while return has not.
>>
>> IMHO the nature of the return statement resembles exception handling
>>more than normal control flow... so I avoid using it. Exceptions are
>>exceptional, and normal control flow leads to the end of the function.
>> 
>>-------------------------------------------------------------------------
>>--
>> Jeff Newmiller                        The     .....       .....  Go
>>Live...
>> DCN:<jdnew...@dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live
>>Go...
>>                                       Live:   OO#.. Dead: OO#..  Playing
>> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
>> /Software/Embedded Controllers)               .OO#.       .OO#.
>>rocks...1k
>> 
>>-------------------------------------------------------------------------
>>--
>> Sent from my phone. Please excuse my brevity.
>>
>> On July 27, 2014 11:12:54 AM PDT, Spencer Graves
>><spencer.gra...@structuremonitoring.com> wrote:
>>>On 7/27/2014 10:34 AM, William Dunlap wrote:
>>>> This is a real hack, but you can redefine return in your function:
>>>>> f <- function() {
>>>> +     return("early return")
>>>> +     "last value in function"
>>>> + }
>>>>> f()
>>>> [1] "early return"
>>>>> f <- function() {
>>>> +     return <- function(x)x
>>>> +     return("early return")
>>>> +     "last value in function"
>>>> + }
>>>>> f()
>>>> [1] "last value in function"
>>>>
>>>> IMO, well written functions do not have return statements in them.
>>>They
>>>> are the equivalent of goto statements.
>>>
>>>
>>>       Is that a fortune or something hotly contested?
>>>
>>>
>>>      I can understand the sentiment, and I'd like to know if there is
>>>research behind this?  I understand that "goto" was eliminated from
>>>modern languages precisely because research indicated it was a major
>>>source of problems.  This may seem related, but I'd like to see the
>>>data
>>>if anyone knows of such.  I've used "return" in the middle of functions
>>>
>>>to avoid an extra "else" layer after an "if". This may not be smart.
>>>I'd like to know how stupid it is ;-)
>>>
>>>
>>>       Thanks for the comment.
>>>
>>>
>>>       Spencer
>>>
>>>> Bill Dunlap
>>>> TIBCO Software
>>>> wdunlap tibco.com
>>>>
>>>>
>>>> On Sun, Jul 27, 2014 at 6:41 AM, super <desolato...@163.com> wrote:
>>>>> Suppose that I had a function as below:
>>>>> f<-function() {
>>>>> return(1)
>>>>> }
>>>>> i want to change the body of f  to the form like this:
>>>>> f<-function(){
>>>>> 1
>>>>> function() {}
>>>>> }
>>>>> How can i do the task using body(f) or something else solutions?
>>>>>          [[alternative HTML version deleted]]
>>>>>
>>>>> ______________________________________________
>>>>> 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.
>>
>> ______________________________________________
>> 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.
>
>______________________________________________
>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.

______________________________________________
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