Coding Style

Defensive R Programming

Dr. Colin Gillespie

Jumping Rivers

Consistency

Everyone agrees that consistency is key

This may mean changing styles when in different teams!

Defensive R Programming

Uncontroversial rules

  • Assignment wars: = vs ->
x = 5
# or
x <- 5
  • Everyone agrees you shouldn't mix & match

  • I prefer the superior = for assignment but

  • DataCamp prefers <- for their courses

So be consistent

Defensive R Programming

Spacing

Consistent spacing makes code far easier to read

Compare

res<-t.test(x,paired=FALSE)

with

res <- t.test(x, paired = FALSE)
Defensive R Programming

Spacing

Two widely accepted rules are

  • spaces around assignment x <- 5
  • spaces after a comma - x[1, 1] instead of x[1,1]
Defensive R Programming

Let's practice!

Defensive R Programming

Preparing Video For Download...