Hello-
I'm looking for an opinionated view/ best practice rec on a coding pattern I run into.
Suppose we're writing functions for an app or a package: What assumptions about existing global environment variables can be made, and how should this be robustly anticipated by functions?
For instance, if we have a list whose contents will be used by various functions, is one of the following styles preferable to another?
# Some useful list, maybe stored locally a hidden file R object, which
# would be loaded via loadRDS, defined here as a list for the reprex
.credentials <- list(logon = "lisa", token = 1234)
# Option 1: assume .creds exists, but throw an error if not
option_1 <- function(x) {
stopifnot(exists(".credentials"))
.credentials$token + x
}
option_1(10)
#> [1] 1244
# Option 2: Pass an assigned value
option_2 <- function(x, token = .credentials$token) {
token + x
}
option_2(20)
#> [1] 1254
# Option 3: Pass an assigned value explicitly every time
option_2(30, token = .credentials$token)
#> [1] 1264
Created on 2018-10-14 by the reprex package (v0.2.1)
Of course, I'm thinking about more complex situations and where this might create problems down the line, and the tradeoffs between verbosity, readability, and brittleness.
I'd be grateful for any advice or pointers to any related posts, blogs etc.
Thank you.