The think-code-test-debug-repeat cycle is an organized way to solve programming problems. First understand what the task asks, then choose an algorithm—a recipe of steps—before writing code. Run the program with different inputs, compare actual results with expected results, fix incorrect behavior, and repeat until the code passes its tests. section 2.3 section 2.5
Figure 2.3. This is the ideal way to approach solving a problem with programming. Understand the problem before you write any code. Then test the code that you write and debug it as necessary. This process repeats until your code passes all tests.
Use the cycle diagram as a quick roadmap: understand the problem before coding, then test and debug repeatedly until the solution works according to its specification. section 2.3
Work through the cycle in this order:
- Think: identify the task, determine its inputs and outputs, and decide what series of steps could solve it. Do not begin by writing code immediately. section 2.3 section 2.5
- Code: implement the chosen algorithm, experimenting with the necessary operations and repeated parts. section 2.3
- Test: run the program with different inputs and check whether its actual output matches the expected output. section 2.3
- Debug: locate the lines causing incorrect behavior and change the implementation. Repeat the cycle as many times as necessary. section 2.3
A recipe for a program
Baking provides a useful analogy. Think about what baked good the task requests and choose or create a recipe. Code by combining the ingredients and following the steps. Test the result by checking whether it matches the requested product. Debug by changing a problem ingredient—for example, reducing salt if the result is too salty—and repeat. section 2.3
Readable code supports the cycle: use descriptive names and comments that explain the problem and the solution. More than one implementation may produce the same correct behavior, so matching the required inputs and outputs matters most. section 2.5 section 13.3
