Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!mit-eddie!uw-beaver!fluke!ssc-vax!dmg From: dmg@ssc-vax.UUCP (David Geary) Newsgroups: comp.object Subject: Re: intelligent self-modifying objects? Message-ID: <3461@ssc-vax.UUCP> Date: 26 Jul 90 23:19:33 GMT Organization: Boeing Aerospace & Electronics, Seattle WA Lines: 77 In article <525@tetrauk.UUCP>, Rick Jones writes: > I am using Eiffel, and planning to make maximum use of it's exception handling > to deal with everything which is not a normal down-the-middle expected > situation (at least at the prototype stage). This includes things which are > not normally treated as errors, such as "record not found" on file access, or > "wrong entry" at a terminal. This could be a good subject for discussion; > has anyone any experience of taking this approach to building software? Well, it's a good idea to check for things which are not "normal down-the-middle expected situations". However, I do not know if one would want to raise exceptions *every* time an unexpected situation arises. I program in C, and use assertions religiously (after reading OOSC by Bertrand Meyer, BTW). Anyway, here is a function that I wrote for a generic doubly linked list package: PUBLIC listNode_t* list_GetNodeByPos(list, pos) list_t* list; int pos; { ASSERT_BOOL(list); { listNode_t* nextNode = list_GetFirstNode(list); int nodeCnt; if(list_IsPosValid(list, pos)) { for(nodeCnt=1; nextNode; ++nodeCnt) { if(nodeCnt == pos) return nextNode; nextNode = listNode_GetNodeAhead(nextNode); } } return NULL; } } The function returns a node at a specified position. It would not be a "normal down-the-middle expected situation" for someone to do: list_GetNodeByPos(list, 5); when there are only 4 nodes on the list. Initially, one might be inclined to put an assertion to assert the validity of pos upon entry to the function. This eliminates the if(list_IsPosValid(list,pos)) test from the function, and, IMHO, makes the function easier to read. However, someone may want to do the following: list_t* myList; int cnt=1; ... while(nextNode = list_GetNodeByPos(myList, cnt++)) { ... do something interesing with nextNode ... } When cnt becomes greater than the number of nodes on the list, the function (as written above) quietly returns NULL, and the while loop is terminated. However, if one puts an assertion at the top of the function to assert pos, then a message will appear on stdout (screen) like so: Assertion Failed: File: list.c Line: xxx Therefore, one must make a decision when deciding how to handle "abnormal" situations (or, in eiffel-speak, when forming the invariant for a class). Is the "abnormal" situation serious enough to warrant an assertion, or do we want to return a value which implies an error? This is a seemingly simple question, which can be surprisingly difficult to answer under certain circumstances.