class: center, middle, inverse, title-slide # Coding style 💃 --- ## Style guide <br/> >"Good coding style is like correct punctuation: you can manage without it, butitsuremakesthingseasiertoread." > >Hadley Wickham <br/><br/> Style guide for this course is based on the [Tidyverse style guide](http://style.tidyverse.org/) --- ## File names & code chunk labels Guidelines: - Do not use spaces in file names, use `-` or `_` to separate words - Use all lowercase letters Example: ```r # Good ucb-admit.csv # Not so good UCB Admit.csv ``` --- ## Object names Guidelines: - Use `_` to separate words in object names - Use informative but short object names - Do not reuse object names within an analysis Example: ```r # Good acs_employed # Not so good acs.employed acs2 acs_subset acs_subsetted_for_males ``` --- ## Spacing Guidelines: - Put a space before and after all infix operators (=, +, -, <-, etc.), and when naming arguments in function calls. - Always put a space after a comma, and never before (just like in regular English). Example: ```r # Good average <- mean(feet / 12 + inches, na.rm = TRUE) # Not so good average<-mean(feet/12+inches,na.rm=TRUE) ``` --- ## ggplot Guidelines: - Always end a line with `+` - Always indent the next line Example: ```r # Good ggplot(diamonds, mapping = aes(x = price)) + geom_histogram() # Not so good ggplot(diamonds,mapping=aes(x=price))+geom_histogram() ``` --- ## Long lines Guidelines: - Limit your code to 80 characters per line. This fits comfortably <br>on a printed page with a reasonably sized font. - Take advantage of RStudio editor's auto formatting for indentation <br>at line breaks. --- ## Assignment Guidelines: - Use `<-` not `=` Example: ```r # Good x <- 2 # Not so good x = 2 ``` --- ## Quotes Guidelines: - Use `"`, not `'`, for quoting text. - The only exception is when the text already contains double quotes and no single quotes. Example: ```r ggplot(diamonds, mapping = aes(x = price)) + geom_histogram() + # Good labs(title = "`Shine bright like a diamond`", # Good x = "Diamond prices", # Not so good y = 'Frequency') ``` --- ## Resources - Tidyverse style guide: http://style.tidyverse.org/ - Slides from datasciencebox.org