Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Programming with GUIs. Join the course to learn more.

Project decomposition

This week you are going to use guizero to build a game. The program you will write is complex and so the project will be broken down into several stages using a process called decomposition.

The Emoji Game

The game you are going to build is a picture-matching game. The object of the game is to spot the emoji that appears in both sets.

An animation of an emoji matching game. There is a 3 by 3 grid of emojis, with a 3 by 3 grid of emojis on buttons below. All of the emojis are unique apart from one which is present in both grids. When this is clicked the text "correct" appears above the top grid. When another emoji button is clicked, the word "incorrect" appears there instead.

The game incorporates many of the elements that you have seen over the past two weeks, including widgets, layout design, commands, and events.

Decomposition

Because this task is complex and involves multiple components, we are going to break it down into stages.

In computer science, the process of breaking down a task into simpler stages that can be reassembled into the completed program is called decomposition.

It is a fundamental skill in programming and computer science. Decomposition requires a programmer to be able to logically break down a task into individual stages. It might be easier to complete each individual stage in isolation, or to build on previous stages and even reuse code from other parts of the project. In either instance, every stage built must be compatible with all of the others.

To decompose a programming task into more manageable stages, a programmer might begin by writing down the objective(s) of the program. They then might list all of the resources they have to reach this objective. Next, they must work out all of the stages that need to be completed in order to reach the objective. The programmer might work backwards from the objective or forwards from their resource list, depending on their preference. They can then work out how to complete each stage.

Below the title "Resource decomposition" are three images in a row, with arrows between them showing a progression from left to right. The first image shows a filled yellow square, a filled blue square, a filled red square, and an empty circle. The second image shows these being combined to create a filled purple square, a filled green square, and a filled red circle. The third images shows a filled red circle overlapping a filled green square, both on top of and contained within a filled purple square. Below the title "Goal decompositon"are three images in a row, with arrows between them showing a progression from left to right. The first image is the same as the final image from the "Resource decomposition" sequence. In the second image this image has been divided up into 9 jigsaw pieces, and the third image shows the jigsaw pieces separately.

There might be multiple ways of completing a stage. In these instances, the programmer must make a decision on which method is best. They could try to determine which method is the most efficient or be limited based on the resources available or their own skills. Some stages might themselves be complex and so the programmer will want to decompose these as well.

To illustrate this process, think back to last week’s task of building a text editor. Our objective was to make a text editor that could open and save files; our resources were the guizero library. This task can be decomposed into the following stages:

  • design the layout of the text editor
  • create a save button
  • create an open button
  • create a file name text box
  • create a box for text to be written and edited.

Some stages, like creating a save button, are a bit more complicated than others so we might want to decompose them further:

  • create a function which, when called, saves the file
  • create a button which, when pushed, calls the function.

It can take practice to learn how to use decomposition efficiently but, if done well, decomposition can make big programming projects easier and make your code easier to edit and adapt.

Challenges

Before applying decomposition to the Emoji Game, which you will do in the next step, practice the process on the following tasks:

  • assembling a model airplane
  • travelling to school
  • sewing a dress.

For each of the above, break the task down into the stages you need to complete in order to finish the task. Think about what you will need to assemble before starting the task: are you reliant on any external factors or resources? Do the stages need to be completed in a particular order? Are there any stages that can be easily replaced with a different method or approach?

Share your decompositions in the comments, and read other learners’ answers. Did any other learners include stages that you didn’t? How does your approach differ from others’? Reflecting on your answers like this can help with your learning.

Share this article:

This article is from the free online course:

Programming with GUIs

Raspberry Pi Foundation