prev next

Class Stubs

Writing even simple computer programs is not an easy task.  There are many steps to the process and there are many details that you need to pay attention to.  If you miss one step, or make one little mistake, your program won't compile, run, or may produce incorrect results.  Like many difficult tasks, you can make things go a little easier if you do things one step at a time and check your work as you go along.  You can use this same process when writing computer programs.

By now you should realize that writing code is only one small part of the overall programming process.  After developing, or being given, good specifications the next step is to develop a design for the program based on the specifications.   After you have developed a good design for your program it is time to start translating that design into code.  If you did the design step well, the coding process should go smoothly.  You will find yourself spending a considerable amount of time writing class definitions that need to be syntactically correct.  One technique that you can use when writing code is to write a stub for each class in your program.  A class stub contains only the code that identifies what the data members are and the headers for each of the methods in the class.

Consider writing a class, named Point, that defines objects that represent points in the XY plane.  The UML class diagram below gives the state and behavior for this class:

PointUML.gif (4093 bytes)

Take a few minutes to look at the documentation for this class by clicking here.

The stub for the Point class, based on the information given in the UML diagram and the documentation, can be found here.  Note that in the stub file the declarations for all of the data members and methods are present, but the code that is required to implement each method has been omitted.  The only code that appears in the body of each method are return statements.  The return statements must be placed in methods that return values, otherwise the compiler will produce error messages.  By writing and compiling the stub you can be guaranteed that at least the syntax for the declaration of the data members and methods is correct.

Now you can continue the development process by filling in the code for each method.  A good programmer will fill in the code for the methods, one or two at a time.  Rarely will a programmer write all of the code and then feed it to the compiler in the hope that they got everything right.  By writing and testing the methods, one at a time, you limit the places in which errors will occur which will make them easier to find.  The complete code for the Point class can be found by clicking here.


prev next