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 Path: g2news1.google.com!news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!wns14feed!worldnet.att.net!attbi_s71.POSTED!53ab2750!not-for-mail From: "Jeffrey R. Carter" Organization: jrcarter at acm dot org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060130 SeaMonkey/1.0 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Handling invalid objects References: <1142279908.327131.230200@j52g2000cwj.googlegroups.com> <41LSf.4126$TK2.1805@trnddc07> In-Reply-To: <41LSf.4126$TK2.1805@trnddc07> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <7LOSf.34253$oL.29742@attbi_s71> NNTP-Posting-Host: 12.214.7.86 X-Complaints-To: abuse@mchsi.com X-Trace: attbi_s71 1142667331 12.214.7.86 (Sat, 18 Mar 2006 07:35:31 GMT) NNTP-Posting-Date: Sat, 18 Mar 2006 07:35:31 GMT Date: Sat, 18 Mar 2006 07:35:31 GMT Xref: g2news1.google.com comp.lang.ada:3423 Date: 2006-03-18T07:35:31+00:00 List-Id: Justin Gombos wrote: > > The 'Valid attribute exists to be able to handle abnormal objects. And it exists to handle results from outside normal Ada processing, such as values from hardware or from interfacing to other languages. There's a basic SW-engineering principle that one thing should have one meaning. Using one thing with 2 meanings, which is what you're asking for, clearly violates that principle. You'll find plenty of code, including some things in the standard Ada library, that violate this principle. Nevertheless, it is rarely a good idea to violate this principle. > type clock_type is mod 12; > > function hour_of_day return clock_type; This seems to specify a function that is expected to always be able to return a valid value. If it is expected to only be able to return a valid value some of the time, that should be documented, probably by naming the function something like Try_To_Get_Hour_Of_Day. If the function should always be able to return a valid value, and it can't, that is clearly an exceptional situation, and an exception is clearly the correct way to deal with such a situation. If valid values are not always possible, then it's a question of how often they are not available. If it's a fairly common occurrence (and that can be less than 50% of the time), then it's not an exceptional situation, and an unconstrained record type with a Boolean discriminant is probably the best thing to return. If, on the other hand, it's a rare occurrence, then it's still an exceptional situation, and an exception is the correct solution. Anyone who has seen C code that correctly checks and deals with all returned error codes and success flags knows how complicated and unreadable such code is. The common practice in C is to not do the necessary checks so that the code is simpler and more readable. That is clearly not an acceptable solution. Exceptions separate the normal processing from the exceptional processing, allowing simple and readable code for the normal case while ensuring that all checks are performed and dealt with. There are cases where exceptions are outlawed; most of them are not justified. Even if they were all justified, advocating a return to the mess that error codes create is not a viable option. -- Jeff Carter "The time has come to act, and act fast. I'm leaving." Blazing Saddles 36