Merck & Co., Inc., August 2024
RCONIS
openstatsware
rpact
packageassertthat
: simple assertions, e.g. assert_that(is.numeric(x))
tidyverse
and written by Hadley Wickhamcheckmate
: more powerful assertions, e.g. assert_numeric(x, lower = 0, upper = 1)
expect_*
verbs for extending testthat
functionalityPydantic
helps verify input data coming into fully type annotated Python programs.Pydantic
helps verify input data coming into fully type annotated Python programs.Going hand in hand with type assertions is type documentation.
Recommendation: Use consistent type hints or types in the roxygen2
documentation to explain to the user which argument of the function expects which type of input.
Simple example (formatting can be tuned to personal preference):
Is shorter and more consistent than:
numeric
, character
, integer
, etc.data.frame
, list
, and custom package classes.number
(scalar numeric), string
(scalar character), flag
(scalar logical), etc. because these have corresponding checkmate
assertions, e.g. assert_number(x)
roxytypes
Provides typed parameter documentation tags for integration with roxygen2
.
Typed parameter tags provide a consistent interface for annotating expected types for parameters and returned values.
Example:
Output format in the .md
file can be configured centrally in the DESCRIPTION
file, e.g.: Config/roxytypes: list(format = "(`{type}`)\cr {description}")
Conversion utility to convert existing roxygen2
documentation to typed parameter tags.
tryCatch()
sparsely only, and only for specific expected errors.
fixInNamespace()
to be able to debug the code successfullystop()
and warning()
functions are sufficient for most use casesmessage()
very sparingly, as usually R packages should not be too verbose during normal operationRecommendation: Write tests for error and warning handling, but not for type assertions.
Use specific enough expectations to ensure that the correct error or warning is thrown.
Example:
Type assertions are evaluated in every function call, and should be code reviewed in combination with their documentation. Testing them separately would cause too much overhead.
Please see here
Please see here
ggplot2
plots, write conclusionroxygen2
documentation from function code (here also assertions help to guess the type for documentation)These slides are at danielinteractive.github.io/merck-eng
Software Engineering Topics | License