8.3 Coding Practices
8.3.1 Coding Style
Consistent coding style improves readability and reduces errors in shared code.
Unless otherwise noted, PEcAn follows the Tidyverse style guide, so please familiarize yourself with it before contributing. In addition, note the following:
- Document all functions using
roxygen2
. See Roxygen2 for more details. - Put your name on things. Any function that you create or make a meaningful contribution to should have your name listed after the author tag in the function documentation. It is also often a good idea to add your name to extended comments describing particularly complex or strange code.
- Write unit tests with
testthat
. Tests are a complement to documentation - they define what a function is (and is not) expected to do. Not all functions necessarily need unit tests, but the more tests we have, the more confident we can be that changes don’t break existing code. Whenever you discover and fix a bug, it is a good idea to write a unit test that makes sure the same bug won’t happen again. See Unit_Testing for instructions, and Advanced R: Tests. - Do not use abbreviations.
Always write out
TRUE
andFALSE
(i.e. do not useT
orF
). Do not rely on partial argument matching – write out all arguments in full. - Avoid dots in function names.
R’s S3 methods system uses dots to denote object methods (e.g.
print.matrix
is theprint
method for objects of classmatrix
), which can cause confusion. Use underscores instead (e.g.do_analysis
instead ofdo.analysis
). (NOTE that many old PEcAn functions violate this convention. The plan is to deprecate those in PEcAn 2.0. See GitHub issue #392). - Use informative file names with consistent extensions.
Standard file extensions are
.R
for R scripts,.rds
for individual objects (viasaveRDS
function), and.RData
(note: capital D!) for multiple objects (via thesave
function). For function source code, prefer multiple files with fewer functions in each to large files with lots of files (though it may be a good idea to group closely related functions in a single file). File names should match, or at least closely reflect, their files (e.g. functiondo_analysis
should be defined in a file calleddo_analysis.R
). Do not use spaces in file names – use dashes (-
) or underscores (_
). - For using external packages, add the package to
Imports:
and call the corresponding function withpackage::function
. Do not use@importFrom package function
or, worse yet,@import package
. (The exception is infix operators likemagrittr::%>%
orggplot2::%+%
, which can be imported via roxygen2 documentation like@importFrom magrittr %>%
). Do not add packages toDepends
. In general, try to avoid adding new dependencies (especially ones that depend on system libraries) unless they are necessary or already widely used in PEcAn (e.g. GDAL, NetCDF, XML, JAGS,dplyr
). For a more thorough and nuanced discussion, see the package dependencies appendix.