Now THAT is a fortune: My preferences are colored by my experience looking for problems in other people's code. My own code is always easy to understand but code other people write can be difficult. :-)
On Sun, Jul 27, 2014 at 8:38 PM, William Dunlap <wdun...@tibco.com> wrote: > The problem with Don's > if (condition) { > Results <- something > } else { > Results <- somethingElse > } > is that in a long sequence of if-then-else-if... you have > to check every branch to make sure Results got assigned to > (or that the remaining branches contained a return() or a stop()). > > Duncan's version may be more readable if you explicitly assign > the value of the if-then-else to a variable and then return the variable > Results <- if (condition) { > something > } else { > somethingElse > } > Return > > I think the original poster may have wanted to put a trace on a function > that could display the return value of the function, for an arbitrary > function. > Having a return() statement makes this more difficult, since you > cannot simply make a transformation from > origFun <- function(...) body > to > newFun <- function(...) { retVal <- body ; print(retVal) ; retVal } > I think you really have to make a new function that wraps the original one, > like > newFun <- function(...) { retVal <- origFun(...) ; print(retVal) ; retval } > The latter will not work with functions that that use things like > sys.parent() so you may have to go into the body of the code and patch > those up. > > (My preferences are colored by my experience looking for problems in > other people's code. My own code is always easy to understand but > code other people write can be difficult. :-)) > > > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > > On Sun, Jul 27, 2014 at 5:14 PM, Duncan Murdoch > <murdoch.dun...@gmail.com> wrote: >> 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. > > ______________________________________________ > 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.