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=-0.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, FREEMAIL_REPLY autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,96ae138aab3beb03 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-05-31 07:48:31 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed00.sul.t-online.de!t-online.de!news-lei1.dfn.de!news-fra1.dfn.de!news.tele.dk!not-for-mail Message-ID: <3CF78D3D.3030400@yahoo.com> Date: Fri, 31 May 2002 16:48:29 +0200 From: David Rasmussen User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.9) Gecko/20020412 Debian/0.9.9-6 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Localized Variable Declaration References: <3CF77998.9040806@yahoo.com> <3CF77CDA.3090805@yahoo.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Organization: TDC Internet NNTP-Posting-Host: 195.215.62.2 X-Trace: 1022856510 dtext.news.tele.dk 96090 195.215.62.2 X-Complaints-To: abuse@post.tele.dk Xref: archiver1.google.com comp.lang.ada:25084 Date: 2002-05-31T16:48:29+02:00 List-Id: martin.m.dowie wrote: > "David Rasmussen" wrote in message > news:3CF77CDA.3090805@yahoo.com... > >>Oh sure, but that is pretty tedious, compared to just a one line >>variable declation. The point is, to be a bit extreme, that centralized >>variable declations (e.g. at the "beginning" of the function) as done in >>C and Pascal, are evil. Variables should be declared close to where they >>are needed, IMO. > > > Sure, but that's what you get with a declare ... begin ... end. You also get > to delimit the scope in the example I gave there could have been 100 more > lines after the 'end' that just didn't need visibility of the local > variable. Can > you do that with a 'one line variable declaration'? > > You can do the same thing in C++ by just doing int whatever(int foo) { ... ... { // local scope starts here int local = 42; .... } // and ends here ... } very simple IMO. So you can do the same in C++ as in Ada, and more. It's usually the other way around. And usually Ada has a good reason for doing things they way it does it. I just don't understand the justification for this limitation. Consider this: void something() { cout << "Write two numbers: "; int a; cin >> a; int b; cin >> b; double c = a * b * 42.42; } This demonstrates very much what I mean. Variables aren't declared until they can immediately be given a value, even by initialization or by some sort of assignment immediately after declaration. This is easier to read and understand and minimizes bugs with uninitialized values etc. Compare it to: void something() { int a,b; double c; cout << "Write two numbers: "; cin >> a; cin >> b; c = a * b * 42.42; } I admit that in this simple example, the second looks simpler, but still, the types of variables are taken away from the local place where they are used, and variables aren't initialized when they are declared. In larger functions and larger programs in general, it makes a difference. Now, of course this can be done in Ada with the declare block, but it will be much more tedious and harder to read. /David