Wiki

Clone wiki

bio3d / Style Guide

Style Guide

We would benefit from a common style guide. e.g. adopt either one of

Most of the following style guide, text and code is collected from Hadley Wickham's Advanced R with minor modifications.

Naming

Function names

Function names should start with a letter and can contain numbers and dots. Use an underscore (_) to separate words within a name. Avoid other characters (e.g. hash etc.).

Avoid using both upper and lower case letters: they make the function name hard to type and hard to remember. For example, I can never remember if it's Rgtk2 or RGTK2 or RGtk2.

#!r
# Good
fix_missing <- function(x) {
  x[x == -99] <- NA
  x
}

# Bad
fixMissing <- function(x) 

Use . when working on S3 objects such as pdb, nma, pca, etc:

#!r
# Good
fix.pdb <- function(x)

# Bad
pdb_fix <- function(x) 
fix_pdb <- function(x) 
fixPdb <- function(x) 

Variables

Variable names should be lowercase. Use an underscore (_) to separate words within a name. Generally, variable names should be nouns and function names should be verbs.

#!r
# Good
day_one
day_1

# Bad
first_day_of_the_month
DayOne
dayone
djm1

Where possible, avoid using names of existing functions and variables. This will cause confusion for the readers of your code.

#!r
# Bad
T <- FALSE
c <- 10
mean <- function(x) sum(x)

Syntax

Spacing

Use spaces as round all infix operator(=, +, -, <-, etc.). The same rule applies when using = in function calls. Always put a space after a comma, and never before (just like in regular English).

#!r
# Good
average <- mean(feet / 12 + inches, na.rm = TRUE)

# Bad
average<-mean(feet/12+inches,na.rm=TRUE)

There’s a small exception to this rule: :, :: and ::: don’t need spaces around them.

#!r
# Good
x <- 1:10
base::get

# Bad
x <- 1 : 10
base :: get

Place a space before left parentheses, except in a function call.

#!r
# Good
if (debug) do(x)
plot(x, y)

# Bad
if(debug)do(x)
plot (x, y)

Extra spacing (i.e., more than one space in a row) is ok if it improves alignment of equal signs or assignments (<-).

#!r
list(
  total = a + b + c, 
  mean  = (a + b + c) / n
)

Do not place spaces around code in parentheses or square brackets (unless there’s a comma, in which case see above).

#!r
# Good
if (debug) do(x)
diamonds[5, ]

# Bad
if ( debug ) do(x)  # No spaces around debug
x[1,]   # Needs a space after the comma
x[1 ,]  # Space goes after comma not before

Curly braces

An opening curly brace should never go on its own line and should always be followed by a new line. A closing curly brace should always go on its own line, unless it’s followed by else.

Always indent the code inside curly braces.

#!r
# Good
if (y < 0 && debug) {
  message("Y is negative")
}

if (y == 0) {
  log(x)
} else {
  y ^ x
}

# Bad
if (y < 0 && debug)
message("Y is negative")

if (y == 0) {
  log(x)
} 
else {
  y ^ x
}

Indentation

When indenting your code, use two spaces. Never use tabs or mix tabs and spaces.

The only exception is if a function definition runs over multiple lines. In that case, indent the second line to where the definition starts:

#!r
long_function_name <- function(a = "a long argument", 
                               b = "another argument",
                               c = "another long argument") {
  # As usual code is indented by two spaces.
}

Assignment

#!r
# Good
x <- 5
# Bad
x = 5

Updated