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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4b06f8f15f01a568 X-Google-Attributes: gid103376,public From: Matthew Heaney Subject: Re: Software landmines (was: Why C++ is successful) Date: 1998/08/29 Message-ID: #1/1 X-Deja-AN: 385861104 Sender: matt@mheaney.ni.net References: <6rnhhe$e2u$1@nnrp1.dejanews.com> <6rsg0d$pcj@dfw-ixnews3.ix.netcom.com> <6s6v4i$mht@dfw-ixnews8.ix.netcom.com> NNTP-Posting-Date: Fri, 28 Aug 1998 20:44:12 PDT Newsgroups: comp.lang.ada Date: 1998-08-29T00:00:00+00:00 List-Id: Richard D Riehle writes: > So, in this respect, C++ initialization lists better express the > "binding" requirement than Ada assignments. One of the areas in Ada in which the assignment vs initialization issue arises is in the declaration of limited types: declare O : LT; begin Initialize (O); The thing I find distasteful here is that the initialization of object O occurs after its declaration. Sometimes this means the state of O is undefined. (However, many times limited objects come with a defined, default state. For example, Is_Open is defined to return False for a newly declared instance of File_Type.) I've advocated in the past that the language syntax should be amended to add an initialization operation ("constructor"): declare File : File_Type'Open (In_Mode, "matt.dat"); begin We could even use the constant keyword in such a declaration. > Also, several consecutive assignment statements in Ada will often be > more readable than the parentheticaly clauses of function returns that > serve as their substitute. Agree. I often see programmers use many nested function calls in an expression. What I have to do then is to mentally unravel the expression. So why not save me the trouble? Use a declare block to declare a few intermediate objects, and feed them to the ultimate subprogram call. > The suggestion that we use fewer assignment statements is liable to > create the same mess that "gotoless" programming fomented in the COBOL > world. Let's make sure everyone knows what this means: o It DOES mean "don't use a variable when a constant will do." o It does NOT mean "use nested function calls to return intermediate values, instead of declaring intermediate constant objects" Yes, you really should use constant object declarations to hold intermediate values, even though this requires a few more "assignments." (The scare quotes are there to remind the reader that the declaration of a constant object isn't really an assignment.) > Ada is designed to encourage the use of the assigment statement. Yes. Far too few programmers use declare blocks and constant object declarations to simplify complex expressions. Instead of Op (Get_A (10), Get_B (X), Get_C (Y)); it's better to do declare A : constant Integer := Get_A (10); B : constant Float := Get_B (X); C : constant Boolean := Get_C (Y); begin Op (A, B, C); end; As we have noted, the declarations above aren't really "assignment" statements. They are "binding" operations. The distinction would be more clear (and consistent) if the language allowed you to say: declare A : Integer is Get_A (10); B : Float is Get_B (X); C : Boolean is Get_C (Y); begin Op (A, B, C); end; The "renames" clause is a binding operator in Ada (although technically it binds a name to an object, not a value). A hip way to do the above directly in the language is declare A : Integer renames Get_A (10); B : Float renames Get_B (X); C : Boolean renames Get_C (Y); begin Op (A, B, C); end; That way we can avoid the use of the assignment operator, so the declaration won't be confused with an assignment statement. It's a bit of a bummer that we have 3 ways ("constant T :=", "renames", "is") of doing essentially the same thing. Why not just one way? > Suggesting that we do not use it so often is probably > counterproductive and can only be realized with some important changes > in the language. An unambiguous way to say this is that, in a declaration, you should use the keyword "constant" whenever the object doesn't change its value. Yet another way to say this is "don't use assignment when you really mean binding." Matt P.S. About the meaning of binding: let me dig up my books on programming language theory. You usually see it mentioned in books about functional programming, and in explications of denotational semantics.