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,4873305131bf4d94 X-Google-Attributes: gid103376,public X-Google-Thread: 1014db,4873305131bf4d94 X-Google-Attributes: gid1014db,public From: emcmanus@gr.opengroup.org (�amonn McManus) Subject: Mixing declarations and statements (Re: Porting (was ADA and Pascal etc)) Date: 1997/11/04 Message-ID: #1/1 X-Deja-AN: 287014371 References: <34557f2b.1934172@news.mindspring.com> <345AB871.413A@dynamite.com.au> <63d5l4$tub$1@helios.crest.nt.com> <878495810snz@genesis.demon.co.uk> Organization: Open Group Research Institute, Grenoble, France Newsgroups: comp.lang.ada,comp.lang.c Date: 1997-11-04T00:00:00+00:00 List-Id: fred@genesis.demon.co.uk writes: > kaz@helios.crest.nt.com "Kaz Kylheku" writes: > >What's worse is that mixed declarations and statements are likely going to be > >part of the new C language. > Actually that finally resolves one of the biggest bugbears I have when > writing C. My experience has shown clearly that localising declarations > can be a big win for readability. Currently I have to use extra { }'s to do > that in some cases (notably for switch cases). With C9X I will finally be > able to write code in a natural way according to my preferred coding style. I think switch cases are a particularly bad example of where this feature is advantageous. Where in current C you write either: int x; switch (thing) { case one: x = whatever(); use(x); break; case two: x = maybe(); use(x); break; } or switch (thing) { case one: { int x = whatever(); use(x); break; } case two: { int x = maybe(); use(x); break; } } with the exciting new feature you can write: switch (thing) { case one: int x = whatever(); use(x); break; case two: int x = maybe(); use(x); break; } Except I very much hope that you can't, since that would be a multiple declaration of x. Which x would be referred to in common code that you arrived at via goto? Presumably the new C will allow the first declaration but require the `int' to be left off the second, turning it back into a lowly statement. That's terrible programming practice of course because you're having the meaning of one switch case depend on the way another one was written. If you start introducing declarations into your switch cases you're going to run into this sort of problem all the time. I did in Java before I stopped doing it; in fact I find that in Java declarations tend to migrate up towards the start of the method, where they would be in C. Nonetheless the new C feature is easy to implement and understand and is fairly often useful. , Eamonn http://www.gr.opengroup.org/~emcmanus "Then I realized I was in France and, in fact, the gibberish was French" -- R Johnson