liveBook · Manning

Decomposition

As explained in Get Programming: Learn to code with Python

The technique of dividing a complex problem, task, or program into smaller, separately manageable parts.

Decomposition means breaking a big problem into smaller tasks you can handle one at a time. Before writing code, decide what the program should accomplish, what input it needs, and what output it should produce. Then look for smaller problems you can solve first. section 2.3

Start with the easy parts

Suppose a program must convert a number of minutes into hours and minutes. One task is to set up the input: `minutes_to_convert = 123`. Another is to show the result: print labels followed by `hours_part` and `minutes_part`. With input and output separated, you can focus next on calculating those two values. section 6.2

For a larger program, try this approach:

  • Write down the main tasks without deciding exactly how to code them. A document-similarity program might read a file, get its words, count them, and calculate similarity. section 29.1
  • Give each task a clear input and output. Prefer a reusable task that reads one filename over one that can only read exactly two. section 29.1
  • Notice which tasks must wait for others. In an online order, finding an item and making its shipping label can happen independently after the order details are extracted; sending the package must wait for the packed box and label. section 20.1
  • Check smaller pieces as they work, then put them together. A problem is less intimidating, and an error is easier to track down when you can focus on one piece. section 19.1 section 20.1
Figure 20.1. One possible way to divide the task of ordering an item online into smaller, self-contained, and reusable subtasks. Each gray box represents a task. Things to the left of the box are the inputs to a task, and things to the right are outputs of the task.

Follow the online order from task to task. Each box takes inputs on its left and produces outputs on its right, making it easier to see what must happen next. section 20.1

Quick check: If you were writing a program to process a list of words, what small task could you complete and check before tackling the whole program? Aim for a piece with a clear job that you could use again. section 19.1 section 29.1

How Get Programming: Learn to code with Python covers it

How deep it goes
Works through task identification, inputs and outputs, dependencies, and reusable task design; leaves specific implementation decisions for later.
Examples
Illustrates the technique through a minutes-conversion program and an online-order workflow.
What it assumes
Starts with plain-language goals, inputs, and outputs before moving to simple Python variables and print statements.
What you can do after
Practice/perform it by dividing a program into clear tasks, checking smaller pieces, and assembling them into a working whole.
How it connects
Connects decomposition to pseudocode, debugging, task dependencies, and reusable functions.

Also explained in

Compare how 10 books explain decomposition →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.