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.

discount
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 = "")
  1. discount ← 10

    1discount <- 102price <- 80
    values this step10discount
  2. price ← 80

    1discount <- 102price <- 803tax <- 8
    values this step80price
  3. tax ← 8

    2price <- 803tax <- 84subtotal <- price - discount
    values this step8tax
  4. subtotal ← 70

    3tax <- 84subtotal <- price - discount5total <- subtotal + tax
    values this step70subtotal80price10discount
  5. total ← 78

    4subtotal <- price - discount5total <- subtotal + tax6label <- paste("total", total, sep = ":")
    values this step78total70subtotal8tax
  6. label ← total:78

    5total <- subtotal + tax6label <- paste("total", total, sep = ":")7cat(label, "\n", sep = "")
    values this steptotal:78label78total
  7. cat(label, " ", sep = "")

    6label <- paste("total", total, sep = ":")7cat(label, "\n", sep = "")
    outputtotal:78
    values this steptotal:78label
  1. discount ← 0

    1discount <- 02price <- 80
    values this step0discount
  2. price ← 80

    1discount <- 02price <- 803tax <- 8
    values this step80price
  3. tax ← 8

    2price <- 803tax <- 84subtotal <- price - discount
    values this step8tax
  4. subtotal ← 80

    3tax <- 84subtotal <- price - discount5total <- subtotal + tax
    values this step80subtotal80price0discount
  5. total ← 88

    4subtotal <- price - discount5total <- subtotal + tax6label <- paste("total", total, sep = ":")
    values this step88total80subtotal8tax
  6. label ← total:88

    5total <- subtotal + tax6label <- paste("total", total, sep = ":")7cat(label, "\n", sep = "")
    values this steptotal:88label88total
  7. cat(label, " ", sep = "")

    6label <- paste("total", total, sep = ":")7cat(label, "\n", sep = "")
    outputtotal:88
    values this steptotal:88label
  1. discount ← 20

    1discount <- 202price <- 80
    values this step20discount
  2. price ← 80

    1discount <- 202price <- 803tax <- 8
    values this step80price
  3. tax ← 8

    2price <- 803tax <- 84subtotal <- price - discount
    values this step8tax
  4. subtotal ← 60

    3tax <- 84subtotal <- price - discount5total <- subtotal + tax
    values this step60subtotal80price20discount
  5. total ← 68

    4subtotal <- price - discount5total <- subtotal + tax6label <- paste("total", total, sep = ":")
    values this step68total60subtotal8tax
  6. label ← total:68

    5total <- subtotal + tax6label <- paste("total", total, sep = ":")7cat(label, "\n", sep = "")
    values this steptotal:68label68total
  7. cat(label, " ", sep = "")

    6label <- paste("total", total, sep = ":")7cat(label, "\n", sep = "")
    outputtotal:68
    values 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.