Re: C 99 compiler access
From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 08/29/04
- Next message: Chris Torek: "Re: fscanf and linked list problem"
- Previous message: Joona I Palaste: "Re: A for loop iterating over the complete range of a variable"
- In reply to: Ben Pfaff: "Re: C 99 compiler access"
- Next in thread: Minti: "Re: C 99 compiler access"
- Reply: Minti: "Re: C 99 compiler access"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Chris Torek: "Re: fscanf and linked list problem"
- Previous message: Joona I Palaste: "Re: A for loop iterating over the complete range of a variable"
- In reply to: Ben Pfaff: "Re: C 99 compiler access"
- Next in thread: Minti: "Re: C 99 compiler access"
- Reply: Minti: "Re: C 99 compiler access"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|