Decomposition is the process of breaking a problem down into its component parts.
For instance if you are designing a hangman style game then your program might be broken down into:
- Displaying the start menu
- Asking for a letter to be entered
- Displaying the gallows
- Displaying a game over screen.
Each of these components can be broken down further into sub components,
- Displaying the start menu:
- Print out all the menu options
- Ask the user for an input
- Redirect the user to the correct screen depending on their choice.
In programming we often use functions, procedures and modules to decompose the problem down into separate sub problems. We can take a structured approach to decomposition, for instance by using stepwise refinement.
A key skill in becoming a programmer is being able to pick out the patterns present in both problems to be solved and the data the problems work on. Recognition of patterns allows algorithms to be adapted so that multiple problems can be solved more efficiently.
In the example above there is a clear pattern and a solution can easily be programmed:
While the above example only saves a couple of lines of code, if the number of players is increased you need only change the number 5.
This example is a little more complex and here functions can be useful in dealing with patterns.
This improved version has a number of benefits:
- The less code is required to solve the problem
- The code of more readable because the code has been split into functions.
- The code is more reusable because it has been generalized to solve a number of similar problems.
Abstraction is the process of simplifying a problem and throwing unnecessary detail until we are left with only the information that we need to solve a problem.
What information is deemed necessary or otherwise depends wholly at the problem at hand. For example if we are storing data about pupils in a school database then we might need to store their education history, whereas in a database for a hospital this data would not be needed.
The abstraction of problems involves making choices such as what data type to use (e.g. integer versus real) or whether to store data in an array or a dictionary.