greta-dev / greta

simple and scalable statistical modelling in R
https://greta-stats.org
Other
528 stars 63 forks source link

Explore method dispatch to avoid if(class(thing)) do (function) else do (other_function) #705

Open njtierney opened 2 months ago

njtierney commented 2 months ago

e.g., Use method dispatch inside of calculate to avoid


    if (is.greta_mcmc_list(values)) {
      result <- calculate_greta_mcmc_list(
        target = target,
        values = values,
        nsim = nsim,
        tf_float = tf_float,
        trace_batch_size = trace_batch_size
      )
    } else {
      result <- calculate_list(
        target = target,
        values = values,
        nsim = nsim,
        tf_float = tf_float,
        env = parent.frame()
      )
    }

Thanks @maelle for the suggestion

maelle commented 2 months ago

my source = https://speakerdeck.com/jennybc/code-smells-and-feels?slide=55 :smile_cat: