Chapter 16 Style guide
16.1 What is a coding style?
“Good coding style is like using correct punctuation. You can manage without it, but it sure makes things easier to read.” Headley Wickham.
“thus try to make use of it try to follow the style guides you may thin it is kind of useless in the beginning however you will see that it is especially when your functions get more complex very useful to do so i often spent hours to find my own bugs just because my coding style was horrible keep that in mind” Reto, 2019.
16.2 Special characters
- do not use special characters (Umlaute, apostrophes, …)
- neither for object names, nor comments!
- you may have noticed it: object names have to start with a letter, blanks are not allowed
16.4 Script file names
Should be meaningful and use the .R
suffix.
Good:
fit-linear-models.R
utility-functions.R
session02_exerciseA.R
Bad:
foo.R
things.R
exercise.R
16.5 Object names
Should be lowercase and meaningful! Use underscores to separate words within a name. If possible use nouns for variables and verbs for functions.
Good:
day_one
day_1
gender
height
smallest_male
temp_kelvin
Bad:
the_first_day_of_month
DayOne
dayone
djm1
foo
T
(existing name!)c
(existing name!)mean
(existing name!)
16.6 Spaces: infix operators
Put spaces around all infix operators (+
, /
, *
,
&
, ==
, |
), around the get operator (<-
) and =
’ s.
Well written code:
temp * 9 / 5 + 32
temp <- 4 / 3 * pi**3
volume <-if (temp == 0) {
cat("pretty cool")
}
Bad code:
*9/5+32
temp<-temp4/3*pi**3
volume<-if( temp==0){ print("cool") }
16.7 Spaces: colons
One exception: There is no space around colons (:
, ::
, :::
):
Good:
1:10 x <-
Bad:
1 : 10 x <-
16.8 Spaces: multiple spaces
Ok to use multiple spaces if it increases readability. E.g.:
More well-structured …
list(
total = l + b + h,
mean = (l + b + h) / 3,
variance = var(c(l + b + h))
)
… than this:
list(
total = l + b + h,
mean = (l + b + h) / 3,
variance = var(c(l + b + h))
)
16.9 Spaces: brackets
A space before opening round brackets ((
) and after closing round brackets
()
) except for function calls.
Good:
if (a == 4) { cat("a equals 4") }
if (debug) print(x)
mean(x)
plot(x, y, col = "red")
Bad:
if(a==4)
if(debug)print (x)
mean (x)
plot (x,y,col="red")
16.10 Spaces: commas
Always use spaces after the comma.
Good:
c(1, 2, 4)
x <- mat[1, ]
x <-plot(x, y, col = "red")
Bad:
c(1,2,4)
x <- mat[1,]
x <-plot(x,y,col = "red")
16.11 Opening/closing brackets
Opening curly brackets should never go in a new line, but should always be followed by a new line. Final closing curly brackets should be in a separate line.
Good:
if (a == 10) {
TRUE
res <-else {
} FALSE
res <-
}
function(x) {
myfun <-# Instructions
}
Bad:
if (a == 10)
{ TRUE
res <-else
}
{ FALSE} res <-
16.12 Indention
Use block-wise indention (RStudio supports you). Suggested by Headley: use two spaces as indent.
Good:
function(x) {
powfun <- x**y
res <-return(res)
}
Bad:
function(x) {
powfun <- x**y
res <-return(res)
}
For very short statements:
function(x, y) { return(x^y) }
powfun <-if (a == 10) { res <- TRUE }
function(x, y) return(x^y)
powfun <-if (a == 10) res <- TRUE
16.13 Explicit return
My personal suggestion: explicitly use the command when returning results from a function!
Good:
function(x) {
powfun <- x^2
res <-return(res)
} function(x, y) return(x^y) powfun <-
Bad:
function(x) {
powfun <- x^2
res <-
res }
For short functions or temporary functions:
function(x, y) x^y
powfun <-sapply(data, function(x) x^2)
16.14 Line breaks
Line breaks: lines should not be longer than 80 characters. Use line breaks for long statements.
Good:
c(firstname = "Reto", lastname = "Stauffer",
data <-city = "Innsbruck", zipcode = "6020",
department = "Department of Statistics")
Bad:
c(firstname = "Reto", lastname = "Stauffer", city = "Innsbruck", zipcode = "6020", department = "Department of Statistics") data <-
16.15 Nested indention
Use block-wise indention (RStudio supports you). Use two spaces as indent.
Good:
function(x) {
demo_function <-if (a == 3) {
"This"
res <-else {
} for (i in 1:10) {
a + i
res <-
}
}return(res)
}
Bad:
function(x) {
demo_function <-if (a == 3) {
"This"
res <-else {
} for (i in 1:10) {
a + i
res <-
}
}return(res)
}
16.16 Object assignment
Use <-
and not =
.
Good:
5
a <- function(x, y) return(x^y) powfun <-
Bad:
5
a = function(x, y) return(x^y) powfun =
16.17 Comments
- Start to write comments!
- Again: code is more often read then written,
- comments help to understand code (also your own!).
- Use
# ------
or# ========
to separate code blocks.
# -----------------------------
# Define variables
seq(0, 100, length.out = 100)
x <- sin(x / pi * 180)
y <-# -----------------------------
# Create plot
plot(x, y, col = 5)
16.18 All together
Both code chunks do the very same. And both will work. I think there’s nothing to say here!
Good:
function(x) {
demo_function <-if (a == 3) {
"This"
res <-else {
} for (i in 1:10) {
a + i
res <-
}
}return(res)
}
Very, very bad:
function(x)
DmoFn<-if(a==3) {res <- "This"
{else{
}for(i in 1 :10) {
a +i}}
res= res}