Do you use `return`

Do you include `return` when you write functions?

  • I always include `return`

    Votes: 0 0.0%
  • I only include `return` when doing an early exit from the function

    Votes: 0 0.0%
  • I never use `return`

    Votes: 0 0.0%

  • Total voters
    3

Dason

Ambassador to the humans
#1
When you write functions do you include a `return` statement? It isn't necessary in most cases since R will return the last evaluated expression as what it returns when it reaches the end of the function. I know some people feel passionately that since it isn't necessary that it's misguided and introduces clutter. Some people think it provides clarity. What are your opinions on include return. Here are some examples for either

Using returns:
C-like:
monitor_object <- function(object_name, FUN = dim){
    
    # Get
    fun_name <- deparse(substitute(FUN))
    print(fun_name)
    object_dim <- NULL
    if(exists(object_name)){
        object_output <- FUN(get(object_name))
    }
    
    callback_function <- function(...){
        new_output <- NULL
        if(exists(object_name)){
            new_output <- FUN(get(object_name))
        }
        if(!identical(new_output, object_output)){
            
            
            msg <- paste0(object_name, " changed. Summary function: ", fun_name, 
                         "\nOld output: ", paste(object_output, collapse = " "),
                         "\nNew output: ", paste(new_output, collapse = " "))
            object_output <<- new_output
            message(msg)
        }
        return(TRUE)
    }
    
    return(callback_function)
}
without returns:
C-like:
monitor_object <- function(object_name, FUN = dim){
    
    # Get
    fun_name <- deparse(substitute(FUN))
    print(fun_name)
    object_dim <- NULL
    if(exists(object_name)){
        object_output <- FUN(get(object_name))
    }
    
    callback_function <- function(...){
        new_output <- NULL
        if(exists(object_name)){
            new_output <- FUN(get(object_name))
        }
        if(!identical(new_output, object_output)){
            
            
            msg <- paste0(object_name, " changed. Summary function: ", fun_name, 
                         "\nOld output: ", paste(object_output, collapse = " "),
                         "\nNew output: ", paste(new_output, collapse = " "))
            object_output <<- new_output
            message(msg)
        }
        TRUE
    }
    
    callback_function
}
 

Dason

Ambassador to the humans
#3
Do not understand why C is releveant here.
Are you saying that because the code says "C-like"? There isn't an option for R but in terms of syntax highlighting I think the best you could describe R as with the options given is "C-like" since R does use a lot of similar keywords.
 

Dason

Ambassador to the humans
#12
If you don't specify a language then it just defaults to "Code". Specifying a language will do things like give you some syntax highlighting and give some indication which language the code is. There isn't an "R" option but the syntax of R is similar enough to C that the option "C-like" gives some appropriate syntax highlighting.
 

trinker

ggplot2orBust
#13
What about
Code:
> f <- function(x, ...){if(x < 5) "Low" else "High"}
> f(3)
[1] "Low"
> f(7)
[1] "High"
Yeah but to me the return is more readable. Plus they can get nested. There may be 3-5 of these if() return() lines in there. It helps to avoid a bunch of nested if/elses.