easystats / easystats/easystats

Start using look-up tables instead of using complex conditionals?

Open
#335 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Code Style :woman_technologist:
Dominant language
R
Stars
1.2k
Forks
88
PR merge metrics
No merged PRs in 30d

Description

After a certain point, a block of conditional statements can become quite difficult to read, maintain, and further extend.

To see what I mean, consider this example from {insight}:

transform_fun <- "exp"

if (transform_fun == "identity") {
  out <- list(transformation = function(x) x, inverse = function(x) x)
} else if (transform_fun == "log") {
  out <- list(transformation = log, inverse = exp)
} else if (transform_fun %in% c("log1p", "log(x+1)")) {
  out <- list(transformation = log1p, inverse = expm1)
} else if (transform_fun == "log10") {
  out <- list(transformation = log10, inverse = function(x) NA)
} else if (transform_fun == "log2") {
  out <- list(transformation = log2, inverse = function(x) NA)
} else if (transform_fun == "exp") {
  out <- list(transformation = exp, inverse = log)
} else if (transform_fun == "sqrt") {
  out <- list(transformation = sqrt, inverse = function(x) x^2)
} else if (transform_fun == "power") {
  out <- list(transformation = function(x) x^2, inverse = sqrt)
} else if (transform_fun == "expm1") {
  out <- list(transformation = expm1, inverse = log1p)
} else if (transform_fun == "log-log") {
  out <- list(
    transformation = function(x) log(log(x)),
    inverse = function(x) exp(exp(x))
  )
}

The alternative here is to create a look-up table, which is much easier to read, and importantly, extend - we just need to add another row for every new transformation:

df <- tibble::tribble(
  ~transform_fun, ~out,
  "identity",     list(transformation = function(x) x, inverse = function(x) x),
  "log",          list(transformation = log, inverse = exp),
  "log1p",        list(transformation = log1p, inverse = expm1),
  "log(x+1)",     list(transformation = log1p, inverse = expm1),
  "log10",        list(transformation = log10, inverse = function(x) NA),
  "log2",         list(transformation = log2, inverse = function(x) NA),
  "exp",          list(transformation = exp, inverse = log),
  "sqrt",         list(transformation = sqrt, inverse = function(x) x^2),
  "power",        list(transformation = function(x) x^2, inverse = sqrt),
  "expm1",        list(transformation = expm1, inverse = log1p),
  "log-log",      list(transformation = function(x) log(log(x)), inverse = function(x) exp(exp(x)))
)

These two approaches, of course, yield the same result:

identical(
  out, 
  df$out[df$transform_fun == transform_fun][[1L]]
)
#> [1] TRUE

Created on 2022-12-21 with reprex v2.0.2

The only complication this introduces is making sure that this data frame is available at build time, which requires collation order (one can use #' @include to this easily).

Should we start using such look-up tables where relevant?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the linked R/get_transformation.R example and the surrounding project code to identify which conditional blocks are intended to change. The issue does not define a target set of files, so completion would first require agreement on scope, then preserving existing transformation behavior while replacing the selected conditionals with lookup tables.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
developer-experience
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.