1 of 8

Problem Decomposition

@BootstrapWorld

2 of 8

Problem Decomposition

  • What functions could help Sally better manage her lemonade stand?
  • What is the difference between revenue and profit?
  • What is the relationship between profit, cost, and revenue?

Sally runs a lemonade stand, which charges $1.75/glass. It costs her $0.30/glass to buy sugar, ice and lemons.

3 of 8

Problem Decomposition

Save a copy.

Use the Design Recipe to define functions for cost, revenue, and profit.

4 of 8

Problem Decomposition

There are many possible solutions…

  1. fun profit(g): ( 1.75 * g) - (0.30 * g) end
  2. fun profit(g): (1.75 - 0.30) * g end
  3. fun profit(g): 1.45 * g end
  4. fun profit(g): revenue(g) - cost(g) end

Which solution is “best”, and why?

5 of 8

Problem Decomposition

Suppose the cost of lemons goes up. Which solution(s) would need to be changed?

What if Sally charges $2/glass? Which solution(s) would need to be changed?

  • fun profit(g): ( 1.75 * g) - (0.30 * g) end
  • fun profit(g): (1.75 - 0.30) * g end
  • fun profit(g): 0.95 * g end
  • fun profit(g): revenue(g) - cost(g) end

6 of 8

Problem Decomposition

profit can be decomposed into a simpler function that uses cost and revenue.

Decomposing a problem allows us to solve it in smaller pieces, which are also easier to test!

These pieces are reusable, resulting in writing less code, and less duplicate code.

Duplicate code means more places to make mistakes, especially when that code needs to be changed.

7 of 8

Top-Down vs. Bottom-Up

Top-Down and Bottom-Up design are two strategies for problem decomposition.

Bottom-Up: Start with the easy relationships first, and then build up to larger relationships. In the Lemonade Stand, you defined cost and revenue first, and then put them together in profit.

Top-Down: Start with the "big picture" and then worry about the details later. We could have started with profit, and made a to-do list of the smaller pieces we’d build later.

8 of 8

Top-Down vs. Bottom-Up

Jamal's trip requires him to drive 20mi to the airport, fly 9,000mi, and then take a bus 6mi to his hotel. His average speed driving to the airport is 40mph, the average speed of an airplane is 575mph, and the average speed of his bus is 15mph. Aside from time waiting for the plane or bus, how long is Jamal in transit?

This can be decomposed via Top-Down or Bottom-Up design. What functions would you define to solve this, and in what order?