19 Writing comments

Written by Isaac Ehrlich and last updated on 7 October 2021.

19.1 Introduction

In this lesson, you will learn what a comment is, as well as how and when to write them.

19.1.1 What is a comment?

Comments are lines of plain text written within code files. The general purpose of comments is to explain what is happening in your code and why. The following sections will outline how to write comments and when they should be used.

19.2 Comments

19.2.1 Why comment?

When revisiting code you have previously written or when reviewing someone else’s code, it is not always immediately clear what the code is doing or how it is working - especially if it is not commented. This makes code difficult to interpret, and hard to edit. By adding comments and explaining what is happening in the code, you make it easier to decipher and understand for when you inevitably return to it in the future.

19.2.2 How to comment?

In R, comments are written by adding a # before text, typically at the beginning of the line. This means that anything written after the # will not be interpreted as code by R, but rather as text. Comments also typically precede the code they are referring to.

 # Anything following the octothorp is a comment
 # Writing code after an octothorp will have no effect, even if properly formatted

If you want to comment more than one line of code, you can select the desired lines and then use the shortcut “ctrl + shift + C” (or “cmd + shift + C” on Mac).

19.2.3 When to comment?

Comments are typically used to improve the readability of code, either by partitioning code into sections, explaining the code, or providing links to additional resources. Not every line of code needs a corresponding comment, it is up to you to determine when you should write a comment. There are certain instances however that typically benefit from commenting.

  1. Defining Code Sections

You can use comments to divide sections of your analysis. Often, you can split a single script into multiple sections, such as setting up the environment, writing functions, and sections of your analysis.

# Load Packages
library(tidyverse)
# library(learnr)

# Load Data

# Data Cleaning

# Functions

# And So On
  1. Functions

When you write new functions, its common practice to explain the inputs, output, and purpose of each function

# Functions

addition <- function(x, y)
# The function addition takes numeric inputs 'x' and 'y' and outputs their sum
  sum <- x + y
return(sum)

subtraction <- function(x, y)
# The function subtraction takes numeric inputs 'x' and 'y' and outputs their difference
  diff <- x - y
return(diff)
  1. Complex Code

While performing operations such as addition or subtraction typically won’t require commenting, once your code begins to involve complex chunks and functions, it becomes important to explain what is going on so that you don’t need to figure it out again in the future.

  1. Providing Reasoning or Linking to Additional Resources

When performing analyses, you may make decisions based on reasoning that is not always evident. In these situations, it is helpful to include a comment explaining the decision you’ve made. If your code or reasoning also refers to some external source, it may be helpful to provide a link to the source.

19.3 Preamble

Beyond commenting within your code, you may also want to begin each R script with a multi-line comment that provides general information about the contents of the script, also known as a preamble. This preamble may contain information such as the author, date, and purpose of the script.

### Example Preamble ###
# Author: Your Name
# Date: The Date
# Contact: Your Email
# Prerequisites: Necessary Data of Folder Set-Up
# Links or Resources
# Purpose

19.4 Exercises

19.5 Key takeaways

  • Comments are an invaluable way to organize code and improve readability
  • Comments are an effective way of explaining code and the decisions made when writing
  • Comments help the author just as much as they help other readers!
  • Simple commands and lines do not require comments, but make sure to always comment thoroughly where necessary!

19.6 Exercises

19.6.1 Question 1

19.6.2 Question 2

19.6.3 Question 3

19.6.4 Question 4

19.6.5 Question 5

19.6.6 Question 6

19.6.7 Question 7

19.6.8 Question 8

19.6.9 Question 9

19.6.10 Question 10