< previous page page_1110 next page >

Page 1110
When deleting from a data structure, the function can easily detect that the component is not there or that there is no component to remove. Should these functions have been written to guard against trying to delete a component that is not there? It depends on the interface between the calling code and the function. Either the caller is responsible for ensuring that the value is there to be deleted (guarantees the precondition), or the function should test for the case in which there is no value to delete or the specific value is not there. If the function does the testing, a flag should be returned to the caller showing whether or not the value was deleted successfully.
Following is a version of the DeleteTop function in which error checking is included within the function itself. Notice that there is an additional parameter named failed. Notice also that part of the original preconditionthat the list must not be emptyhas been eliminated.
void OrdList::DeleteTop(
     /* out */ ComponentType& item,      // Item removed from list
     /* out */ Boolean&       failed )   // Error flag

// Precondition:
//     component members of list nodes are in ascending order
// Postcondition:
//     IF list at entry is empty
//         failed == TRUE  &&  Value of item is undefined
//     ELSE
//         failed == FALSE
//      && item == component member of first list node at entry
//      && Node containing item is removed from linked list
//      && component members of list nodes are in ascending order

{
    if (head == NULL)
        failed = TRUE;
    else
    {
        NodePtr tempPtr = head;    // Temporary pointer
        item = head->component;
        head = head->link;
        delete tempPtr;
        failed = FALSE;
    }
}
Whenever it is possible for an error condition to occur in a function, you must decide where to check for the condition. If the caller is responsible for seeing that the situation does not occur, the assumption that the condition will not occur must be stated in the precondition of the function. If the function is to check for the error condition, your documentation of the function

 
< previous page page_1110 next page >