Appendix B: Appendix: Coding Style Guidelines
B.1 Why Style Matters
Consistent code style makes your work:
- Easier to read (for you and collaborators)
- Easier to debug (clean structure reveals problems quickly)
- Easier to maintain (future you will thank present you)
This appendix summarizes the tidyverse style guide based on R4DS Workflow: Style.
B.2 File Naming
- Use lowercase, descriptive names, and hyphens (not spaces).
- Good:
data-cleaning.R,plot-analysis.R - Bad:
Data Cleaning.R,final.R
B.3 Object Naming
- Use snake_case for variable and function names.
- Be descriptive, not cryptic.
B.4 Spaces and Indentation
- Use two spaces for indentation.
- Always put a space after commas and around operators.
B.5 Long Lines
- Keep lines under 80 characters.
- Use line breaks for long function calls.
mpg |>
filter(cyl == 4, hwy > 30) |>
arrange(desc(hwy))B.6 Function Formatting
- Use consistent curly brace placement.
# Good
my_function <- function(x) {
x + 1
}
# Bad
my_function <- function(x){
x+1}B.7 Commenting Code
- Write comments to explain why, not what.
- Use
#for inline comments.
B.8 Piping
- Each step in a pipeline goes on a new line.
- Use the pipe
|>to connect transformations.
B.9 Tidyverse Style Summary
- Use
|>for pipelines, snake_case for names - Indent two spaces per level
- Avoid deeply nested code — break into steps
- Write clear, short, and well-commented code
B.10 In-Class Exercise
- Take a messy R script (provided in class).
- Reformat it to follow these style guidelines.
- Compare before vs. after readability.
B.11 Conclusion
Good code style is not just aesthetic — it improves reproducibility and collaboration.
Follow these conventions for all homework and projects in this course.