Defining how the program should work

After choosing a specific programming language, don’t start typing commands into your computer just yet. Just as programmers create mock-ups (prototypes) of their program’s user interface, they often create mock-up instructions that describe exactly how a program works. These mock-up instructions are known as pseudocode.

If you need to write a program that guides a nuclear missile to another city to wipe out all signs of life within a 100-mile radius, your pseudocode may look as follows:


1. Get the target’s coordinates.
2. Get the missile’s current coordinates.
3. Calculate a trajectory so the missile hits the target.
4. Detonate the nuclear warhead.

By using pseudocode, you can detect flaws in your logic before you start writing your program - places where the logic behind your program gets buried beneath the complexity of a specific programming language’s syntax.

In the preceding example, you can see that each pseudocode instruction needs further refining before you can start writing your program. You can’t just tell a computer, “Get the target’s coordinates,” because the computer wants to know, “Exactly how do I get the target’s coordinates?” So rewriting the preceding pseudocode may look as follows:


1. Get the target’s coordinates.
            a. Have a missile technician type the target
           coordinates.
            b. Make sure that the target coordinates are
           valid.
            c. Store the target coordinates in memory.
2. Get the missile’s current coordinates.
3. Calculate a trajectory so the missile hits the target.
4. Detonate the nuclear warhead.
Image from book

Technical StuffUsing multiple programming languages

Instead of writing an entire program in one programming language (such as C++), some compilers can convert source code into a special file known as an object file. The purpose of object files is to allow one programmer to write a program in C++, another in assembly language, and still a third in Pascal. Each programmer writes a portion of the program in his or her favorite language and stores it in a separate object file. Then the programmers connect (or link) all these object files together to create one big program. The program that converts multiple object files into an executable program is known as a linker.

In the world of Microsoft Windows, another way to write a program by using multiple languages is to use dynamic link libraries (DLLs), which are special programs that don’t have a user interface. One programmer can use C, another can use Java, and a third can use COBOL to create three separate DLL files. Then a fourth programmer can write a program using another language such as Visual Basic, which creates the user interface and uses the commands that each separate DLL file stores.

A third way to write a program is to use your favorite language (such as Pascal) and then write assembly-language instructions directly in parts of your program. (Just be aware that not all compilers enable you to switch between different languages within the same program.)

Finally, Microsoft offers a programming framework known as .NET. By using the .NET framework, one programmer can program in C#, another can program in FORTRAN, and still another can program in BASIC. Then their different programs can share data and communicate with other programs through the .NET framework and create a single user interface that unifies these separate programs. The whole point to all these different methods is that by using different programming languages, you can take advantage of each language’s strengths while minimizing its weaknesses.

Image from book
 

You can refine the instructions even further to specify how the computer works in more detail, as follows:


1. Get the target’s coordinates.
            a. Have a missile technician type the target
           coordinates.
            b. Make sure that the target coordinates are
           valid.
                 1) Make sure that the target coordinates
           are complete.
                 2) Check to make sure that the target
           coordinates are within the missile’s range.
                 3) Make sure that the target coordinates
           don’t accidentally aim the missile at friendly
           territories.
            c. Store the target coordinates in memory.
2. Get the missile’s current coordinates.
3. Calculate a trajectory so the missile hits the target.
4. Detonate the nuclear warhead.

When programmers define the general tasks that a program needs to accomplish and then refine each step in greater detail, they say that they’re doing a top-down design. In other words, they start at the top (with the general tasks that the program needs to do) and then work their way down, defining each task in greater detail until the pseudocode describes every possible step that the computer must go through.

Writing pseudocode can prove to be time-consuming. But the alternative is to start writing a program with no planning whatsoever, which is like hopping in your car and driving north and then wondering why you never seem to wind up in Florida.

Remember Pseudocode is a tool that you can use to outline the structure of your program so that you can see all the possible data that the computer needs to accomplish a given task. The idea is to use English (or whatever language you understand best) to describe the computer’s step-by-step actions so that you can use the pseudocode as a map for writing the actual program in whatever language (C/C++, FORTRAN, Pascal, Java, and so on) that you choose.


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply



eXTReMe Tracker