Problem Decomposition
@BootstrapWorld
Problem Decomposition
Sally runs a lemonade stand, which charges $1.75/glass. It costs her $0.30/glass to buy sugar, ice and lemons.
Problem Decomposition
Problem Decomposition
There are many possible solutions…
Which solution is “best”, and why?
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?
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.
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.
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?