9.1 - Structured Programming
What is structured programming
Structured programming is an approach to writing code that breaks down a complex program into smaller, manageable parts. A single program often consists of multiple sub programs, which are smaller sections of code designed to perform specific tasks.
Structure diagrams in program design
Structure diagrams, also known as modular diagrams, are visual tools that illustrate how a program is broken down into smaller tasks. They help in planning and organising code by showing the hierarchy of modules.
How to create structure diagrams
Creating a structure diagram involves decomposition, which is the process of breaking down a main program into progressively smaller modules until each performs a simple, individual task.
Steps to create a structure diagram:
- Start with the main program at the top level.
- Decompose it into major modules that represent key sections.
- Break each major module into sub-modules.
- Continue until reaching basic tasks that can be implemented with just a few lines of code.
Sub programs can then be written for these basic tasks, and higher-level modules can incorporate them. If the program requirements change, additional modules can be added to the diagram without disrupting the existing structure.
Example structure diagram for a noughts and crosses game
Consider designing a program to simulate a game of noughts and crosses (also known as tic-tac-toe). The structure diagram might look like this:

Structure breakdown:
- Main program: Noughts and crosses program
- Module: Set up a new game
- Enter player names
- Enter number of rounds
- Choose who goes first
- Module: Play the game (can be broken down further into tasks like making moves and checking board state)
- Module: Work out winner (can be broken down further into evaluating board and declaring result)
- Module: Reset the program (can be broken down further into clearing data and preparing for new game)
- Module: Set up a new game
In this diagram, the top level shows the overall program, with branches to major modules. Each module decomposes into simpler tasks, some requiring just a few lines of code. This visual representation makes it clear how the program flows and which parts can be developed independently.
Advantages of using structure diagrams
Structure diagrams offer several benefits that simplify the programming process and improve collaboration. By providing a clear overview of the program's architecture, they make development more efficient.
Key advantages
- Easier coding - Programmers focus on writing code for simple, individual tasks within modules, reducing complexity.
- Team collaboration - Multiple programmers can work on the same program simultaneously, as each module can be developed independently.
- Simplified testing - Each module can be tested on its own, making it easier to identify and fix issues without affecting the entire program.
- Efficient updates - Individual sub programs or modules can be modified or fixed without impacting other parts of the program.
- Reusability - Sub programs and modules created for one project can be reused in future programs, saving time and effort.
Improving program maintainability
Maintainability refers to how easily a program's source code can be understood, modified, and updated by other programmers without causing unintended issues elsewhere. In structured programming, good maintainability ensures that changes to one part of the code do not create knock-on effects in others.
Well-maintained code is crucial for long-term software development, as it allows teams to adapt programs to new requirements or fix bugs efficiently.
Features that enhance maintainability
- Comments - These are notes added to the code (often starting with
#in Python or//in other languages) that explain what key sections do. Clear comments help others understand the program's logic, but too many can make the code look cluttered. - Indentation - Using consistent spacing (typically 4 spaces in Python) to align code blocks, such as those within loops or functions. This improves readability by clearly showing the program's flow and structure.
- Meaningful naming - Variables, sub programs, and parameters should have names that describe their purpose, following standard conventions (e.g., lowercase with underscores in Python, like
player_name). This makes it easier to track and comprehend their roles. - Use of sub programs - Breaking code into sub programs allows programmers to quickly grasp how different parts function, leading to a faster understanding of the overall program.
In this example, comments explain the sub program's purpose, indentation separates the function body, and names like calculate_score clearly indicate functionality, all contributing to better maintainability.