comp.lang.ada
 help / color / mirror / Atom feed
From: jls@rutabaga.Rational.COM (Jim Showalter)
Subject: Re: Pre-condition vs. Post-condition
Date: 27 Mar 91 21:34:55 GMT	[thread overview]
Message-ID: <jls.670109695@rutabaga> (raw)
In-Reply-To: 5074@goanna.cs.rmit.oz.au

>Perhaps I have completely misunderstood the point of default values for
>fields of records.  I thought that this, like default values for
>omitted parameters, was a dodge to help people cope with the problem
>where there is
>	-- a package P providing a record type or procedure, and
>	-- a package U using that record type or procedure
>and package P is updated to include extra fields in the record type
>or procedure, and it may not be practical to update the source of
>package U.  (In fact, U may be a customer who won't let you anywhere
>near their source.)  In this practically important but conceptually
>limited case, the package provider _can_ determine sensible default
>values: "those values which make the new version of the software
>act just like the old version".

Since I tend to use private types, this argument is spurious. If I
am providing a private type in P to which I have added a new field, U is
unable to initialize the fields ANYWAY: I must do it for the client,
since I am providing the abstraction upon which the client depends.

Furthermore, if each U was required to initialize the values in my
type, the odds of at least someone screwing it up increase linearly
with the number of U's.

Finally, consider this case:

    package Some_Package is

        type T is private;

        function Initialize (From_Some_Input : in A_Type) return T;

        procedure Do_Something (To_This_Object : in out T);

        Not_Initialized : exception;

    private
   
        type T is record
                      Some_Field : Some_Type;
                      Is_Initialized : Boolean := False;
                  end record;

    end Some_Package;

I, the provider of T, have a field in the record that is used to insure
consistency. The default initial value is not something the client can
override, so there is no way the client can pass an unitialized value
of T to me and get away with it. Without default initialization I cannot
ensure this.

    package body Some_Package is

        function Initialize (From_Some_Input : in A_Type) return T is
       
            The_T : T;

        begin
            The_T.Some_Field := Some_Normalization_On (From_Some_Input);
            The_T.Is_Initialized := True; <====***
            return The_T;
        end Initialize;

        procedure Do_Something (To_This_Object : in out T) is
        begin
            if not To_This_Object.Is_Initialized then
                raise Not_Initialized; <====****
            end if;
            {do whatever to the object>
        end Do_Something;

    end Some_Package;

This sort of thing is very useful for applications where data integrity
must be ensured (can you think of any where data integrity is NOT
important?...).

>Now consider the kinds of problems that can arise with default
>initialisations, *where the package user knows what the default is*.

That's not the issue: default initialization, particularly of private
types, is for the benefit of the SUPPLIER, not the client.

>One thing which would reduce this temptation would
>be to set things up so that a type or subtype can be specified in
>a package specification and its default value in the package body.

Actually, this is a pretty good idea, at least for visible types
(it doesn't matter for private types).
--
***** DISCLAIMER: The opinions expressed herein are my own, except in
      the realm of software engineering, in which case I've borrowed
      them from incredibly smart people.

  parent reply	other threads:[~1991-03-27 21:34 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1991-03-24 21:23 Pre-condition vs. Post-condition stt
1991-03-25 16:00 ` Arthur Evans
1991-03-25 17:05   ` Michael Feldman
1991-03-26  4:31     ` Jim Showalter
1991-03-26 10:21       ` Richard A. O'Keefe
1991-03-26 16:44         ` Michael Feldman
1991-03-26 22:03           ` Richard A. O'Keefe
1991-03-26 23:36             ` Michael Feldman
1991-03-28 20:43               ` Pre-condition vs. Post-condition (actually inintialization) Dana Carson
1991-03-27 21:34             ` Jim Showalter [this message]
1991-03-28  2:54               ` Pre-condition vs. Post-condition Michael Feldman
1991-03-29  3:28                 ` Jim Showalter
1991-03-27  3:12         ` Jim Showalter
1991-03-27 21:32         ` Initialization Paul Stachour
  -- strict thread matches above, loose matches on Subject: below --
1991-03-18 15:47 Pre-condition vs. Post-condition "Norman H. Cohen"
1991-03-15  3:57 Chris M. Little
1991-03-15 19:07 ` Michael Feldman
1991-03-17 12:26   ` George C. Harrison, Norfolk State University
1991-03-18 15:04   ` Joe Hollingsworth
1991-03-18 19:51     ` Marlene M. Eckert
1991-03-19 19:07       ` Michael Feldman
1991-03-21  3:01         ` Jim Showalter
1991-03-21 18:40           ` Michael Feldman
1991-03-19 20:38       ` Charles H. Sampson
1991-03-21  3:06         ` Jim Showalter
1991-03-19 21:07       ` Jim Showalter
1991-03-19  7:38     ` Jim Showalter
1991-03-19 14:46       ` Joe Hollingsworth
1991-03-21  2:46         ` Jim Showalter
1991-03-22 15:18       ` Brad Balfour
1991-03-19 18:17   ` Mike Gilbert
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox