Structured Programming¶
Learning Objectives: This supplementary material should be reviewed before starting the course project, but it’s already worth considering at this point. The material briefly presents one way to systematically approach solving a problem or task (for which, for example, a computer program is originally created) using a divide and conquer method, rather than jumping straight into coding with a "Stetson-Harrison" method.
Structured and Modular Programming refers to a method of designing and programming a computer program where the problem (or the program being created) is logically divided into small and simple tasks, which are implemented as subroutines, or functions in C language. The idea is to break down tasks/problems into such small pieces that they eventually reach the level of an individual statement in the programming language.
This approach to software development provides the following benefits:
- Developing programs becomes easier because handling complex and/or large problems is simplified through libraries, functions, code blocks, etc.
- Debugging becomes easier since errors are more easily spotted in short segments of the program (one at a time).
- Code reusability improves. Functions should be written in such a way that they can be reused later in another context. Even if some modification is necessary for a previously written function, this is still faster than writing a new one from scratch.
The Design Process¶
As in the real world, planning in advance saves a lot of time and effort. Before writing even a single line of code, the program must be designed and divided into parts. Professional programmers use a variety of planning tools, but for us, pen and paper will suffice.
Plan your programs well before coding to avoid getting lost in a tangle of poorly structured code. Here’s a simple example of a purchase event:
Program Example:
- Purchase Event
- Execute until the last price is entered
- Print to the screen: "Enter price:"
- Wait until the price is entered
- Add the price to the total
- Print: "Total:"
- Print: "Amount given:"
- Wait until the money is entered
- Calculate change to be given
- Print: "Change:"
- Return to the interface
Even though our plan has become more detailed, we can add even more specifics:
- Purchase Event
- Until the last price is entered
- Print to the monitor: "Enter price:"
- Wait until the price is entered
- Save the price
- Determine the date
- Open file ppkkvv.txt
- Print the price to the end of the file
- Close the file
- Add the price to the total variable
- Print: "Total:"
- Print: "Amount given:"
- Wait until the money is entered
- Calculate change to be given
- Print: "Change:"
- Return to the interface
We can keep refining until we have such a detailed plan that we end up with an algorithm described in pseudocode!
Once the entire program is put together in this abstract form, a large part of the software project is already done, and we can start implementing the program in the chosen programming language.
More info¶
Explore following Wikipedia pages:
Conclusion¶
Often, beginning programmers underestimate the importance of planning and the time it takes. It’s easier to just jump in and start hacking away at code, thinking the problems will be fixed as they come up. This will almost certainly result in a messy codebase and spaghetti code. When dealing with difficult bugs, you’ll eventually face two choices: add more spaghetti, or start fresh from scratch. Consider yourself warned.
So let’s really make use of the structured programming approach when designing and solving programming tasks and implement code as easily understandable functions. It’s worth the effort.
Give feedback on this content
Comments about this material