This lesson reviews best practices associated with clean coding.
At the end of this activity, you will be able to:
- Write code using Hadley Wickham’s style guide.
What You Need
RStudio to complete this tutorial. Also we recommend that you have an
earth-analytics directory set up on your computer with a
/data directory within it.
Clean code means that your code is organized in a way that is easy for you and for someone else to follow and read. Certain conventions are suggested to make code easier to read. For example, many guides suggest the use of a space after a comment. Like so:
#poorly formatted comments are missing the space after the pound sign. # good comments have a space after the pound sign
While these types of guidelines may seem unimportant when you first begin to code, after a while you’ll realize that consistently formatted code is much easier for your eye to scan and quickly understand.
Consistent, Clean Code
Take some time to review Hadley Wickham’s style guide. From here on in, you will follow this guide for all of the assignments in this class.
Object Naming Best Practices
- Keep object names short: This makes them easier to read when scanning through code
- Use meaningful names: For example,
precipis a more useful name that tells us something about the object compared to
- Don’t start names with numbers! Objects that start with a number are NOT VALID in
- Avoid names that are existing functions in R: e.g.
for, see here
A few other notes about object names in
Ris case sensitive (e.g.
weight_kgis different from
- Avoid other function names (e.g.
- Use nouns for variable names and verbs for function names.
- Avoid using dots in object names - e.g.
my.dataset- dots have a special meaning in
R(for methods) and other programming languages. Instead use underscores
Take a look at the code below.
- Create a list of all of the things that could be improved to make the code easier to read and work with.
- Add to that list things that don’t fit the Hadley Wickham style guide standards.
- Try to run the code in
R. Any issues?
#my code #load stuff library(ggplot2) #turn off factors options(stringsAsFactors = FALSE) 1variable <- 3 * 6 meanVariable <- 1variable #calculate something important mean-variable <- meanvariable * 5 thefinalthingthatineedtocalculate <- mean-variable + 5 #get things that are important download.file(url = "https://ndownloader.figshare.com/files/7010681", destfile = "data/boulder-precip.csv") my.data <- read.csv(file="data/boulder-precip.csv") head(my_data) str(my.data) qplot(x=my.data$DATE, y=my.data$PRECIP)