R Style and Readability
Intermediate Steps
Name Each Calculation
Breaking a calculation into named steps makes the data flow easier to inspect and replay.
Program
Play the script to change the discount and watch each intermediate value stay visible.
intermediate_steps.R
discount <-
price <- 80
tax <- 8
subtotal <- price - discount
total <- subtotal + tax
label <- paste("total", total, sep = ":")
cat(label, "\n", sep = "")
discount <-
price <- 80
tax <- 8
subtotal <- price - discount
total <- subtotal + tax
label <- paste("total", total, sep = ":")
cat(label, "\n", sep = "")
discount <-
price <- 80
tax <- 8
subtotal <- price - discount
total <- subtotal + tax
label <- paste("total", total, sep = ":")
cat(label, "\n", sep = "")
named step
`subtotal` explains the price after the discount.
small expression
Each line performs one simple calculation.
traceable flow
Replay can show the discount, subtotal, and total separately.