Skip to content

General Coding Practices

Thomas J. Brailey edited this page May 25, 2020 · 5 revisions

Naming objects

  • When naming objects, use underscores ("_"), not periods (".") or CamelCase.

  • Check out the styler package for more information.

If stop statements

  • Within functions and for loops, it is good practice to include an "if stop" statement and error message. If there is a specific error in the code, the loop will stop and return a message, as shown in the example below:
if(my_year < 2007){
      stop("Invalid range of years.", immediate. = TRUE)
    }
  • This makes the debugging process considerably less stressful!
  • R also has the stopifnot function. See the example below:
stopifnot(1 == 1, all.equal(pi, 3.14159265), 1 < 2) # all TRUE

Clone this wiki locally