Previous | Next | Table of Contents | Index | Program List | Copyright

8.11 Tricks of the Trade: Common Programming Errors

When programmers use records, their most common compilation error is incorrectly specifying the record field to be manipulated. The full field selector (record variable and field name) must be used unless the entire record is to be manipulated. Copying one record to another or comparison of two records can be done only if the two records are of the same type. Passing a record as a parameter to a procedure or function can only be done if the actual parameter has the same type as the formal one. When records are read, or written at the terminal, each field must be processed separately.

Similarly, in using arrays the most common compilation errors come from type inconsistencies. Remember that two arrays must have the same type name to be assigned or compared and that an array passed as an actual parameter must have the same type as the formal parameter.

The most common run-time error when arrays are used is a Constraint_Error, raised when the subscript value is outside the allowable range for the array being processed. Most often, this error is caused by an incorrect subscript expression, a loop parameter error, or a nonterminating loop. Before you spend considerable time debugging, you should carefully check all suspect subscript calculations for out-of-range errors. You can check most easily by inserting diagnostic output statements in your program in order to print subscript values that might be out of range.

If an out-of-range subscript occurs inside a loop, you should make sure that the loop is terminating properly. If the loop control variable is not being updated as expected, the loop may be repeated more often than required. This could happen, for example, if the update step came after the loop end statement or if the loop begin and end were erroneously omitted.

You should also doublecheck the subscript values at the loop boundaries. If these values are in range, it is likely that all other subscript references in the loop will be in range as well. Using the form

    FOR SubscriptVariable IN IndexType LOOP
instead of writing the bounds explicitly helps to ensure that the subscript variable stays in bounds, because the loop body cannot modify it. As with all Ada data types, make sure that there are no type inconsistencies. The subscript type and element type used in all array references must correspond to the types specified in the array declaration.


Previous | Next | Table of Contents | Index | Program List | Copyright

Copyright © 1996 by Addison-Wesley Publishing Company, Inc.