Re: C 99 compiler access

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 08/29/04


Date: Sun, 29 Aug 2004 12:59:32 -0400 (EDT)


On Sun, 29 Aug 2004, Ben Pfaff wrote:
>
> "Malcolm" <malcolm@55bank.freeserve.co.uk> writes:
>> Instead we had a lot of feature of dubious use which require rewrites of the
>> compiler. For instance variables can now be declared anywhere, which seems
>> to be just a way of making code less organised and harder to read, [...]
>
> I disagree. Declaring a variable in mid-block is a valuable way
> to reduce the scope of that variable, giving the programmer less
> time to screw it up. Furthermore, a variable declared in
> mid-block can usually be initialized in the declaration, which
> means there's a bigger chance it can be marked `const', which in
> turn gives the programmer less to screw up.

   I would agree with you [Ben] if there were some intuitive mechanism
in C99 for /closing/ the scope of a variable with a minimum of clutter.
That is, a lot of the time I want to write the equivalent of:

     int foo, baz;
     process(foo, baz);
     { /* scope opens */
         int bar = 2*foo+1;
         process2(bar);
     } /* scope closes */
     process(baz, foo);

   The above code is valid C90 and C99, but it's ugly (IMHO) because
the braces and indentation don't actually represent any control
structure.
   C99 fixes /half/ of the problem by letting me write

     int foo, baz;
     process(foo, baz);
     int bar = 2*foo+1; /* scope opens */
     process2(bar);
     process(baz, foo);

but now, as I said, the problem is that there's no way to "close" the
scope of 'bar' where I intend. 'bar' can be destroyed (by the computer)
and forgotten about (by the reader) once we get to line 5; unfortunately,
there's no way to tell the reader that. And in a long function, the
mental clutter of so many "scope-unclosed" temporary variables can really
impede understanding. ("Okay, here he's defining 'bar'. And he uses it
on the next line... and nowhere else? That can't be right... grep grep...
okay, I guess that is right. Now where was I?")

   At least, that's what I find. YMMV.

-Arthur



Relevant Pages

  • Re: pydoc enforcement.
    ... I have written a small tool for declaring ... class Bar: ... python code using tools from stdlib, so it wasn't really wasted time. ... using some modules/API's that have less than stellar documentation. ...
    (comp.lang.python)
  • Re: Is VB Caca??
    ... It warns you because you are declaring a variable in the scope that hides a variable with the same name with a larger scope. ... Mike Ober. ... Dim s As String ...
    (microsoft.public.dotnet.languages.vb)
  • Untold(...) variable scope in ASP.NET
    ... We all know that declaring a variable as 'Public' gives it global ... scope over the application, nothing new... ... I usually dont like to create session variable because i found them ... scope goes beyong the user session, actually you are better off using ...
    (microsoft.public.dotnet.languages.vb)
  • Re: working example File::Taill
    ... A> Couldnt this be a disadvantage in big projects, where declaring ... A> criables at the top of the appropriate scope allows ppl to find *all* ... reason not to do this. ... but the strong consensus is that it is poor style. ...
    (comp.lang.perl.misc)
  • Re: Suggestion: Python global scope
    ... declaring a variable using the global statement automatically ... variable from the global scope into the current scope. ...
    (comp.lang.python)