Consistency is key!
Main guide: tidyverse style guide
Below are more specific guidelines
- use 4 spaces (not the
tab
character and not 2 spaces) to indent - do not exceed 80 characters per line
- very helpful to be able to open windows side by side w/o scrolling
- very helpful to be able to see the code in a terminal window (if needed)
- functions hint: for long lines use this format (which can work with pipes too)
long_function_name_that_never_ends(
first_long_argument_name = 1,
second_long_argument_name = 2,
)
- Always in English, please run a spell-checker
- Explain Why not What (assume people know the programming language)
- leave a space after the comment symbol, as in
#
- leave a space after the comment symbol, as in
- Watch The Naming of Ducks
- Where the language and the previous code base allow, variable names should follow the pattern
my_variable
(no CamelCase, just use underscore)- This works for Python, R and SQL identifiers
- Functions names should be verbs
- except for math-line functions such as
cos()
- avoid
get_
andset_
unless they are OO like methods - should fit on one page (if longer, think about creating a second function)
- except for math-line functions such as
- When using Hungarian notation, consider using the suffixes from
purrr::
, such as_dbl
,_lgl
, etc.- other suffixes:
_msk
: mask for logical/selecting within a vector_idx
: index, as invar_idx <- which(var_msk)
_sp
: spatial package_mat
: matrix
- other suffixes:
- Table / collection / list names: singular or plural?
- good subjective discussion
- preference for singular
- use singular for dataframes, SQL tables, ORM classes
- use plural for vectors, e.g.,
customer_names