Solve the General Problem

One of the keys to writing reusable code is to change the way you think about the problem you are solving. Don’t just think about the problem in front of you, but consider the type of problem you are solving and then create a solution that works for any problem of that type—not just your problem. Changing the way you think is no easy task, and it takes practice and experience. At the beginning of the chapter, I introduced the countToTen function, which solves a specific problem of “count from 1 to 10.” Then we built countTo, a function with parameters that can count from 1 to any number we want, which solves a much more general problem.

Get Learning to Program now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.