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
Replay: real traced execution (multi-file project)
discount <- 10
price <- 80
tax <- 8
subtotal <- price - discount
total <- subtotal + tax
label <- paste("total", total, sep = ":")
cat(label, "\n", sep = "")
discount <- 0
price <- 80
tax <- 8
subtotal <- price - discount
total <- subtotal + tax
label <- paste("total", total, sep = ":")
cat(label, "\n", sep = "")
discount <- 20
price <- 80
tax <- 8
subtotal <- price - discount
total <- subtotal + tax
label <- paste("total", total, sep = ":")
cat(label, "\n", sep = "")
discount ← 10
1discount <- 102price <- 80values this step10discountprice ← 80
1discount <- 102price <- 803tax <- 8values this step80pricetax ← 8
2price <- 803tax <- 84subtotal <- price - discountvalues this step8taxsubtotal ← 70
3tax <- 84subtotal <- price - discount5total <- subtotal + taxvalues this step70subtotal80price10discounttotal ← 78
4subtotal <- price - discount5total <- subtotal + tax6label <- paste("total", total, sep = ":")values this step78total70subtotal8taxlabel ← total:78
5total <- subtotal + tax6label <- paste("total", total, sep = ":")7cat(label, "\n", sep = "")values this steptotal:78label78totalcat(label, " ", sep = "")
6label <- paste("total", total, sep = ":")7cat(label, "\n", sep = "")outputtotal:78values this steptotal:78label
discount ← 0
1discount <- 02price <- 80values this step0discountprice ← 80
1discount <- 02price <- 803tax <- 8values this step80pricetax ← 8
2price <- 803tax <- 84subtotal <- price - discountvalues this step8taxsubtotal ← 80
3tax <- 84subtotal <- price - discount5total <- subtotal + taxvalues this step80subtotal80price0discounttotal ← 88
4subtotal <- price - discount5total <- subtotal + tax6label <- paste("total", total, sep = ":")values this step88total80subtotal8taxlabel ← total:88
5total <- subtotal + tax6label <- paste("total", total, sep = ":")7cat(label, "\n", sep = "")values this steptotal:88label88totalcat(label, " ", sep = "")
6label <- paste("total", total, sep = ":")7cat(label, "\n", sep = "")outputtotal:88values this steptotal:88label
discount ← 20
1discount <- 202price <- 80values this step20discountprice ← 80
1discount <- 202price <- 803tax <- 8values this step80pricetax ← 8
2price <- 803tax <- 84subtotal <- price - discountvalues this step8taxsubtotal ← 60
3tax <- 84subtotal <- price - discount5total <- subtotal + taxvalues this step60subtotal80price20discounttotal ← 68
4subtotal <- price - discount5total <- subtotal + tax6label <- paste("total", total, sep = ":")values this step68total60subtotal8taxlabel ← total:68
5total <- subtotal + tax6label <- paste("total", total, sep = ":")7cat(label, "\n", sep = "")values this steptotal:68label68totalcat(label, " ", sep = "")
6label <- paste("total", total, sep = ":")7cat(label, "\n", sep = "")outputtotal:68values this steptotal:68label
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.