home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!wupost!cs.utexas.edu!hellgate.utah.edu!fcom.cc.utah.edu!swillden
- From: swillden@news.ccutah.edu (Shawn Willden)
- Subject: Re: question on REFERENCES
- Message-ID: <1992Nov20.195725.3009@fcom.cc.utah.edu>
- Sender: news@fcom.cc.utah.edu
- Organization: University of Utah Computer Center
- X-Newsreader: Tin 1.1 PL3
- References: <5239@holden.lulea.trab.se>
- Date: Fri, 20 Nov 92 19:57:25 GMT
- Lines: 64
-
- jbn@lulea.trab.se (Johan Bengtsson) writes:
- : swillden@news.ccutah.edu (Shawn Willden) writes:
-
- [ Much stuff deleted ]
-
- : : How is this different from a test for NULL? The syntax may be slightly
- : : cleaner but not much is really gained by this approach.
- :
- : You can often arrange things so that it is safe to attempt quite a few
- : function calls without checking anything at all (the first error is
- : safely stored away in a global variable). If on the average you make
- : a few (say five) function calls before testing, you get a significant
- : reduction in the number of error tests, making the real algorithm more
- : easily recognizable.
-
- Where this can be arranged, I would say it's a good idea.
-
- : : Something must
- : : still be returned and that something will be invalid if the function
- : : fails. Why is the programmer more likely to remember to test an error
- : : flag than to test a returned pointer against NULL?
- :
- : Because the error flag does not go away. A later check will detect the
- : error. Often this is quite acceptable, especially if the error is very
- : rare. Also, you can arrange for checking the error flag at program
- : termination. Any unhandled error at program exit can then be reported.
- : Similarly, if an object keeps an internal error state, then its
- : destructor should check the error state. If there is an error, the destructor
- : should report it when the object goes out of scope (the iostream classes
- : do not do this, but IMHO they should).
-
- Again, these are useful options.
-
- [...]
-
- : : In addition, it requires all code
- : : that uses an object to test its goodness and then take appropriate
- : : action if not. In many cases it is more convenient to design
- : : the object such that a given instance is always valid and to just avoid
- : : creating invalid ones.
- :
- : Yes, and a nice variation is to return an object that checks the
- : "goodness" flag itself, and performs safe default actions if it
- : is an invalid object. Sometimes this can be programmed by returning
- : an object of a subclass of the return type (which is a reference),
- : that overrides virtual functions appropriately.
-
- This is an interesting variation that hadn't occurred to me. It
- would have to be used carefully, though, because in many cases the
- value returned from a function is copied into another object instead
- of being used directly. How often have you found this approach
- worthwhile? It seems to me that this would be a more useful option
- if you return a pointer, which you could, of course, guarantee to be
- non-NULL.
-
- : --
- : --------------------------------------------------------------------------
- : | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden |
- : | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490 |
- : --------------------------------------------------------------------------
-
- --
- Shawn Willden
- swillden@icarus.weber.edu
-