[R] How to modify the body of a function?

Bert Gunter gunter.berton at gene.com
Sun Jul 27 22:55:26 CEST 2014


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
<jdnewmil at 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:<jdnewmil at 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.graves at 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 <desolator88 at 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 at 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 at 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.



More information about the R-help mailing list