From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,ec21c3c7cdc7ff3e X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Newsgroups: comp.lang.ada Subject: Re: Handling invalid objects References: <1142279908.327131.230200@j52g2000cwj.googlegroups.com> <41LSf.4126$TK2.1805@trnddc07> From: Brian May Date: Mon, 20 Mar 2006 09:06:32 +1100 Message-ID: User-Agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux) Cancel-Lock: sha1:adYrmAdGjYP8VCHUMZ1SCIjMrys= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii NNTP-Posting-Host: snoopy.microcomaustralia.com.au X-Trace: news.melbourne.pipenetworks.com 1142805992 202.173.153.89 (20 Mar 2006 08:06:32 +1000) X-Complaints-To: abuse@pipenetworks.com X-Abuse-Info: Please forward all headers to enable your complaint to be properly processed. Path: g2news1.google.com!news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nntp.waia.asn.au!202.72.130.18.MISMATCH!quokka.wn.com.au!news.melbourne.pipenetworks.com!not-for-mail Xref: g2news1.google.com comp.lang.ada:3479 Date: 2006-03-20T09:06:32+11:00 List-Id: >>>>> "Justin" == Justin Gombos writes: Justin> The 'Valid attribute exists to be able to handle abnormal Justin> objects. Justin> Here's a concrete example. Suppose I have: Justin> type clock_type is mod 12; Justin> function hour_of_day return clock_type; Justin> If hour_of_day gets called and for whatever reason I Justin> cannot return an hour_of_day, the caller needs to know Justin> that. Exceptions are a poor choice. The quality and Justin> style guide advises against them for a good reason; Justin> exceptions are like gotos - and produce a questionable Justin> state. It would be more graceful for me to return an Justin> invalid value (like -1), so my caller can simply do a Justin> 'Valid to discover whether the operation was successful. If you returned an invalid value, I would consider that a breach of the contract you established where you declare you only return a valid clock_type. This is similar to a problem I have seen when tutoring C++, students get stuck on the problem that looks something like this: item &find_item(...) { ... search for item ... if (found) { return item; } else { return ????; } } The ideal solution would be exceptions, but the students haven't been taught exceptions yet when conducting this exercise. Another solution would be to change the API to return a struct value, but students are told they cannot change the API. So students typically hard code it to return the first item, on the assumption that there is a first item. Either that, or simply omit the return statement, and ignore the compiler warning. Not good programming practise either way... -- Brian May