9.6 Structured Programming Basics

I l @ ve RuBoard

Computer scientists spend a great deal of time and effort studying how to program. The result is that they come up with the absolutely , positively, best programming methodology ”a new one each month. Some of these systems include flow charts , top-down programming, bottom-up programming, structured programming, and object-oriented programming.

Now that you have learned about functions, we can talk about using structured programming techniques to design programs. This is a way of dividing up or structuring a program into small, well-defined functions. It makes the program easy to write and easy to understand. I don't claim that this system is the absolute best way to program, but it happens to be the system that works best for me. If another system works better for you, use it.

Structured programming focuses on a program's code. Later you'll see how to merge code and data to form classes and begin to perform object-oriented programming.

The first step in programming is to decide what you are going to do. This has already been described in Chapter 7. Next, decide how you are going to structure your data.

Finally, the coding phase begins. When writing a paper, you start with an outline, with each section in the paper described by a single sentence . The details are filled in later. Writing a program is similar. You start with an outline, but this outline is your main function. The details can be hidden within other functions. For example, the program in Example 9-10 solves all of the world's problems.

Example 9-10. A global solution
 int main(  )  {      init(  );      solve_problems(  );      finish_up(  ); } 

Of course, some of the details remain to be filled in.

Start by writing the main function. It should be less than two pages long. If it grows longer, consider splitting it up into two smaller, simpler functions. The size of the function should be limited to three pages, because that is about the maximum amount of information a human being can store in short- term memory at one time. After the main function is complete, you can start on the other functions. This type of structured programming is called top-down programming . You start at the top ( main ) and work your way down.

Another type of coding is called bottom-up programming . This involves writing the lowest -level function first, testing it, and then building on that working set. I tend to use some bottom-up techniques when I'm working with a new standard function that I haven't used before. I write a small function to make sure I really know how the function works and continue from there. This is the approach used in Chapter 7 to construct the calculator program.

Later on, in Chapter 13, we'll learn about object-oriented programming. That's where you design your data and the things that can be done with it together in something called a class.

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net