Just one comment

Defensive R Programming

Colin Gillespie

Jumping Rivers

I don't know about you...

  • Code that is obvious today
  • Is often a lot less obvious in a few weeks times
Defensive R Programming

Comments

  • You can add comments to your R code via #
  • It turns out that writing good comments is tricky!
# This is a comment
# The above comment isn't very helpful
# Or is it?
Defensive R Programming

Tip 1: Avoid obvious comments

  • What's obvious is sometimes hard to decide
    • For example, the comments
       # Loop through data sets
       for (dataset in datasets) {
        # Read in data set
        r <- read.csv(dataset)
       }
      
      look reasonable
    • But are perhaps a little too obvious
Defensive R Programming

Tip 2: Avoid comments that you will never update

The most common example is header comments at the top of the file

# Last updated: 1967-02-25
# Author: D Law
# Status: No 1
  • These sorts of comments are almost never updated
  • I once saw # list of packages used: XXX, YYY
Defensive R Programming

Tip 3: Be consistent

  • Always start with a single # or double ##
  • Start with a capital letter - follow the rules of grammar
  • Be careful with jokes
    • What you find funny, others may take offense
  • Be sure to comment on code that "looks wrong"
  • Use # TODO or # XXX to indicate a future problem
Defensive R Programming

Let's practice

Defensive R Programming

Preparing Video For Download...