Wikijunior:Programming for Kids/Top-Down or Bottom-Up?

From Wikibooks, open books for an open world
< Wikijunior:Programming for Kids
Jump to: navigation, search
Wikijunior:Programming for Kids
Designing Your Solution Top-Down or Bottom-Up? Writing Your Algorithms

The top-down and bottom-up approaches are well suited for different situations. Let's discuss them here.

The Top-Down Approach[edit]

Top-down structure diagram.svg

With the top-down approach, we start with our top-level program, then divide and sub-divide it into many different modules. The division process is known as stepwise refinement. As we design each module, we will discover what kind of submodules we will need, then go on to program those. After programming our smaller submodules, we will group them together into the larger module. In the diagram, each level of refinement is labelled as LN.

When we write higher-level modules, we need some sort of placeholder to take the place of the lower-level modules. These placeholders are called stubs. They allow us to test higher-level modules without coding the lower-level ones first.

The key to stepwise refinement is that each module should end up doing only one task. Bottom-level modules can be completed using only a few lines of code.

One advantage of the top-down approach is that we see the full picture at first. When we write our high-level modules, we don't need to care about the small details yet. Our lower-level modules are designed with our higher-level modules in mind, and we already know in great detail what we want to achieve with them. This ensures compatibility between high and lower levels. Therefore, the top-down approach is suitable for larger problems.

The Bottom-Up Approach[edit]

Bottomup.svg

The bottom-up approach, as the name suggests, starts from the bottom of our structure and slowly gets to the top. To be more precise, we start by creating the smallest modules, put them together to form larger modules, and continue the process until we have our whole thing built. This approach to programming is especially useful when we already have pre-made modules at our disposal.

When we design top-down, each of our individual modules were designed to work for the above module only. When we design bottom-up, each of our individual modules serves a purpose on its own. Therefore, the smaller modules can be used more than one larger module, facilitating code reuse. Moreover, general-purpose modules are easier to read and understand than specific ones, so the bottom-up approach also enhances code readability.

A lego building uses the bottom-up approach, starting from the base and piling up more and more blocks until the top is reached.

The bottom-up approach is also simpler as we don't need to write stubs for higher-level modules. We write a stub, test it, and move on.

However, unlike the top-down approach, we don't get to see the full picture at first. When we write out low-level modules, we don't have our higher-level ones in mind. This may lead to poor connections between the higher- and lower-level modules. It is also more difficult to monitor and manage the project. Therefore, the bottom-up approach is better suited for smaller problems.

Libraries[edit]

APE is a physics engine for Flash.

Whether or not we're using the top-down or bottom-up approach, we tend to use libraries to help us. A library is a pre-defined set of modules we can use in programming. For example, a physics engine allows us to perform realistic simulations in an application. Using a library can be regarded as a form of bottom-up design.

Most programming languages come with a standard library. A standard library can be used whenever we write a program. For example, ActionScript, the programming language for Flash applications, contains a sophisticated standard library for representing visual elements on a computer screen.

Can we start now?[edit]

Now that we've drawn up a blueprint for our program, we can start writing its algorithms. Only one problem... what is an algorithm?