Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Robustness linters #48

@jimhester

Description

@jimhester

Suggestions from @hadley

  • Require drop = FALSE whenever [ is used with two or more args
  • Require na.rm argument for any function that has it (to force you to
    think about missing values during development)
  • Don't use of require()/library() inside a function
  • Don't change global state: setwd(), options(), par() etc. Could
    have next package robustr that contained alternatives (i.e. the with_*
    functions from devtools)
  • Always turn partial matching warning options on (not really code
    linting, but related)
  • Avoid functions that use NSE (with(), subset(), transform() etc)
  • Avoid functions that return different types of output (e.g. sapply())
  • TRUE and FALSE instead of T and F.
  • return() calls at the end of functions (result of last statement is always returned, so the explicit return() is redundant (and also slower as it requires an additional function call).
  • use of 1:length(x) in for loops (dangerous if length(x) can be 0) as 1:0 returns c(1, 0), should use seq_len(length(x)) instead.
  • Iterative appending to vectors within loops rather than pre-allocation the vectors and filling. (the former is quadratic in time complexity, as R has to keep re-sizing the vector on every assignment.
# bad
for (i in seq_len(10)) {
  x <- c(x, i)
}

# good
x <- integer(10)
for (i in seq_len(10)) {
  x[i] <- i
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions