Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: What's new in C?

Reply
Thread Tools

Re: What's new in C?

 
 
Ian Collins
Guest
Posts: n/a
 
      12-08-2012
BartC wrote:
> "Jorgen Grahn" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed). ..
>> On Sat, 2012-12-08, BartC wrote:

>
>>> "Jorgen Grahn" <(E-Mail Removed)> wrote in message

>
>>>> Ugh. Yeah, it would be interesting to read Linus' rationale for not
>>>> allowing it.
>>>
>>> Maybe he thought that overall that were more minuses than pluses.
>>>
>>> (I can see so many things wrong with having mixed declarations, that I
>>> wouldn't know where to start. Let's say I can't see many pluses at all!)

>>
>> We are at different ends of the spectrum then. I can think of one
>> semi-reason for banning them ("I want to pretend I have a compre-
>> hensive list of what's on this function's stack, and an idea of how
>> much stack it uses") but that's about it.

>
>> If you really have more arguments, I'm interested and willing to
>> listen.

>
> I've this discussed this here before I think. My arguments weren't received
> well then either!
>
> So, mixed declarations, AIUI, allow you to have unlimited, distinct versions
> of the same identifier within a function. They may or might not have the
> same type.
>
> For example, a 3-deep nested for-loop all using 'i' as the index variable!


You can do that in C89.

> Searching for a declaration for an identifer, instead of looking at the top
> of the function, it means searching up line-by-line counting blocks, since
> it will now be hidden in a mass of code somewhere.


If a function is long enough for this to be a problem, you have bigger
things to worry about than mixed declarations and code.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      12-08-2012
On Sat, 2012-12-08, Malcolm McLean wrote:
> On Saturday, December 8, 2012 3:23:23 PM UTC, Jorgen Grahn wrote:
>> On Sat, 2012-12-08, BartC wrote:
>>
>> We are at different ends of the spectrum then. I can think of one
>> semi-reason for banning them ("I want to pretend I have a compre-
>> hensive list of what's on this function's stack, and an idea of how
>> much stack it uses") but that's about it.
>>
>> (No, actually I've heard another one: "it becomes so painful to
>> maintain for long functions that you're encouraged to split it into
>> smaller functions". I don't buy such arguments.)
>>

> A function is a unit, and variables have meaning within that unit.
>
> If N means "the number of employees", then it makes sense if N means that
> thoughout the whole function. If it means "the bitpattern that represents
> the Latin glyph N", again it should mean that in the whole function.


Sure. I think everyone agrees about that.

> So it's natural to have a glossary at the start of the function.
>
> unsigned char *N; /* bit pattern for N */
> double theta; /* anticlockwise angle */
> int x, y; /* top left Cartesian co-ordinates */


Your example also happens to be a good counterexample.

The documentation for N, theta, x and y is incorrect the point of
declaration. 'theta' might well be an angle further down into the
function, but at this point it's just garbage.

That's why I prefer 'theta' to show up at initialization time:

const double theta = some_trigonometry_stuff(x, y);

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      12-08-2012
"BartC" <(E-Mail Removed)> writes:
> "Jorgen Grahn" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed). ..
>> On Sat, 2012-12-08, BartC wrote:

>
>>> "Jorgen Grahn" <(E-Mail Removed)> wrote in message

>
>>>> Ugh. Yeah, it would be interesting to read Linus' rationale for not
>>>> allowing it.
>>>
>>> Maybe he thought that overall that were more minuses than pluses.
>>>
>>> (I can see so many things wrong with having mixed declarations, that I
>>> wouldn't know where to start. Let's say I can't see many pluses at all!)

>>
>> We are at different ends of the spectrum then. I can think of one
>> semi-reason for banning them ("I want to pretend I have a compre-
>> hensive list of what's on this function's stack, and an idea of how
>> much stack it uses") but that's about it.

>
>> If you really have more arguments, I'm interested and willing to
>> listen.

>
> I've this discussed this here before I think. My arguments weren't received
> well then either!
>
> So, mixed declarations, AIUI, allow you to have unlimited, distinct versions
> of the same identifier within a function. They may or might not have the
> same type.


No, they don't.

> For example, a 3-deep nested for-loop all using 'i' as the index variable!


You've been able to have declarations with the same name *in nested
blocks* at least since C89, and I think K&R C allowed it too:

void func(void) {
int n;
{
double n;
}
}

What C99 added (borrowed from C++) was the ability to mix declarations
and statements in the same block, (whether that block happens to be the
outermost block of a function or not) -- but you still can't have
declarations with duplicate names in the same scope:

void func(void) {
int a;
statement();
int b; /* Illegal in C89/C90, legal in C99 */
double a; /* Illegal in all versions of C */
}

[...]

>> The argument *for* allowing it is simply:
>>
>> I can declare the variable exactly at the point where it's needed,
>> meaning I can give it a guaranteed sane initial value, and frequently
>> make it const.

>
> Yes, it makes it easy to write. But harder to read and to maintain. Maybe I
> should just have said that...


I agree it's easier to write; I don't agree that it makes it harder to
read or maintain:

void func(void) {
int x = something;
/* code that does some calculations affecting x */
const int y = value_that_depends_on(x);
/* code that uses y */
}

As Jorgen says, it means you can initialize y based on information
that's not available at the top of the function.

This is just an easier way to do what you could already do without this
feature:

void func(void) {
int x = something;
/* code that does some calculations affecting x */
{
const int y = value_that_depends_on(x);
/* code that uses y */
}
}

I *think* that any code that depends on mixed declarations and
statements can be transformed to code without mixed declarations and
statements, by adding opening and closing braces. (I could easily
be mistaken on that point; does anyone have a counterexample?)
The problem is that it can result in deeply nested blocks.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      12-08-2012
On Sat, 2012-12-08, BartC wrote:
> "Jorgen Grahn" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed). ..
>> On Sat, 2012-12-08, BartC wrote:

>
>>> "Jorgen Grahn" <(E-Mail Removed)> wrote in message

>
>>>> Ugh. Yeah, it would be interesting to read Linus' rationale for not
>>>> allowing it.
>>>
>>> Maybe he thought that overall that were more minuses than pluses.
>>>
>>> (I can see so many things wrong with having mixed declarations, that I
>>> wouldn't know where to start. Let's say I can't see many pluses at all!)

>>
>> We are at different ends of the spectrum then. I can think of one
>> semi-reason for banning them ("I want to pretend I have a compre-
>> hensive list of what's on this function's stack, and an idea of how
>> much stack it uses") but that's about it.

>
>> If you really have more arguments, I'm interested and willing to
>> listen.

>
> I've this discussed this here before I think. My arguments weren't received
> well then either!


Why the "either"? I have so far just requested to hear them.

> So, mixed declarations, AIUI, allow you to have unlimited, distinct versions
> of the same identifier within a function. They may or might not have the
> same type.
>
> For example, a 3-deep nested for-loop all using 'i' as the index variable!


My reaction to that is:
(a) Why would I do something crazy like that?
(b) What does that have to do with mixed declarations?
It seems to me I can do that just as well (or badly) in C90:

int foo(void) {
for(int i=0; i<4; i++)
for(int i=0; i<5; i++) ;
return i;
}

int foo(void) {
int i;
for(i=0; i<4; i++) {
int i;
for(i=0; i<5; i++) ;
}
return i;
}

> Searching for a declaration for an identifer, instead of looking at the top
> of the function, it means searching up line-by-line counting blocks, since
> it will now be hidden in a mass of code somewhere.


OK, I see now we are talking about different things. I was talking
about the C99 feature disliked by Linus Torvalds, but you also don't
like the K&R feature which lets you declare variables at the beginning
of a block. To avoid confusion, I will not enter that second
discussion.

[snip]

> From an implementation point of view, it creates extra difficulties. Instead
> of {...} simply meaning you have 2 statements instead of 1 in the that
> if-else branch, you now have to take care of allocating and deallocating any
> variables in that block, not only at the end, but in case of a goto or break
> too.


It's a bit of a moot point though, isn't it? C compilers have AFAIK
handled this since the early 1970s.

>> The argument *for* allowing it is simply:
>>
>> I can declare the variable exactly at the point where it's needed,
>> meaning I can give it a guaranteed sane initial value, and frequently
>> make it const.

>
> Yes, it makes it easy to write. But harder to read and to maintain.


That's the exact opposite of what I meant, though. I've done a lot of
maintenance in my days, and I believe people who value ease of writing
over ease of maintenance should simply be fired.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      12-08-2012
"Ian Collins" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> BartC wrote:


>> For example, a 3-deep nested for-loop all using 'i' as the index
>> variable!

>
> You can do that in C89.


Do you have an example? That is meaningful I mean.

Although nothing is stopping you anyway from using the same index variable,
because they are all the same variable, it's going to cause difficulties, so
it doesn't happen.

But with mixed declarations it will work, because all the 'i's are distinct.
However, it's just confusing

(What *is* a good idea, I think, is to just auto-declare for-loop variables
anyway. Then the question of where to declare them is eliminated!)

> If a function is long enough for this to be a problem, you have bigger
> things to worry about than mixed declarations and code.


And if a function is short, there's less to gain from localising all the
declarations.

--
Bartc

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      12-08-2012
BartC wrote:
> "Ian Collins" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> BartC wrote:

>
>>> For example, a 3-deep nested for-loop all using 'i' as the index
>>> variable!

>>
>> You can do that in C89.

>
> Do you have an example? That is meaningful I mean.


int main(void) {
int i;

for( i = 0; i < 10; ++i ) {
int i;
for( i = 0; i < 10; ++i ) {
}
}
}

> Although nothing is stopping you anyway from using the same index variable,
> because they are all the same variable, it's going to cause difficulties, so
> it doesn't happen.
>
> But with mixed declarations it will work, because all the 'i's are distinct.
> However, it's just confusing
>
> (What *is* a good idea, I think, is to just auto-declare for-loop variables
> anyway. Then the question of where to declare them is eliminated!)


Isn't that what C99 added?

>> If a function is long enough for this to be a problem, you have bigger
>> things to worry about than mixed declarations and code.

>
> And if a function is short, there's less to gain from localising all the
> declarations.


My point was that one of the common reasons for not liking mixed
declarations and code is it doesn't sit well with badly written code.

--
Ian Collins
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      12-08-2012
"Jorgen Grahn" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed). ..
> On Sat, 2012-12-08, BartC wrote:


>> I've this discussed this here before I think. My arguments weren't
>> received
>> well then either!

>
> Why the "either"? I have so far just requested to hear them.


Sorry. I assumed the same people who argued for the feature then,
would still have the same views.

>> Searching for a declaration for an identifer, instead of looking at the
>> top
>> of the function, it means searching up line-by-line counting blocks,
>> since
>> it will now be hidden in a mass of code somewhere.

>
> OK, I see now we are talking about different things. I was talking
> about the C99 feature disliked by Linus Torvalds, but you also don't
> like the K&R feature which lets you declare variables at the beginning
> of a block. To avoid confusion, I will not enter that second
> discussion.


You mean, declarations don't even need to be at the start of a block now?

Then it's worse than I thought!

In that case most of my arguments apply doubly.

>> From an implementation point of view, it creates extra difficulties.


> It's a bit of a moot point though, isn't it? C compilers have AFAIK
> handled this since the early 1970s.


Maybe. It was still unnecessary in my opinion. 99% of time you're using
begin..end, {...} or whatever, simply because there is more than one
statement, nothing more.

--
Bartc
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-08-2012
"BartC" <(E-Mail Removed)> writes:
> "Ian Collins" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> BartC wrote:
>>> For example, a 3-deep nested for-loop all using 'i' as the index
>>> variable!

>>
>> You can do that in C89.

>
> Do you have an example? That is meaningful I mean.
>
> Although nothing is stopping you anyway from using the same index variable,
> because they are all the same variable, it's going to cause difficulties, so
> it doesn't happen.


What do you mean by "they are all the same variable"?

Using C99 syntax:

for (int i = 0; i < 100; i ++) {
for (int i = 0; i < 100; i ++) {
// ...
}
}

The outer and inner "i" are two distinct objects (that happen to
have the same name). If you say they're the same variable, then
you're using a very different definition of "variable" than I do.

> But with mixed declarations it will work, because all the 'i's are distinct.
> However, it's just confusing
>
> (What *is* a good idea, I think, is to just auto-declare for-loop variables
> anyway. Then the question of where to declare them is eliminated!)


By "auto-declare", are you referring to the new-in-C99 feature I just
demonstrated? I wouldn't call that "auto-declaring" (unless you're
referring to automatic storage duration, but I don't think that's what
you mean). You still need a declaration; the language just permits it
to be in the header of the for-loop.

Note that if the body of the loop doesn't need to refer to the variable,
once could argue that using the same name for both loop variables is
sensible. (I'd still want to use distinct names.)

Some languages do provide a syntax to execute a loop N times without
creating a variable that gives you the current iteration number. C's
for-loop provides capabilities far beyond a simple counted loop, so it
wouldn't make too much sense to add such a special case. I suppose the
language could have had something like:

repeat(100) {
/* ... */
}

but that's probably not sufficiently useful to make it a language
feature.

>> If a function is long enough for this to be a problem, you have bigger
>> things to worry about than mixed declarations and code.

>
> And if a function is short, there's less to gain from localising all the
> declarations.


What you gain is the ability to initialize a variable using information
that's not available at the top of the function (or block). Do you
really not find that useful?

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      12-08-2012


"Ian Collins" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> BartC wrote:
>> "Ian Collins" <(E-Mail Removed)> wrote in message


>>> You can do that in C89.

>>
>> Do you have an example? That is meaningful I mean.

>
> int main(void) {
> int i;
>
> for( i = 0; i < 10; ++i ) {
> int i;
> for( i = 0; i < 10; ++i ) {
> }
> }
> }
>
>> Although nothing is stopping you anyway from using the same index
>> variable,
>> because they are all the same variable, it's going to cause difficulties,
>> so
>> it doesn't happen.
>>
>> But with mixed declarations it will work, because all the 'i's are
>> distinct.
>> However, it's just confusing
>>
>> (What *is* a good idea, I think, is to just auto-declare for-loop
>> variables
>> anyway. Then the question of where to declare them is eliminated!)

>
> Isn't that what C99 added?


It seems I misunderstood was being discussed. I thought it was declarations
at the top vs. block declarations. Not block declarations vs. genuinely
anywhere.

Which I consider even more wrong.

If, for example, you have to fill in the declarations in a newly written
functions (if like me you leave such matters until the code is at a first
draft); it means searching backwards for the first occurrence of each name,
and paying attention to block structure, instead of just sticking them all
at the top where they belong. And where they don't spoil the lines of the
function with all these incongruous declarations.

Then you have to edit the code and have to keep moving the declarations
around ...

But I suppose it's people's choice if they want to make life more difficult.

--
Bartc

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      12-08-2012
BartC wrote:
>
> It seems I misunderstood was being discussed. I thought it was declarations
> at the top vs. block declarations. Not block declarations vs. genuinely
> anywhere.
>
> Which I consider even more wrong.
>
> If, for example, you have to fill in the declarations in a newly written
> functions (if like me you leave such matters until the code is at a first
> draft); it means searching backwards for the first occurrence of each name,
> and paying attention to block structure, instead of just sticking them all
> at the top where they belong. And where they don't spoil the lines of the
> function with all these incongruous declarations.
>
> Then you have to edit the code and have to keep moving the declarations
> around ...


Which yet again emphasises my earlier point (that you conveniently
snipped): one of the common reasons for not liking mixed declarations
and code is it doesn't sit well with badly written code.

--
Ian Collins
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript new-new-new-new-newbee weblinkunlimited@gmail.com Javascript 2 03-11-2008 01:15 AM
New computer, New OS, New Wireless Problem :-\ =?Utf-8?B?RGFu?= Wireless Networking 3 07-31-2005 02:11 PM
[Firefox] Use New Tab instead of New Window? paul j Firefox 7 04-07-2005 09:40 PM
Why can not register a new .net passport or a new hotmail account Alick Lv MCSD 1 01-04-2004 06:12 PM



Advertisments