Code Monkey home page Code Monkey logo

ntdmc_tech_meeting_2021's People

Contributors

ben18785 avatar tlestang avatar

Watchers

 avatar  avatar  avatar  avatar

ntdmc_tech_meeting_2021's Issues

Code snippets for long parameter lists

This is a method that runs a simulation. How can we make it better?

model <- function(start, stop, stoc,
                       spec, dens,
                       b,
                       i_mat, i_dur,
                       ntype, ncov) {

# do stuff

}

Change the names to convey meaning

simulate <- function(time_start, time_stop,
		       is_stochastic, mosquito_species,
                       mosquito_density,
                       mosquito_to_human_prob,
                       immunity_maternal, immunity_duration,
                       net_type, net_coverage) {

# do stuff

}

What are the problems with this?

  • We're passing a long list of things that could easily be mixed up; especially if we don't always pass in named arguments
  • It's not clear what types things are expected to be
  • If we add more parameters, the thing can quickly get unwieldy

How can we make it better?

SimulationParameters <- setClass("SimulationParameters",
                                 slots=list(time_start="numeric",
                                            time_stop="numeric",
                                            is_stochastic="logical"))

# doesn't work
simulation_parameters <- SimulationParameters(
                             time_start=1990,
                             time_stop=2018,
                             is_stochastic=2)

# works
simulation_parameters <- SimulationParameters(
                             time_start=1990,
                             time_stop=2018,
                             is_stochastic=FALSE)

Other parameter classes

check <-function(object) {

  if(!object@net_type %in% c("net 1", "net 2"))
    return("A net must of type `net 1` or `net 2`.")

  coverage <- object@net_coverage
  if(coverage < 0 | coverage > 1)
    return("Net coverage must not be outside [0, 1].")
}

BednetParameters <- setClass("BednetParameters",
                             slots=list(net_type="character",
                                        net_coverage="numeric"),
                             validity = check)
                                   
# fails
bednet_parameters <- BednetParameters(net_type = "net 3",
                                      net_coverage = 0.3)

# fails
bednet_parameters <- BednetParameters(net_type = "net 2",
                                      net_coverage = 100)

Simulation object

Simulation <- setClass("Simulation",
                       slots=list(simulation_parameters="SimulationParameters",
                                  bednet_parameters="BednetParameters",
                                  immunity_parameters="ImmunityParameters",
                                  mosquito_parameters="MosquitoParameters"))

# ...don't show how to do but could create a "run" method
simulation <- Simulation(simulation_parameters=....)
result <- run(simulation)

# what parameters did I use to run my simulation?
simulation@simulation_parameters

Most useful tips for epidemiological coders

I've been going through the various links (thanks @tlestang!) and, to my mind, the following is an ordering representing the prevalence of various code smells in epidemiological code. Note, this partly reflects that most epidemiological people code in R (there are people that also code in C++ but I don't know the language as well) and don't tend to use classes.

  1. Long parameter lists (I liked the refactoring guru heuristic here ">3/4 parameters?")
  2. Long methods
  3. Highly nested if/else loops (Jenny Bryan's presentation had a good example of this)
  4. Comments within methods
  5. Abstract or unhelpful variable or function names

What are your thoughts @tlestang ? Any clangers I've missed off? I don't think we'll want to go into the detail of too many.

Could you please recommend more good examples?

Dear Thibault and Ben,

Thank you so much for the excellent talk on Monday and for developing this repo! I am currently a first-year PhD student at Oxford and previously I was trained in medicine and public health. I am quite new to programming and I learned quite a lot from your materials. However, I wonder, could you please recommend more good codes examples? Programming is a language and I feel most of my day when I was coding by myself I was just checking R/Python libraries--- this feels I was more like learning how to use vocabularies. However, usually when we learn a new language, we also want to learn from good articles so that to learn how other people organize their articles and use different vocabularies. so I wonder, have you noticed any good open-source code project in health data science field? I know nowadays more infectious disease modellers shared their codes, but sometimes I found some of them are so hard to read/understand!

In addition, do you by any chance know any research coding community within Oxford? It would be nice to learn from and help with each other!

Many thanks,

Li
[email protected]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.