Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Always draw your curves, then plot your reading.


devel / comp.lang.c / Re: Do you insist on const-correctness?

SubjectAuthor
* Do you insist on const-correctness?Anton Shepelev
+- Re: Do you insist on const-correctness?Anton Shepelev
+* Re: Do you insist on const-correctness?Ben Bacarisse
|`* Re: Do you insist on const-correctness?Anton Shepelev
| +* Re: Do you insist on const-correctness?David Brown
| |`* Re: Do you insist on const-correctness?David Brown
| | `* Re: Do you insist on const-correctness?comp.lang.c
| |  `* Re: Do you insist on const-correctness?David Brown
| |   +* Re: Do you insist on const-correctness?Keith Thompson
| |   |`- Re: Do you insist on const-correctness?Chris M. Thomasson
| |   +- Re: Do you insist on const-correctness?Scott Lurndal
| |   +* Re: Do you insist on const-correctness?David Brown
| |   |`- Re: Do you insist on const-correctness?Chris M. Thomasson
| |   `- Re: Do you insist on const-correctness?Keith Thompson
| `* Re: Do you insist on const-correctness?Ben Bacarisse
|  `- Re: Do you insist on const-correctness?Anton Shepelev
+* Re: Do you insist on const-correctness?Tim Rentsch
|`* Re: Do you insist on const-correctness?Anton Shepelev
| +* Re: Do you insist on const-correctness?David Brown
| |+* Re: Do you insist on const-correctness?Scott Lurndal
| ||+* Re: Do you insist on const-correctness?Kaz Kylheku
| |||`* Re: Do you insist on const-correctness?Bart
| ||| +* Re: Do you insist on const-correctness?Kaz Kylheku
| ||| |`* Re: Do you insist on const-correctness?Bart
| ||| | +- Re: Do you insist on const-correctness?Chris M. Thomasson
| ||| | +- Re: Do you insist on const-correctness?Kaz Kylheku
| ||| | +- Re: Do you insist on const-correctness?Keith Thompson
| ||| | `- Re: Do you insist on const-correctness?David Brown
| ||| +* Re: Do you insist on const-correctness?David Brown
| ||| |`* Re: Do you insist on const-correctness?Bart
| ||| | +- Re: Do you insist on const-correctness?Ben Bacarisse
| ||| | +- Re: Do you insist on const-correctness?Keith Thompson
| ||| | +* Re: Do you insist on const-correctness?Kaz Kylheku
| ||| | |`* Re: Do you insist on const-correctness?Chris M. Thomasson
| ||| | | `* Re: Do you insist on const-correctness?Keith Thompson
| ||| | |  `* Re: Do you insist on const-correctness?Chris M. Thomasson
| ||| | |   `* Re: Do you insist on const-correctness?Keith Thompson
| ||| | |    `- Re: Do you insist on const-correctness?Chris M. Thomasson
| ||| | `* Re: Do you insist on const-correctness?David Brown
| ||| |  `* Re: Do you insist on const-correctness?Bart
| ||| |   `* Re: Do you insist on const-correctness?David Brown
| ||| |    `* Re: Do you insist on const-correctness?Bart
| ||| |     +- Re: Do you insist on const-correctness?Keith Thompson
| ||| |     +- Re: Do you insist on const-correctness?Kaz Kylheku
| ||| |     `- Re: Do you insist on const-correctness?David Brown
| ||| +- Re: Do you insist on const-correctness?Keith Thompson
| ||| `* Re: Do you insist on const-correctness?James Kuyper
| |||  `* Re: Do you insist on const-correctness?Bart
| |||   `* Re: Do you insist on const-correctness?David Brown
| |||    `* Re: Do you insist on const-correctness?Kaz Kylheku
| |||     `- Re: Do you insist on const-correctness?Chris M. Thomasson
| ||`* Re: Do you insist on const-correctness?David Brown
| || `* Re: Do you insist on const-correctness?Chris M. Thomasson
| ||  `* Re: Do you insist on const-correctness?Chris M. Thomasson
| ||   `* Re: Do you insist on const-correctness?Ben Bacarisse
| ||    `* Re: Do you insist on const-correctness?Chris M. Thomasson
| ||     `* Re: Do you insist on const-correctness?Ben Bacarisse
| ||      `- Re: Do you insist on const-correctness?Chris M. Thomasson
| |+* Re: Do you insist on const-correctness?Kaz Kylheku
| ||`* Re: Do you insist on const-correctness?David Brown
| || `* Re: Do you insist on const-correctness?Anton Shepelev
| ||  `* Re: Do you insist on const-correctness?David Brown
| ||   `* Re: Do you insist on const-correctness?Anton Shepelev
| ||    `- Re: Do you insist on const-correctness?David Brown
| |`* Re: Do you insist on const-correctness?Anton Shepelev
| | +- Re: Do you insist on const-correctness?Bart
| | +- Re: Do you insist on const-correctness?Richard Damon
| | +* Re: Do you insist on const-correctness?Scott Lurndal
| | |`- Re: Do you insist on const-correctness?Anton Shepelev
| | `* Re: Do you insist on const-correctness?David Brown
| |  +* Re: Do you insist on const-correctness?Bart
| |  |`* Re: Do you insist on const-correctness?David Brown
| |  | `* Re: Do you insist on const-correctness?Bart
| |  |  +* Re: Do you insist on const-correctness?Chris M. Thomasson
| |  |  |`* Re: Do you insist on const-correctness?Bart
| |  |  | +* Re: Do you insist on const-correctness?Chris M. Thomasson
| |  |  | |`- Re: Do you insist on const-correctness?Bart
| |  |  | +* Re: Do you insist on const-correctness?David Brown
| |  |  | |`* Re: Do you insist on const-correctness?Chris M. Thomasson
| |  |  | | +- Re: Do you insist on const-correctness?Kaz Kylheku
| |  |  | | `* Re: Do you insist on const-correctness?David Brown
| |  |  | |  `* Re: Do you insist on const-correctness?Chris M. Thomasson
| |  |  | |   `- Re: Do you insist on const-correctness?Chris M. Thomasson
| |  |  | `* Re: Do you insist on const-correctness?Anton Shepelev
| |  |  |  +* Re: Do you insist on const-correctness?Anton Shepelev
| |  |  |  |`- Re: Do you insist on const-correctness?David Brown
| |  |  |  +- Re: Do you insist on const-correctness?Bart
| |  |  |  +* Re: Do you insist on const-correctness?Dan Cross
| |  |  |  |`* Re: Do you insist on const-correctness?Anton Shepelev
| |  |  |  | `* Re: Do you insist on const-correctness?Dan Cross
| |  |  |  |  `* Re: Do you insist on const-correctness?Scott Lurndal
| |  |  |  |   `- Re: Do you insist on const-correctness?Dan Cross
| |  |  |  `* Re: Do you insist on const-correctness?David Brown
| |  |  |   +* Re: Do you insist on const-correctness?Bart
| |  |  |   |+* Re: Do you insist on const-correctness?Kaz Kylheku
| |  |  |   ||+* Re: Do you insist on const-correctness?Bart
| |  |  |   |||`* Re: Do you insist on const-correctness?David Brown
| |  |  |   ||| +- Re: Do you insist on const-correctness?Anton Shepelev
| |  |  |   ||| `* Re: Do you insist on const-correctness?Bart
| |  |  |   |||  `* Re: Do you insist on const-correctness?David Brown
| |  |  |   |||   `* Re: Do you insist on const-correctness?Ben Bacarisse
| |  |  |   ||`* Re: Do you insist on const-correctness?David Brown
| |  |  |   |`- Re: Do you insist on const-correctness?David Brown
| |  |  |   +* Re: Do you insist on const-correctness?Anton Shepelev
| |  |  |   +* Re: Do you insist on const-correctness?David Brown
| |  |  |   `- Re: Do you insist on const-correctness?Chris M. Thomasson
| |  |  `- Re: Do you insist on const-correctness?David Brown
| |  `* Re: Do you insist on const-correctness?Anton Shepelev
| `* Re: Do you insist on const-correctness?Tim Rentsch
+* Re: Do you insist on const-correctness?Kaz Kylheku
`- Re: Do you insist on const-correctness?Tim Rentsch

Pages:12345678
Re: Do you insist on const-correctness?

<uev5c1$73g$1@reader2.panix.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29304&group=comp.lang.c#29304

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!panix!.POSTED.spitfire.i.gajendra.net!not-for-mail
From: cro...@spitfire.i.gajendra.net (Dan Cross)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Tue, 26 Sep 2023 17:45:37 -0000 (UTC)
Organization: PANIX Public Access Internet and UNIX, NYC
Message-ID: <uev5c1$73g$1@reader2.panix.com>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc> <20230926171422.fbc681174edadb9d385a9c0f@gmail.moc> <ueupor$kiu$1@reader2.panix.com> <yGCQM.35598$ugs.11802@fx36.iad>
Injection-Date: Tue, 26 Sep 2023 17:45:37 -0000 (UTC)
Injection-Info: reader2.panix.com; posting-host="spitfire.i.gajendra.net:166.84.136.80";
logging-data="7280"; mail-complaints-to="abuse@panix.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: cross@spitfire.i.gajendra.net (Dan Cross)
 by: Dan Cross - Tue, 26 Sep 2023 17:45 UTC

In article <yGCQM.35598$ugs.11802@fx36.iad>,
Scott Lurndal <slp53@pacbell.net> wrote:
>cross@spitfire.i.gajendra.net (Dan Cross) writes:
>>In article <20230926171422.fbc681174edadb9d385a9c0f@gmail.moc>,
>>Anton Shepelev <anton.txt@gmail.moc> wrote:
>>>Dan Cross:
>>>
>>>> Robert C Martin is, at best, a middling-quality
>>>> programmer.
>>>
>>>Maybe he is, but how do you come to that conclusion?
>>
>>By examining his publicly available work, as well as his
>>writing.
>>
>>"Clean Code" in particular is rife with spelling and grammar
>>errors, and one can look at his code on Github. See, for
>>example, this:
>>https://github.com/unclebob/PDP8EmulatorIpad/pull/2
>>(Here's the original source program:
>>https://github.com/unclebob/PDP8EmulatorIpad/blob/1eba53c08fb530effb9d29aca8134f7acadfdd5f/src/topt.c
>
>What's with the screwy indentation? That's a 'do not hire this guy' flag.
>
>Waste of time to bzero the string before constructing it using strcpy,
>rather than just using snprintf().
>
>Ah, I see your point is that he writes poor code.

Indeed! Strangely kind par for the course on people who have
strongly held opinions on programming.

- Dan C.

Re: Do you insist on const-correctness?

<uev5e6$2k5cd$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29305&group=comp.lang.c#29305

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Tue, 26 Sep 2023 18:46:47 +0100
Organization: A noiseless patient Spider
Lines: 65
Message-ID: <uev5e6$2k5cd$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me> <ueuv1r$2iped$1@dont-email.me>
<20230926093309.110@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 26 Sep 2023 17:46:46 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="3a983e4fb81f333ec35c6d9e8f18abd4";
logging-data="2758029"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+EC3Gndwzi7PoKKWJbb/3LSSS3ykFKXZw="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:nFqnjFRlRSe0ZxALvTFMyfIKFeA=
In-Reply-To: <20230926093309.110@kylheku.com>
 by: Bart - Tue, 26 Sep 2023 17:46 UTC

On 26/09/2023 17:40, Kaz Kylheku wrote:
> On 2023-09-26, Bart <bc@freeuk.com> wrote:
>> On 26/09/2023 16:10, David Brown wrote:

>> What is the actual rule about braces? Because you've clearly missed some
>> out there, in omitting them around the first two 'else' branches.
>
> The actual rules is that the required braces, comprising a compound
> statement, can be prefixed by something which modifies the statement
> into a conditional.

But then that compound statement becomes a single statement with no
braces of its own, and many of the problems associated with optional
braces return.

>> If you saying this is OK, then that's playing fast and rule with any
>> such rule that braces must always be used even around one statement.
>
> Indeed, I myself sometimes writes in a style like the following:
>
> if (condition) {
> /* loop not needed */
> } else for (...) {
>
> }
>
> The else keyword needs a statement, and the for statement
> satisfies it. It has braces of its own, so everything is cool.

Suppose you were to write it like this:

} else
for (...) {

then someone adds an extra statement:

} else
puts("Loop follows.");
for (...) {

Now it's not so cool.

Really, it's quite silly: how far deep inside a series of /single/
nested statements are you allowed to go, to enable you to claim that
your 'else' branch is actually brace-delimited?

> What I wouldn't write is:
>
>
> if (condition) {
> /* loop not needed */
> } else

Why wouldn't you write this? Presumably something might be added there
in future, otherwise it's a poorly thought-out condition.

> Some languages combine "else" and "if" into a single token, even.
> The C preprocessing language is like that: #elif.

This has always puzzled me, where C's preprocessor has a more grown-up,
more solid , more readable and less error prone syntax than the main
language. No dangling 'else' there!

Re: Do you insist on const-correctness?

<20230926205357.930d996e817bd458e0ff656b@gmail.moc>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29306&group=comp.lang.c#29306

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton....@gmail.moc (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Tue, 26 Sep 2023 20:53:57 +0300
Organization: A noiseless patient Spider
Lines: 211
Message-ID: <20230926205357.930d996e817bd458e0ff656b@gmail.moc>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me>
<ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me>
<ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me>
<ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: dont-email.me; posting-host="25ca480c1321b8ab0a98e8bba2150e80";
logging-data="2760854"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+L5P3RrLK0m7h+GQ5o8SlETMOABCsz/4A="
Cancel-Lock: sha1:4Mbe0e1gaI+EmyWfoftaeS33W6Y=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Tue, 26 Sep 2023 17:53 UTC

David Brown to Anton Shepelev:

> > Modern languages with multiple returns or tuples provide
> > a partial solution, another being to unify error info
> > with the result of successful execution in dynamic
> > languages or ones with templates (to save the declaring
> > a combined TResult type for every function). But then,
> > I loose the whole pupose of an error flag -- the ability
> > to invoke funtions inside an `if' statement.
>
> You realise that in C you can return structs, and thus
> include status returns as well as value returns in one
> item?

I do, which is why I wrote about defining a
<func_name>TResult type for /every/ function. If, on the
other hand, I define TResult to contain only error
information and the success flag, I save a single parameter
compared to returning the success flag and error info
separately.

> Still, why are you talking about modern languages and
> object oriented programming when you insist on using a
> language outdated a generation ago?

Because C lacks those new features and I am spared the
pressure to use them in my code :-) Seriously, I see nothing
bad in considering alternative approaches to get out of the
box, so to say... I do not think C is outdated a generation
ago, and even the new standards have far from changed it
beyond recognition. It is basically the same language.

> > Linus 8-chareater-tab Torvalds was IMHO right when he
> > decreed that a function shall not have more then three
> > levels of nesting, his enourmous tab serving as a dumb,
> > primitive, mechanical, and ugly means of enforsing this
> > decree.
>
> No, Linus was wrong - as he is on many things. (He's
> right on many things too, of course.) We are not using
> typewriters, we are not using 80x25 character displays,
> and we do not need to have pointless limitations.

They are not pointless. 80 characters is a generous the
upper limit of a compfortable line width, most typographical
manuals suggesting lower lengths, and I did not refer to the
25-line limit anywhere. My functions are frequently longer.

> I don't advocate for deeply nested or overly complex
> functions, of course - I advocate for clear code with
> immediately obvious blocks indicated by braces /and/
> indents. And if clear code requires more than 3 levels of
> nesting, use that.

In my opinion, up to three levels are easy to perceive, and
then it becomes harder epxonentially: four levels is
tolerable and five is a mess.

> > For an example, and the standard if-else-if-else...
> > chain is in fact a variant of a switch, and a switch has
> > the
>
> Not in C.

Yes, I meant the broader condept of switch as a table of
tests and corresponding statements, with perhaps a fallback
(default) entry. The point is that the sturcture of switch
is a table with two columns (see below).

> > if( test_1 ) {
> > stat_1 } else {
> > if( test_2 ) {
> > stat_2 } else {
> > if( test_3 ) {
> > stat_3 } else {
>
> I have never seen if-else chains formatted that way - it
> is clearly incorrect indentation.

It is technically /the one true/ correct indentation,
because each level of nesting has its own indent.

> if (test1) {
> stat_1;
> } else if (test2) {
> stat_2;
> } else if (test3) {
> stat_3;
> } else {
> ...
> }

Now, you have recoginised that although syntatically it is a
deeply nested unblananced tree, semantically it is a linear
structure, and therefore you flattened it, so that the
nesting level is no longer equal to the indent. Does a dumb
pretty-printer understand semantics? Do coding standards
care about semantics?

> > if( test_1 ) stat_1;
> > else if( test_2 ) stat_2;
> > else if( test_3 ) stat_3;
> >
> > It shorter, clearer, and better structured, because the
> > programmer has formatted the code according its intent.
> > Observe also the indent of the first `if' to have it
> > aligned with the other entries of the table.
>
> That's a strawman argument, because no one would ever
> write an if-else chain the way you first suggested.

No, what /you/ say after `because' is a strawman argument
becase it appeals to external circumstances (the number of
followers) rather than criticises the formatting itself. I
will try to explain my formatting again: the structure of
that control statement is a table with two columns and three
rows. A careful programmer, having respect for the reader,
will therefore take advantage of the 2-dimensional nature of
text and format it in a 2x3 grid, aligning the correspondent
parts together:
test_1 stat_1
test_2 stat_2
test_3 stat_3

It is only meet also to alignt the correspondent keywords,
as I have done above. This is the only way to express the
programmer's intent in the formatting. Curly braces would
be redundant, because the structure is sufficiently
transparent not to require them, but you can add them
anyway:
if( test_1 ) { stat_1 ; }
else if( test_02 ) { stat_02 ; }
else if( test_003 ) { stat_003; }

I have introduced indentifiers of varying length to show the
correct horisontal alignment of closing parentheses, curly
braces, and semicolons.

You say that your version above

> is clearer than yours, and far more maintainable.

I outright deny that it is any clearer. It is certainly
messier and less structured, which any non-programmer, or a
programmer without professional bias due to experience with
curly languages, should confirm. Show them our styles
side-by-side:

Format I | Format II
|
if (test1) { | if( test_1 ) stat_1 ;
stat_1; | else if( test_2 ) stat_2 ;
} else if (test2) { | else if( test_3 ) stat_3 ;
stat_2; | else stat_def;
} else if (test3) { |
stat_3; |
} else { |
stat_def; |
} |

As to maintainability, it depends. My version is suitable
for small, ideally single, statements, whereas yours is more
universal and works better with large, compund ones. In my
own practice, however, I never need an if-else chain with
large statements, for some reason. If I should suddently
need to introduce long statements into my if-else chain, I
will then rewrited it differently, but not before. The
problem of universal one-for-all formatting rules is that
they are not optimised to the situation at hand, cf. my
three versions of an shortened if-else statement, each for
its own purpose.

> And any coding standard with a care for safety, security
> or reliability, (such as MISRA or JSF-AV mentioned in this
> thread) will insist on the braces here.

Why is my version unsafe, insecure, and unreliable? What
kind of error does it invite? My opinion is that, as long
as the grahica tabular alignment is kept in place, human
errors are very unlikely.

> > Coding standards rarery (if ever) take these semantical
> > (intent) and perceptinve (table) considerations into
> > accout, whereas automatic linters or pretty-printers
> > supply the necessary technology to implement this
> > Nazism.
>
> I have no idea what you are trying to say there.

That quotation contain two parts. The first one states that
coding standards require a uniform formatting style based on
the syntax of the language, ignoring the fact that the same
syntax may express different semantics. For example, a
syntatically deeply nested if-else statement may be written
with deep indentaiton and without (as you have done above),
depending on the intent of the programmer it expresses, that
is whether it is sematically deeply nested. The enforcement
of a uniform formatting regardless of intent makes code less
readable.

The second part refers to my previous observation that
coding standards are often enforced by automatic tools,
which (unaware of sematics and the programmer's intent),
format everything accroding to the same mechanical (syntax-
based) rules, effectively destroying any effort of the
programmer to increase readability by emphasizing code
structure via semantic formatting.

--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments

Re: Do you insist on const-correctness?

<uev74t$2kgdi$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29307&group=comp.lang.c#29307

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Tue, 26 Sep 2023 19:15:58 +0100
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <uev74t$2kgdi$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me>
<20230926205357.930d996e817bd458e0ff656b@gmail.moc>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 26 Sep 2023 18:15:57 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="3a983e4fb81f333ec35c6d9e8f18abd4";
logging-data="2769330"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/q7V95yBJKQ7UTNpurB+j8fs1gUrRBDkc="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:F3bf4dZlw9t+PFE716YKFAxkvDY=
In-Reply-To: <20230926205357.930d996e817bd458e0ff656b@gmail.moc>
 by: Bart - Tue, 26 Sep 2023 18:15 UTC

On 26/09/2023 18:53, Anton Shepelev wrote:
> David Brown to Anton Shepelev:

>> if (test1) {
>> stat_1;
>> } else if (test2) {
>> stat_2;
>> } else if (test3) {
>> stat_3;
>> } else {
>> ...
>> }
>
> Now, you have recoginised that although syntatically it is a
> deeply nested unblananced tree, semantically it is a linear
> structure, and therefore you flattened it, so that the
> nesting level is no longer equal to the indent. Does a dumb
> pretty-printer understand semantics? Do coding standards
> care about semantics?

If I take that example, with an extra branch:

if (test1) {
stat_1;
} else if (test2) {
stat_2;
} else if (test3) {
stat_3;
} else if (test4) {
stat_4;
} else {
stat_5;
}

and pass it through a visualisation tool that turns it into my syntax, I
get this:

if test1 then
stat_1
else
if test2 then
stat_2
else
if test3 then
stat_3
else
if test4 then
stat_4
else
stat_5
fi
fi
fi
fi

The indent level expands sideways as the length of the chain increases.
(Actully I can see the same thing when looking at the generated AST.)

I need a way to detect whether an if-else-if chain was intended, because
that is not the natural form of such a sequence in C. If successul, that
output would instead be:

if test1 then
stat_1
elsif test2 then
stat_2
elsif test3 then
stat_3
elsif test4 then
stat_4
else
stat_5
fi

But I'm not sure that can be done in foolproof manner. Maybe the intent
of the original C /was/ a nested structure with increasing indents.

Re: Do you insist on const-correctness?

<uevkg9$2mqsq$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29309&group=comp.lang.c#29309

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Tue, 26 Sep 2023 15:03:52 -0700
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <uevkg9$2mqsq$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<uesn2u$22nob$1@dont-email.me> <uesnro$22s1l$1@dont-email.me>
<uetv8m$2cqip$1@dont-email.me> <ueu0sh$2d5c9$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 26 Sep 2023 22:03:53 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="33a7a7a3c13bfe90a2f3511b20400978";
logging-data="2845594"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/VMoZI0R0Kpz9rgc7u+W9iRTw2IdVuIPo="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:huBQuY8uEtSWzMZoRAO4/K09zG8=
In-Reply-To: <ueu0sh$2d5c9$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Tue, 26 Sep 2023 22:03 UTC

On 9/26/2023 12:22 AM, Chris M. Thomasson wrote:
> On 9/25/2023 11:55 PM, David Brown wrote:
>> On 25/09/2023 21:42, Chris M. Thomasson wrote:
>>>
>>> I must of remembered it wrong David. I think I remember something
>>> about trying not to have too many functions in a class...
>>
>> That is not in MISRA either, nor in the JSF rules you linked.  (The
>> JSF rules take a lot of inspiration from MISRA, and are IMHO a better
>> set of rules.)
>>
>> I don't know of any coding standards that have fixed maximum limits to
>> the size of functions or the number of functions in a file.  That
>> makes sense, since there is no way to pick an absolute limit - it
>> varies too much by circumstance.  The best you can have is a
>> generalisation, not a rule - do not make your source files so big or
>> have so many functions that they are difficult to navigate.
>>
>>
>
> Yeah. I was misremembering section 3.2:
> ________________________________
> 3.2 Code Size and Complexity
> AV Rule 1
> Any one function (or method) will contain no more than 200 logical
> source lines of code (LSLOCs).
> Rationale: Long functions tend to be complex and therefore difficult to
> comprehend and test.

For some reason the rationale reminded me of you and Kaz wrt being a
good programmer. :^)

> Note: Section 4.2.1 defines should and shall rules as well the
> conditions under which
> deviations from should or shall rules are allowed.
> AV Rule 2
> There shall not be any self-modifying code.
> Rationale: Self-modifying code is error-prone as well as difficult to
> read, test, and maintain.
> AV Rule 3
> All functions shall have a cyclomatic complexity number of 20 or less.
> Rationale: Limit function complexity. See AV Rule 3 in Appendix A for
> additional details.
> Exception: A function containing a switch statement with many case
> labels may exceed this
> limit.
> Note: Section 4.2.1 defines should and shall rules as well the
> conditions under which
> deviations from should or shall rules are allowed.
> ________________________________
>
> And appendix A wrt AV rule 3.

Re: Do you insist on const-correctness?

<20230926122027.184@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29310&group=comp.lang.c#29310

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Tue, 26 Sep 2023 23:05:29 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 74
Message-ID: <20230926122027.184@kylheku.com>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me>
<20230926205357.930d996e817bd458e0ff656b@gmail.moc>
<uev74t$2kgdi$1@dont-email.me>
Injection-Date: Tue, 26 Sep 2023 23:05:29 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c0e01ce6519512ba10a3c818fb613fd9";
logging-data="2868683"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/f+nUaHjoi5c7/uASK4wZfQu7n0XXV62U="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:0Pc3ELNsLZ98kFBGrQ36nmFsVqg=
 by: Kaz Kylheku - Tue, 26 Sep 2023 23:05 UTC

On 2023-09-26, Bart <bc@freeuk.com> wrote:
> and pass it through a visualisation tool that turns it into my syntax, I
> get this:
>
> if test1 then
> stat_1
> else
> if test2 then
> stat_2
> else
> if test3 then
> stat_3
> else
> if test4 then
> stat_4
> else
> stat_5
> fi
> fi
> fi
> fi
>
> The indent level expands sideways as the length of the chain increases.
> (Actully I can see the same thing when looking at the generated AST.)

In Lisp there is no such conundrum.

(if test-1
stat-1
(if test2
stat-2
(if test3
stat-3
(if test4
stat-4
stat-5))))

There is no ladder coding style to make this vertical; any attempt to
make one would be repugnant, due to incorrect indentation:

(if test-1
stat-1
(if test2
stat-2
(if test3
stat-3
(if test4
stat-4
stat-5))))

You'd be constantly fighting with the editor which would be indenting
it.

How you make it vertical is via cond

(cond
(test-1 stat-1)
(test-2 stat-2)
(test-3 stat-3)
(test-4 stat-4)
(t stat-5))

Note that cond came before if. It allowed (cond (x y) (t z)) to be
condensed to (if x y z). Originally, cond clauses were strictly
pairs called "cond pairs". In ANSI Common Lisp they can have one
or more clauses. If the first expression is true, the remaining
expresssions, if any, are evaluated. The value of the last expression
in the clause becomes the result; thus (cond (3)) yields 3.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Do you insist on const-correctness?

<uevtup$2of5s$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29311&group=comp.lang.c#29311

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Wed, 27 Sep 2023 01:45:12 +0100
Organization: A noiseless patient Spider
Lines: 76
Message-ID: <uevtup$2of5s$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me>
<20230926205357.930d996e817bd458e0ff656b@gmail.moc>
<uev74t$2kgdi$1@dont-email.me> <20230926122027.184@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 27 Sep 2023 00:45:13 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="07ea6ddc360482fe4d9e51f9ca54afb9";
logging-data="2899132"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19XBqYxyZ2OvxgT4XxvI4wDMI4JJt20zDw="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:ag37jUvHQweRmlDpitfH12E72Zo=
In-Reply-To: <20230926122027.184@kylheku.com>
 by: Bart - Wed, 27 Sep 2023 00:45 UTC

On 27/09/2023 00:05, Kaz Kylheku wrote:
> On 2023-09-26, Bart <bc@freeuk.com> wrote:
>> and pass it through a visualisation tool that turns it into my syntax, I
>> get this:
>>
>> if test1 then
>> stat_1
>> else
>> if test2 then
>> stat_2
>> else
>> if test3 then
>> stat_3
>> else
>> if test4 then
>> stat_4
>> else
>> stat_5
>> fi
>> fi
>> fi
>> fi
>>
>> The indent level expands sideways as the length of the chain increases.
>> (Actully I can see the same thing when looking at the generated AST.)
>
> In Lisp there is no such conundrum.
>
> (if test-1
> stat-1
> (if test2
> stat-2
> (if test3
> stat-3
> (if test4
> stat-4
> stat-5))))
>
> There is no ladder coding style to make this vertical; any attempt to
> make one would be repugnant, due to incorrect indentation:
>
> (if test-1
> stat-1
> (if test2
> stat-2
> (if test3
> stat-3
> (if test4
> stat-4
> stat-5))))
>
> You'd be constantly fighting with the editor which would be indenting
> it.
>
> How you make it vertical is via cond
>
> (cond
> (test-1 stat-1)
> (test-2 stat-2)
> (test-3 stat-3)
> (test-4 stat-4)
> (t stat-5))

Yes this looks like the 'case-when' statement I posted a day or two ago.
If I rename 'case' to 'cond', and 'else' to 't', then it's almost the
same other than the style of syntax.

But this is yet another solution that doesn't exist in the main C language.

I wonder how big a deal it would have been to introduce an actual
'else-if' token? I expect it would look something like _Elseif; just
using a the hypenated 'else-if' would be better!

Re: Do you insist on const-correctness?

<87y1gss5dq.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29312&group=comp.lang.c#29312

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Tue, 26 Sep 2023 19:43:13 -0700
Organization: None to speak of
Lines: 42
Message-ID: <87y1gss5dq.fsf@nosuchdomain.example.com>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me>
<20230926205357.930d996e817bd458e0ff656b@gmail.moc>
<uev74t$2kgdi$1@dont-email.me> <20230926122027.184@kylheku.com>
<uevtup$2of5s$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="08e30d084ec8438dcadf9cbd76f2c53f";
logging-data="3057137"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+IoFabvI4D9V56BHdhxHlS"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:hib5x8/KLUvrU6wbYeQYC6lGCTc=
sha1:vpyld27pFHv/V26jHPv7pXcEVVY=
 by: Keith Thompson - Wed, 27 Sep 2023 02:43 UTC

Bart <bc@freeuk.com> writes:
[...]
> I wonder how big a deal it would have been to introduce an actual
> 'else-if' token? I expect it would look something like _Elseif; just
> using a the hypenated 'else-if' would be better!

A hyphenated keyword?

In various languages, I've seen "elif", "elsif", and "elseif".
I'd be fine with any of those.

But though I think it would have been nice to add such a keyword from
the beginning, I'm not convinced it would be worth doing now. It's
already perfectly possible to write long if/else chains in C:

if (...) {
...
}
else if (...) {
...
}
else if (...) {
...
}
else {
...
}

(I know some people prefer to put the else on the same line as the {.
That's not really the point.)

Even though the indentation doesn't strictly reflect the grammar, it's a
common enough idiom that that doesn't bother me.

Adding it as _Elseif would IMHO be too ugly. Adding a new keyword
always risks breaking existing code, but C23 is already adding bool,
false, and true.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Re: Do you insist on const-correctness?

<20230926200352.419@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29314&group=comp.lang.c#29314

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Wed, 27 Sep 2023 03:17:48 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 58
Message-ID: <20230926200352.419@kylheku.com>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me>
<20230926205357.930d996e817bd458e0ff656b@gmail.moc>
<uev74t$2kgdi$1@dont-email.me> <20230926122027.184@kylheku.com>
<uevtup$2of5s$1@dont-email.me>
Injection-Date: Wed, 27 Sep 2023 03:17:48 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c0e01ce6519512ba10a3c818fb613fd9";
logging-data="3066010"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18LVB3Pjww2P2b9p7YiGSWOitWAxdM4oxY="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:8D+tUs0k15TZc7Y/hjaEGK0sQck=
 by: Kaz Kylheku - Wed, 27 Sep 2023 03:17 UTC

On 2023-09-27, Bart <bc@freeuk.com> wrote:
> On 27/09/2023 00:05, Kaz Kylheku wrote:
>> How you make it vertical is via cond
>>
>> (cond
>> (test-1 stat-1)
>> (test-2 stat-2)
>> (test-3 stat-3)
>> (test-4 stat-4)
>> (t stat-5))
>
>
> Yes this looks like the 'case-when' statement I posted a day or two ago.
> If I rename 'case' to 'cond', and 'else' to 't', then it's almost the
> same other than the style of syntax.
>
> But this is yet another solution that doesn't exist in the main C language.

Yes; the multi-conditional statement is simulated by nested ifs.

> I wonder how big a deal it would have been to introduce an actual
> 'else-if' token? I expect it would look something like _Elseif; just
> using a the hypenated 'else-if' would be better!

You can get something like it using the preprocessor:

#define elif else if

The paradigm of "elif" is less flexible; since the lone else
combines with statements other than if as I showed in other posts:

if (this) {
x = 42;
} else while (i--) {
foo(j++);
}

A C implementation is free to recognize the abstract syntax tree
pattern:

if (E1) S1
else if (E2) S2
...
else if (EN) SN
else SF

and rewrite it into a single abstract tree node implementing
an equivalent internal case statement. That form could facilitate
certain analyses, like turning case into a switch, because that AST node
would have the clauses as children at the same level. E.g. answering
the question whether all the Ei expressions are constant would be a
simple iteration.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Do you insist on const-correctness?

<20230926201809.34@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29315&group=comp.lang.c#29315

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Wed, 27 Sep 2023 03:21:44 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <20230926201809.34@kylheku.com>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me>
<20230926205357.930d996e817bd458e0ff656b@gmail.moc>
<uev74t$2kgdi$1@dont-email.me> <20230926122027.184@kylheku.com>
<uevtup$2of5s$1@dont-email.me> <87y1gss5dq.fsf@nosuchdomain.example.com>
Injection-Date: Wed, 27 Sep 2023 03:21:44 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c0e01ce6519512ba10a3c818fb613fd9";
logging-data="3066010"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19/PKPRX8Aayz5AtGXqiaKRliwVDHL0zUk="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:UPzhUv0s1LftC4QhNtRGHZ/+Ei0=
 by: Kaz Kylheku - Wed, 27 Sep 2023 03:21 UTC

On 2023-09-27, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
> Adding it as _Elseif would IMHO be too ugly. Adding a new keyword
> always risks breaking existing code, but C23 is already adding bool,
> false, and true.

One advantage might be that in LALR(1) parsing, "_Elseif" is a
single symbol, consuming only one lookahead position, whereas "else if"
is two tokens. If we are at else, and the next token is if, that's our
lookeahead; we don't see past it. Whereas if wee are at _Elseif,
we have whatever token follows as lookahead.

I can't think of what situations that would benefit; i.e. what could
be parsed that is not parseable now.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Do you insist on const-correctness?

<87pm24s20a.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29316&group=comp.lang.c#29316

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Tue, 26 Sep 2023 20:56:05 -0700
Organization: None to speak of
Lines: 22
Message-ID: <87pm24s20a.fsf@nosuchdomain.example.com>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me>
<20230926205357.930d996e817bd458e0ff656b@gmail.moc>
<uev74t$2kgdi$1@dont-email.me> <20230926122027.184@kylheku.com>
<uevtup$2of5s$1@dont-email.me>
<87y1gss5dq.fsf@nosuchdomain.example.com>
<20230926201809.34@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="08e30d084ec8438dcadf9cbd76f2c53f";
logging-data="3075691"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18BEB69EF8FJ3NzhEBAokaA"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:EVa5vsXVllghq8ed3qzRv/DY5Rw=
sha1:95b+xgGA8haczjMO6DiWjbQdPd8=
 by: Keith Thompson - Wed, 27 Sep 2023 03:56 UTC

Kaz Kylheku <864-117-4973@kylheku.com> writes:
> On 2023-09-27, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>> Adding it as _Elseif would IMHO be too ugly. Adding a new keyword
>> always risks breaking existing code, but C23 is already adding bool,
>> false, and true.
>
> One advantage might be that in LALR(1) parsing, "_Elseif" is a
> single symbol, consuming only one lookahead position, whereas "else if"
> is two tokens. If we are at else, and the next token is if, that's our
> lookeahead; we don't see past it. Whereas if wee are at _Elseif,
> we have whatever token follows as lookahead.
>
> I can't think of what situations that would benefit; i.e. what could
> be parsed that is not parseable now.

Nor can I -- and if I could, I'd probably try to avoid writing code like
that.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Re: Do you insist on const-correctness?

<uf0r7l$30kt2$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29318&group=comp.lang.c#29318

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Wed, 27 Sep 2023 11:04:53 +0200
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <uf0r7l$30kt2$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me>
<499dbc5e-44f7-45a6-bb0b-69ec6933d0a6n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 27 Sep 2023 09:04:53 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bf84cce5931c1821101b4b3bbf25c309";
logging-data="3167138"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19fku8aYXUYEGIITqssG5Wl/eBNdcgKys4="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:ImY5sAZbhVf+qlgMV29m9C1Zpfc=
Content-Language: en-GB
In-Reply-To: <499dbc5e-44f7-45a6-bb0b-69ec6933d0a6n@googlegroups.com>
 by: David Brown - Wed, 27 Sep 2023 09:04 UTC

On 26/09/2023 17:29, Malcolm McLean wrote:
> On Tuesday, 26 September 2023 at 16:11:07 UTC+1, David Brown wrote:
>> On 26/09/2023 13:37, Anton Shepelev wrote:
>>>
>>> Linus 8-chareater-tab Torvalds was IMHO right when he
>>> decreed that a function shall not have more then three
>>> levels of nesting, his enourmous tab serving as a dumb,
>>> primitive, mechanical, and ugly means of enforsing this
>>> decree.
>> No, Linus was wrong - as he is on many things. (He's right on many
>> things too, of course.) We are not using typewriters, we are not using
>> 80x25 character displays, and we do not need to have pointless
>> limitations. I don't advocate for deeply nested or overly complex
>> functions, of course - I advocate for clear code with immediately
>> obvious blocks indicated by braces /and/ indents. And if clear code
>> requires more than 3 levels of nesting, use that.
>>
> Rule of three. Three levels of nesting or indirection. Because we live in a three
> dimensional world.

Rule of seven. Seven levels of nesting or indirection. Because we have
seven days in the week.

See? Anyone can write complete and utter drivel, with totally
nonsensical "justifications".

Don't you ever get tired of spouting these insane platitudes, as though
you imagine yourself as some kind of holy prophet of programming? Your
track record here, and the way you make such pronouncements as if you
were revealing the hidden laws of the universe, makes people assume you
are wrong. Even though you are sometimes accidentally correct, or at
least partially right, the gut reaction is that Malcolm is pontificating
again, so its nonsense.

Please put a post-it note on the top of your screen - "There is no rule
of three". Maybe over time it will sink in.

Re: Do you insist on const-correctness?

<uf0s28$30uj6$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29319&group=comp.lang.c#29319

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Wed, 27 Sep 2023 11:19:03 +0200
Organization: A noiseless patient Spider
Lines: 60
Message-ID: <uf0s28$30uj6$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me> <ueuv1r$2iped$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 27 Sep 2023 09:19:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bf84cce5931c1821101b4b3bbf25c309";
logging-data="3177062"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18INFTwIeGgHZ9Y736VHBL7bSlvEpbCktY="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:Z7dDDFJKDOSKVTYLtQuHRjz+RPw=
In-Reply-To: <ueuv1r$2iped$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Wed, 27 Sep 2023 09:19 UTC

On 26/09/2023 17:57, Bart wrote:
> On 26/09/2023 16:10, David Brown wrote:
>> On 26/09/2023 13:37, Anton Shepelev wrote:
>
>>>          if( test_1 ) stat_1;
>>>     else if( test_2 ) stat_2;
>>>     else if( test_3 ) stat_3;
>>>
>>
>> These are both wrong :-)
>>
>>
>>      if (test1) {
>>          stat_1;
>>      } else if (test2) {
>>          stat_2;
>>      } else if (test3) {
>>          stat_3;
>>      } else {
>>          ...
>>      }
>>
>> That is clearer than yours, and far more maintainable.  And any coding
>> standard with a care for safety, security or reliability, (such as
>> MISRA or JSF-AV mentioned in this thread) will insist on the braces here.
>
> What is the actual rule about braces? Because you've clearly missed some
> out there, in omitting them around the first two 'else' branches.

It is certainly /possible/ to put in more braces (that is possible in a
great many places in C code). And you do have a valid argument for
consistency.

However, "else if" can be viewed as the C equivalent of "elseif" or
"elsif", as found in some other languages. This leads itself to a
simple and clear pattern like this. Sometimes such patterns can be more
important for clarity than the general rules.

>
> If you saying this is OK, then that's playing fast and rule with any
> such rule that braces must always be used even around one statement.
>

You are correct that my rules (and most people's rules) for braces are
not quite simple.

I am also quite happy with simple, short and unambiguous (no macros)
statements on the same line :

if (!p) return;

if (x > 0) x--;

But if things are more complex - the conditional is larger, the
statement is not "simple" (I admit that I am not defining "simple"
here), or the total line is getting too long, then I put in the braces
and indentation. Spilling to a second line without having the braces is
definitely wrong.

Re: Do you insist on const-correctness?

<uf0s9s$30uj6$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29320&group=comp.lang.c#29320

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Wed, 27 Sep 2023 11:23:08 +0200
Organization: A noiseless patient Spider
Lines: 108
Message-ID: <uf0s9s$30uj6$2@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me> <ueuv1r$2iped$1@dont-email.me>
<20230926093309.110@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 27 Sep 2023 09:23:08 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bf84cce5931c1821101b4b3bbf25c309";
logging-data="3177062"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/3nCSZSy0tLZDpRqxwwWyjZ5xWpUazNRo="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:6IsUet5b7TSx+W0RE22HH2zllxs=
Content-Language: en-GB
In-Reply-To: <20230926093309.110@kylheku.com>
 by: David Brown - Wed, 27 Sep 2023 09:23 UTC

On 26/09/2023 18:40, Kaz Kylheku wrote:
> On 2023-09-26, Bart <bc@freeuk.com> wrote:
>> On 26/09/2023 16:10, David Brown wrote:
>>> On 26/09/2023 13:37, Anton Shepelev wrote:
>>
>>>>          if( test_1 ) stat_1;
>>>>     else if( test_2 ) stat_2;
>>>>     else if( test_3 ) stat_3;
>>>>
>>>
>>> These are both wrong :-)
>>>
>>>
>>>     if (test1) {
>>>         stat_1;
>>>     } else if (test2) {
>>>         stat_2;
>>>     } else if (test3) {
>>>         stat_3;
>>>     } else {
>>>         ...
>>>     }
>>>
>>> That is clearer than yours, and far more maintainable.  And any coding
>>> standard with a care for safety, security or reliability, (such as MISRA
>>> or JSF-AV mentioned in this thread) will insist on the braces here.
>>
>> What is the actual rule about braces? Because you've clearly missed some
>> out there, in omitting them around the first two 'else' branches.
>
> The actual rules is that the required braces, comprising a compound
> statement, can be prefixed by something which modifies the statement
> into a conditional.
>>
>> If you saying this is OK, then that's playing fast and rule with any
>> such rule that braces must always be used even around one statement.
>
> Indeed, I myself sometimes writes in a style like the following:
>
> if (condition) {
> /* loop not needed */
> } else for (...) {
>
> }
>
> The else keyword needs a statement, and the for statement
> satisfies it. It has braces of its own, so everything is cool.

I would not go that far. "else if" in C is, to me, a short form for
"elseif" or "elsif" found in other languages. I've never seen an
"elsefor" or "elsewhile" keyword.

"if, else if, else" chains are common and fit a nice pattern. But a
"for" loop is completely different, breaking the pattern - I'd have it
in its own braces :

if (...) {
...
} else {
for ( ... ) {
...
}
}

>
> The for (...) header is effectively a statement prefix which indicates
> iteration of that statement.
>
> In my style, I allow the braced statement to be prefixed not only
> by if (...) but other, similar statement introducers/prefixes
> like while (...) and switch.
>
> I don't think I've ever written a
>
> if (this) {
>
> } else do {
>
> } while(...);
>
> But I totally might. :)

Eek!

>
> What I wouldn't write is:
>
>
> if (condition) {
> /* loop not needed */
> } else for (...)
> body_expression_statement;
>
> Or, let alone:
>
> if (condition) {
> /* loop not needed */
> } else for (...)
> /* empty */;
>

No, indeed.

> Some languages combine "else" and "if" into a single token, even.
> The C preprocessing language is like that: #elif.
>

Re: Do you insist on const-correctness?

<uf0t27$3149p$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29321&group=comp.lang.c#29321

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Wed, 27 Sep 2023 10:36:06 +0100
Organization: A noiseless patient Spider
Lines: 60
Message-ID: <uf0t27$3149p$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me>
<20230926205357.930d996e817bd458e0ff656b@gmail.moc>
<uev74t$2kgdi$1@dont-email.me> <20230926122027.184@kylheku.com>
<uevtup$2of5s$1@dont-email.me> <87y1gss5dq.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 27 Sep 2023 09:36:07 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="07ea6ddc360482fe4d9e51f9ca54afb9";
logging-data="3182905"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+lv3ujFR4xY7JJpwZNnRjS6pKYH4W20sw="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:5JwZ+cWdCzaiJ6pJB6uk4YKrjPo=
In-Reply-To: <87y1gss5dq.fsf@nosuchdomain.example.com>
 by: Bart - Wed, 27 Sep 2023 09:36 UTC

On 27/09/2023 03:43, Keith Thompson wrote:
> Bart <bc@freeuk.com> writes:
> [...]
>> I wonder how big a deal it would have been to introduce an actual
>> 'else-if' token? I expect it would look something like _Elseif; just
>> using a the hypenated 'else-if' would be better!
>
> A hyphenated keyword?

It would be three consecutive tokens: else, -, if, treated as though
there was a dedicated 'elseif' keyword. The advantage is that it
requires no new keyword.

> In various languages, I've seen "elif", "elsif", and "elseif".
> I'd be fine with any of those.
>
> But though I think it would have been nice to add such a keyword from
> the beginning, I'm not convinced it would be worth doing now. It's
> already perfectly possible to write long if/else chains in C:
>
> if (...) {
> ...
> }
> else if (...) {
> ...
> }
> else if (...) {
> ...
> }
> else {
> ...
> }

> (I know some people prefer to put the else on the same line as the {.
> That's not really the point.)

> Even though the indentation doesn't strictly reflect the grammar, it's a
> common enough idiom that that doesn't bother me.

It bothers /me/ because it's not pure. Because, if you write translation
rules, the nested nature of the chain is still there.

Both 'else if' and 'else { if' sequences are encountered in source code.

Because sometimes 'else' is followed by one statement, sometimes by
more, or you want the possibility that the one statement can be expanded
to more, or temporarily commented out.

You get that flexibility by having a strict rule that an 'if' statement
always looks like one of these:

if (cond) {...}
if (cond) {...} else {...}

and not for example like this:

if (cond) {...} else if (cond2) {...}

Re: Do you insist on const-correctness?

<uf0t4p$314n3$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29322&group=comp.lang.c#29322

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Wed, 27 Sep 2023 11:37:28 +0200
Organization: A noiseless patient Spider
Lines: 130
Message-ID: <uf0t4p$314n3$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me> <ueuv1r$2iped$1@dont-email.me>
<20230926093309.110@kylheku.com> <uev5e6$2k5cd$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 27 Sep 2023 09:37:29 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bf84cce5931c1821101b4b3bbf25c309";
logging-data="3183331"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/sJvEZYPjTCmQovWlxo5r8XP3F9JrrGjE="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:Hn32a6hdQEnhPu6Oueu+49p/qjw=
Content-Language: en-GB
In-Reply-To: <uev5e6$2k5cd$1@dont-email.me>
 by: David Brown - Wed, 27 Sep 2023 09:37 UTC

On 26/09/2023 19:46, Bart wrote:
> On 26/09/2023 17:40, Kaz Kylheku wrote:
>> On 2023-09-26, Bart <bc@freeuk.com> wrote:
>>> On 26/09/2023 16:10, David Brown wrote:
>
>>> What is the actual rule about braces? Because you've clearly missed some
>>> out there, in omitting them around the first two 'else' branches.
>>
>> The actual rules is that the required braces, comprising a compound
>> statement, can be prefixed by something which modifies the statement
>> into a conditional.
>
> But then that compound statement becomes a single statement with no
> braces of its own, and many of the problems associated with optional
> braces return.
>
>>> If you saying this is OK, then that's playing fast and rule with any
>>> such rule that braces must always be used even around one statement.
>>
>> Indeed, I myself sometimes writes in a style like the following:
>>
>>    if (condition) {
>>      /* loop not needed */
>>    } else for (...) {
>>
>>    }
>>
>> The else keyword needs a statement, and the for statement
>> satisfies it. It has braces of its own, so everything is cool.
>
> Suppose you were to write it like this:
>
>
>     } else
>         for (...) {
>
> then someone adds an extra statement:
>
>     } else
>         puts("Loop follows.");
>         for (...) {
>
> Now it's not so cool.
>

Exactly. The "else if" shortcut, IMHO, should only ever be for "else
if", and the attached actions need to be in braces. Then there are no
ambiguities, and minimal risk for mistakes if someone adds to the code
later.

> Really, it's quite silly: how far deep inside a series of /single/
> nested statements are you allowed to go, to enable you to claim that
> your 'else' branch is actually brace-delimited?
>
>
>> What I wouldn't write is:
>>
>>
>>    if (condition) {
>>      /* loop not needed */
>>    } else
>
> Why wouldn't you write this? Presumably something might be added there
> in future, otherwise it's a poorly thought-out condition.

I assumed from his post (Kaz can correct me if I'm wrong) that it was
the second half of that example that he saw as a problem. The full case
was :

if (condition) {
/* loop not needed */
} else for (...)
/* empty */;

To me, there are several things wrong here - so I certainly won't write
it. But empty bodies in conditionals do turn up sometimes, if that's
the clearest way to write the conditionals. They can be a good
placeholder for comments explaining the reasoning in the code, or - as
you suggest - for possible future additions.

if ((x >= 0) && (y >= 0)) {
// Both non-negative, so nothing to do here
} else if (x >= 0) {
y = x; // Fix negative y
} else if (y >= 0) {
x = y; // Fix negative x
} else {
panic(); // x and y should not both be negative
}

Of course the ordering could be changed, or the tests could be
re-arranged, so that the empty statement is not needed. But sometimes
code clarity suggests a pattern including empty statements.

I will even write something like this on occasion :

if (<complicated condition>) {
// Everything is fine, nothing to do
} else {
do_something();
}

rather than

if (!(<complicated condition>)) {
do_something();
}

It is all about what is clearest to read.

>
>> Some languages combine "else" and "if" into a single token, even.
>> The C preprocessing language is like that: #elif.
>
> This has always puzzled me, where C's preprocessor has a more grown-up,
> more solid , more readable and less error prone syntax than the main
> language. No dangling 'else' there!
>

C doesn't need an "elseif", since it has "else if" that does exactly the
same job. There are no circumstances (AFAICS) where you could use an
"elseif" keyword but not "else if".

For the preprocessor, you could not have "#else #if", thus you need a
separate preprocessor directive.

Re: Do you insist on const-correctness?

<uf10up$31pin$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29323&group=comp.lang.c#29323

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Wed, 27 Sep 2023 11:42:33 +0100
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <uf10up$31pin$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me> <ueuv1r$2iped$1@dont-email.me>
<20230926093309.110@kylheku.com> <uf0s9s$30uj6$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 27 Sep 2023 10:42:33 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="07ea6ddc360482fe4d9e51f9ca54afb9";
logging-data="3204695"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19PAusIcV1z9DSfXv/6AncE68UsNSbghcw="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:PrC9Z0r40L/yyxw6NhAxIWJ2sic=
In-Reply-To: <uf0s9s$30uj6$2@dont-email.me>
 by: Bart - Wed, 27 Sep 2023 10:42 UTC

On 27/09/2023 10:23, David Brown wrote:
> On 26/09/2023 18:40, Kaz Kylheku wrote:

>> The else keyword needs a statement, and the for statement
>> satisfies it. It has braces of its own, so everything is cool.
>
> I would not go that far.  "else if" in C is, to me, a short form for
> "elseif" or "elsif" found in other languages.  I've never seen an
> "elsefor" or "elsewhile" keyword.

My language has 'if-elsif', 'case' and 'switch' selection statements
which can each have arbitrarily long sequences of tests.

Each can have an optional 'else' branch. (When used inside an
expression, which is not often, the whole thing returns a value so the
'else' is needed.)

Now, brace yourself... sometimes a series of tests changes between if,
case and switch, so I allow these to be mixed within the same statement:

if c1 then
elsif c2 then
elsecase x1
...
elsif c3 then
...
elseswitch x2
...
end

This just saves having deeply nested statements when the testing pattern
is really linear.

I don't have elsefor, that never comes up and doesn't make sense. But I
do have looping forms of switch/case called doswitch and docase, which
are used extensively.

(My 'switch' has the same restrictions as C's, plus it must be
implementable as a jump table. 'case' has the same syntax, but no
restrictions, and testing is sequential.)

Re: Do you insist on const-correctness?

<20230927151302.f1b8f4a162a805a9ba1688ab@gmail.moc>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29324&group=comp.lang.c#29324

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton....@gmail.moc (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Wed, 27 Sep 2023 15:13:02 +0300
Organization: A noiseless patient Spider
Lines: 108
Message-ID: <20230927151302.f1b8f4a162a805a9ba1688ab@gmail.moc>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me>
<ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me>
<ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me>
<ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me>
<ueuv1r$2iped$1@dont-email.me>
<20230926093309.110@kylheku.com>
<uev5e6$2k5cd$1@dont-email.me>
<uf0t4p$314n3$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: dont-email.me; posting-host="bbee8acfd285cc8a316c79c6954e8e46";
logging-data="3230709"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19/Jy1ozUHXUbQJpd0lC6RqIRrE6xAyRvM="
Cancel-Lock: sha1:7GG3h2mWcI9ndExo94RUcYqNFJ8=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Wed, 27 Sep 2023 12:13 UTC

David Brown:

> But sometimes
> code clarity suggests a pattern including empty statements.
>
> I will even write something like this on occasion :
>
> if (<complicated condition>) {
> // Everything is fine, nothing to do
> } else {
> do_something();
> }
>
> rather than
>
> if (!(<complicated condition>)) {
> do_something();
> }

Sometimes my arrangement is decided by which conditional
expression is simpler. When the branches are extremely
unbalanced, that is one is one is very long and the other a
one-liner, I will put the short one into the then-clause,
because the layout is better:

if( <condition> ) one_liner(); if( <condition> )
else { a = rather(long);
{ a = rather(long); and(cumbersome);
and(cumbersome); multiline();
multiline(); paragraph(of);
paragraph(of); code();
code(); }
} else one_liner();

When two thrirds of a long function is the code of some
special conditional processing, sometimes I prefer to jump
over it with a `goto' to save a level of indentation:

function struct baz ver_1(int b) function struct baz ver_1(int b)
{ int a = normal_proc_1(b ); { int a = normal_proc_1(b );
int c = normal_proc_2(a, b); int c = normal_proc_2(a, b);
if( special ) if( !special ) goto SKIP_SPECIAL;
{ spec_t = spec_init(a,c);
if( special_test(s) ) spec_t s = spec_init(a,c);
{ s1 = do_foo(s); if( special_test(s) )
s2 = do_bar(s); { s1 = do_foo(s);
if( s1 > s2 ) s2 = do_bar(s);
{ c = calc_c(a, s1, s2,); if( s1 > s2 )
many(); { c = calc_c(a, s1, s2,);
more(); many();
lines(); more();
here(); lines();
} here();
} }
special_finalize(c); }
} special_finalize(c);
temp = finalize(c); SKIP_SPECIAL:
if( test( temp ) ) temp = finalize(c);
{ update(c); } if( test( temp ) )
return c; { update(c); }
} return c;
}

Do all present vehemently condemn this practice?
Christopher Seiwald's seventh pillar seems to encourage
it[1]:

The left edge of the code defines its structure, while
the right side holds the detail. You must fight
indentation to safeguard this property. Code which moves
too quickly from left to right (and back again) mixes
major control flow with minor detail.

Forcibly align the main flow of control down the left
side, with one level of indentation for
if/while/for/do/switch statements. Use break, continue,
return, even 'goto' to coerce the code into left-side
alignment. Rearrange conditionals so that the block with
the quickest exit comes first, and then return (or break,
or continue) so that the other leg can continue at the
same indentation level.

> For the preprocessor, you could not have "#else #if", thus
> you need a separate preprocessor directive.

I think this is possible, but only hierarchically:

#if M1
...
#else
#if M2
...
#else
#if M3
...
#endif
#endif
#endif

Therefore, #elseif is introduced not to add a new possiblity
but to let the programmer express what was already possible
in a layout that better reflects his intent.
____________________
1. <https://web.archive.org/web/20100227185627/http://www.perforce.com/perforce/papers/prettycode.html>

--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments

Re: Do you insist on const-correctness?

<20230927152759.8c60e82f32aecacadc83ea1b@gmail.moc>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29325&group=comp.lang.c#29325

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton....@gmail.moc (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Wed, 27 Sep 2023 15:27:59 +0300
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <20230927152759.8c60e82f32aecacadc83ea1b@gmail.moc>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me>
<ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me>
<ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me>
<ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me>
<ueuv1r$2iped$1@dont-email.me>
<20230926093309.110@kylheku.com>
<uf0s9s$30uj6$2@dont-email.me>
<uf10up$31pin$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: dont-email.me; posting-host="bbee8acfd285cc8a316c79c6954e8e46";
logging-data="3243092"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19isxyIGybohx1cPdJhWBem/H7l3koHiBA="
Cancel-Lock: sha1:KRG66gjVOFJrpEwV/GaUXkhJ5VM=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Wed, 27 Sep 2023 12:27 UTC

Bart:

> Now, brace yourself... sometimes a series of tests changes
> between if, case and switch, so I allow these to be mixed
> within the same statement:
>
> if c1 then
> elsif c2 then
> elsecase x1
> ...
> elsif c3 then
> ...
> elseswitch x2
> ...
> end
>
> This just saves having deeply nested statements when the
> testing pattern is really linear.

It looks awkward to me, especially without any horisontal
alignment, but your rationale is sound: the concept of
chaining branching statements is orthogonal to the the kind
individual statements, so that mixing them must be logical.

> (My 'switch' has the same restrictions as C's, plus it
> must be implementable as a jump table. 'case' has the same
> syntax, but no restrictions, and testing is sequential.)

You said upthread that one of your multiple-select
statements was more like SQL's CASE, i.e. able to dispatch
by condition /and/ by a value.

For lack of a special provision for a linear if-else chains,
I sometimes use a sequence of normal `if' statements with
`goto':

if( c1 ) { x = v1 ; goto X_SET; }
if( c2 ) { x = v2 ; goto X_SET; }
if( c3 ) { x = v3 ; goto X_SET; }
x = v_def;
X_SET:
/* continue with x ... */

--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments

Re: Do you insist on const-correctness?

<uf17dn$33109$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29326&group=comp.lang.c#29326

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Wed, 27 Sep 2023 13:32:54 +0100
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <uf17dn$33109$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me> <ueuv1r$2iped$1@dont-email.me>
<20230926093309.110@kylheku.com> <uev5e6$2k5cd$1@dont-email.me>
<uf0t4p$314n3$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 27 Sep 2023 12:32:55 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="07ea6ddc360482fe4d9e51f9ca54afb9";
logging-data="3245065"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/dr6IDk23YgeN8Ku3DDBJjzExUzLwIwBY="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:8JPfHI/oqaoU6ncBzM295HdttVg=
In-Reply-To: <uf0t4p$314n3$1@dont-email.me>
 by: Bart - Wed, 27 Sep 2023 12:32 UTC

On 27/09/2023 10:37, David Brown wrote:

> I will even write something like this on occasion :
>
>     if (<complicated condition>) {
>         // Everything is fine, nothing to do
>     } else {
>         do_something();
>     }
>
> rather than
>
>     if (!(<complicated condition>)) {
>         do_something();
>     }
>
> It is all about what is clearest to read.

I have a version of 'if' called 'unless', which sometimes reads better,
but not always.

In particular, although 'unless' can have an 'else' branch, it doesn't
have 'else-unless'; I couldn't work out what it meant!

Also, while C has 'do...while', my equivalent is 'repeat...until', with
the opposite logic.

I can't have repeat...while as that is ambiguous: the 'while' could be
starting a new loop. Actually I had some troubling figuring out how C
manages to avoid the same problem.

(Note that I can write C's

do {a;b;c;} while (cond);

as:

while a;b;c; cond do end

Ugly, though.)

Re: Do you insist on const-correctness?

<4085f2b1-5d8e-4479-a983-5581cea49b53n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29327&group=comp.lang.c#29327

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:4706:b0:770:58ab:afb4 with SMTP id bs6-20020a05620a470600b0077058abafb4mr23875qkb.8.1695822245337;
Wed, 27 Sep 2023 06:44:05 -0700 (PDT)
X-Received: by 2002:a05:6870:c79d:b0:1dc:8274:a1d with SMTP id
dy29-20020a056870c79d00b001dc82740a1dmr931386oab.6.1695822245090; Wed, 27 Sep
2023 06:44:05 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!border-2.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Wed, 27 Sep 2023 06:44:04 -0700 (PDT)
In-Reply-To: <20230927152759.8c60e82f32aecacadc83ea1b@gmail.moc>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:f919:d1df:b42:2927;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:f919:d1df:b42:2927
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com> <20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc> <ueus9s$2ibi2$1@dont-email.me>
<ueuv1r$2iped$1@dont-email.me> <20230926093309.110@kylheku.com>
<uf0s9s$30uj6$2@dont-email.me> <uf10up$31pin$1@dont-email.me> <20230927152759.8c60e82f32aecacadc83ea1b@gmail.moc>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4085f2b1-5d8e-4479-a983-5581cea49b53n@googlegroups.com>
Subject: Re: Do you insist on const-correctness?
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Wed, 27 Sep 2023 13:44:05 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 42
 by: Malcolm McLean - Wed, 27 Sep 2023 13:44 UTC

On Wednesday, 27 September 2023 at 13:28:15 UTC+1, Anton Shepelev wrote:
> Bart:
> > Now, brace yourself... sometimes a series of tests changes
> > between if, case and switch, so I allow these to be mixed
> > within the same statement:
> >
> > if c1 then
> > elsif c2 then
> > elsecase x1
> > ...
> > elsif c3 then
> > ...
> > elseswitch x2
> > ...
> > end
> >
> > This just saves having deeply nested statements when the
> > testing pattern is really linear.
> It looks awkward to me, especially without any horisontal
> alignment, but your rationale is sound: the concept of
> chaining branching statements is orthogonal to the the kind
> individual statements, so that mixing them must be logical.
> > (My 'switch' has the same restrictions as C's, plus it
> > must be implementable as a jump table. 'case' has the same
> > syntax, but no restrictions, and testing is sequential.)
> You said upthread that one of your multiple-select
> statements was more like SQL's CASE, i.e. able to dispatch
> by condition /and/ by a value.
>
> For lack of a special provision for a linear if-else chains,
> I sometimes use a sequence of normal `if' statements with
> `goto':
>
> if( c1 ) { x = v1 ; goto X_SET; }
> if( c2 ) { x = v2 ; goto X_SET; }
> if( c3 ) { x = v3 ; goto X_SET; }
> x = v_def;
> X_SET:
> /* continue with x ... */
>
I'd find that confusing. Are c1, c2, c3 mutually exclusive? If so, why are they
separate conditions? If more than one is true, the first option is chosen. But
why?

Re: Do you insist on const-correctness?

<uf1f8f$34mmt$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29328&group=comp.lang.c#29328

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Wed, 27 Sep 2023 16:46:38 +0200
Organization: A noiseless patient Spider
Lines: 363
Message-ID: <uf1f8f$34mmt$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me>
<20230926205357.930d996e817bd458e0ff656b@gmail.moc>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 27 Sep 2023 14:46:39 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bf84cce5931c1821101b4b3bbf25c309";
logging-data="3300061"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+usx4FY+xXICB2l3xOTqfpHbGARaIFtvA="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:I98I5sf7Xu3eycThJRnvQ03QdZU=
Content-Language: en-GB
In-Reply-To: <20230926205357.930d996e817bd458e0ff656b@gmail.moc>
 by: David Brown - Wed, 27 Sep 2023 14:46 UTC

On 26/09/2023 19:53, Anton Shepelev wrote:
> David Brown to Anton Shepelev:
>
>>> Modern languages with multiple returns or tuples provide
>>> a partial solution, another being to unify error info
>>> with the result of successful execution in dynamic
>>> languages or ones with templates (to save the declaring
>>> a combined TResult type for every function). But then,
>>> I loose the whole pupose of an error flag -- the ability
>>> to invoke funtions inside an `if' statement.
>>
>> You realise that in C you can return structs, and thus
>> include status returns as well as value returns in one
>> item?
>
> I do, which is why I wrote about defining a
> <func_name>TResult type for /every/ function.

You don't need a separate type for each function.

> If, on the
> other hand, I define TResult to contain only error
> information and the success flag, I save a single parameter
> compared to returning the success flag and error info
> separately.
>
>> Still, why are you talking about modern languages and
>> object oriented programming when you insist on using a
>> language outdated a generation ago?
>
> Because C lacks those new features and I am spared the
> pressure to use them in my code :-) Seriously, I see nothing
> bad in considering alternative approaches to get out of the
> box, so to say... I do not think C is outdated a generation
> ago, and even the new standards have far from changed it
> beyond recognition. It is basically the same language.

Thinking about alternatives is always a good idea.

No, C is not outdated (though there are better choices for many of the
programming tasks that used to be done in C). But C90 /is/ outdated,
and has been for a very long time.

>
>>> Linus 8-chareater-tab Torvalds was IMHO right when he
>>> decreed that a function shall not have more then three
>>> levels of nesting, his enourmous tab serving as a dumb,
>>> primitive, mechanical, and ugly means of enforsing this
>>> decree.
>>
>> No, Linus was wrong - as he is on many things. (He's
>> right on many things too, of course.) We are not using
>> typewriters, we are not using 80x25 character displays,
>> and we do not need to have pointless limitations.
>
> They are not pointless. 80 characters is a generous the
> upper limit of a compfortable line width, most typographical
> manuals suggesting lower lengths, and I did not refer to the
> 25-line limit anywhere. My functions are frequently longer.

No, 80 characters is not a comfortable line width limit. It is very
artificial, and limits the use of longer identifiers or occasional
naturally longer lines. Remember, line width limits in programming are
not remotely the same as letter counts per line in prose text.

>
>> I don't advocate for deeply nested or overly complex
>> functions, of course - I advocate for clear code with
>> immediately obvious blocks indicated by braces /and/
>> indents. And if clear code requires more than 3 levels of
>> nesting, use that.
>
> In my opinion, up to three levels are easy to perceive, and
> then it becomes harder epxonentially: four levels is
> tolerable and five is a mess.
>

In reality, it depends on the code and the structure, as well as the
familiarity for the programmer. There's no doubt that deeper nesting
gets harder to follow, all other things being equal. But all other
things are often /not/ equal. Good indentation is hugely easier to
interpret than flat unindented code, or strange "outdented" styles. If
there is a clear pattern to a piece of code, then deeply nested code
will be perfectly comprehensible.

/Most/ code probably needs no more than three levels of indentation (in
addition to the indentation for the function body). But that follows
from the principle of avoiding doing too much in one function, rather
than any fixed rule for indentation limits.

All you achieve by putting arbitrary limits on these things is that the
occasions where longer lines, or deeper nesting, or bigger functions
would result in clearer code, now have to be artificially divided up in
less clear ways. It doesn't help for most code, which will already be
well within whatever limits you pick, but it means that less common code
is worse off.

>>> For an example, and the standard if-else-if-else...
>>> chain is in fact a variant of a switch, and a switch has
>>> the
>>
>> Not in C.
>
> Yes, I meant the broader condept of switch as a table of
> tests and corresponding statements, with perhaps a fallback
> (default) entry. The point is that the sturcture of switch
> is a table with two columns (see below).
>
>>> if( test_1 ) {
>>> stat_1 } else {
>>> if( test_2 ) {
>>> stat_2 } else {
>>> if( test_3 ) {
>>> stat_3 } else {
>>
>> I have never seen if-else chains formatted that way - it
>> is clearly incorrect indentation.
>
> It is technically /the one true/ correct indentation,
> because each level of nesting has its own indent.

Again - I have never seen if-else if-else chains formatted that way. If
you think it is "technically" following the rules, then you have
misunderstood the rules, or whoever gave you the rules (perhaps me)
oversimplified them. The point of style rules is to give clear and
consistent visual formatting that minimises the risk of misunderstanding
or making errors - they are not designed to give simplistic but absolute
rules that result in incomprehensible monstrosities.

The additional rule here can be thought of as treating "else if" as
though it were an "elseif" keyword.

>
>> if (test1) {
>> stat_1;
>> } else if (test2) {
>> stat_2;
>> } else if (test3) {
>> stat_3;
>> } else {
>> ...
>> }
>
> Now, you have recoginised that although syntatically it is a
> deeply nested unblananced tree, semantically it is a linear
> structure, and therefore you flattened it, so that the
> nesting level is no longer equal to the indent. Does a dumb
> pretty-printer understand semantics? Do coding standards
> care about semantics?

Of course pretty-printers take semantics into account. Except for a few
points (such as line continuation, and single-line comments), all white
space is syntactically equivalent to a single space character in C, and
most such spaces have no syntactic meaning at all. "Pretty printing" is
all about making the visual appearance match the semantics of the code,
while keeping the syntax unchanged.

>
>>> if( test_1 ) stat_1;
>>> else if( test_2 ) stat_2;
>>> else if( test_3 ) stat_3;
>>>
>>> It shorter, clearer, and better structured, because the
>>> programmer has formatted the code according its intent.
>>> Observe also the indent of the first `if' to have it
>>> aligned with the other entries of the table.
>>
>> That's a strawman argument, because no one would ever
>> write an if-else chain the way you first suggested.
>

To be clear here - your comparison of your choice of formatting with
your unbalanced tree version is a strawman argument, because no one
would ever write the unbalanced tree version. A comparison to the
linear structure that I gave would not be a strawman argument, because
it is the form that many people use in real code.

> No, what /you/ say after `because' is a strawman argument
> becase it appeals to external circumstances (the number of
> followers) rather than criticises the formatting itself. I
> will try to explain my formatting again: the structure of
> that control statement is a table with two columns and three
> rows. A careful programmer, having respect for the reader,
> will therefore take advantage of the 2-dimensional nature of
> text and format it in a 2x3 grid, aligning the correspondent
> parts together:
> test_1 stat_1
> test_2 stat_2
> test_3 stat_3
>
> It is only meet also to alignt the correspondent keywords,
> as I have done above. This is the only way to express the
> programmer's intent in the formatting. Curly braces would
> be redundant, because the structure is sufficiently
> transparent not to require them, but you can add them
> anyway:
> if( test_1 ) { stat_1 ; }
> else if( test_02 ) { stat_02 ; }
> else if( test_003 ) { stat_003; }
>
> I have introduced indentifiers of varying length to show the
> correct horisontal alignment of closing parentheses, curly
> braces, and semicolons.
>


Click here to read the complete article
Re: Do you insist on const-correctness?

<20230927175646.0b69b35a85c6105a06a773d9@gmail.moc>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29329&group=comp.lang.c#29329

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.nntp4.net!news.gegeweb.eu!gegeweb.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton....@gmail.moc (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Wed, 27 Sep 2023 17:56:46 +0300
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <20230927175646.0b69b35a85c6105a06a773d9@gmail.moc>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me>
<ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me>
<ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me>
<ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me>
<ueuv1r$2iped$1@dont-email.me>
<20230926093309.110@kylheku.com>
<uf0s9s$30uj6$2@dont-email.me>
<uf10up$31pin$1@dont-email.me>
<20230927152759.8c60e82f32aecacadc83ea1b@gmail.moc>
<4085f2b1-5d8e-4479-a983-5581cea49b53n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: dont-email.me; posting-host="bbee8acfd285cc8a316c79c6954e8e46";
logging-data="3302264"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19j9n79Gx4ZQP61FiMFtyLr4kV0yTFVJpE="
Cancel-Lock: sha1:wVbq7iQ/iA5Fao5eRVt87jywsPc=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Wed, 27 Sep 2023 14:56 UTC

Malcolm McLean to Anton Shepelev:

> > For lack of a special provision for a linear if-else
> > chains, I sometimes use a sequence of normal `if'
> > statements with `goto':
> >
> > if( c1 ) { x = v1 ; goto X_SET; }
> > if( c2 ) { x = v2 ; goto X_SET; }
> > if( c3 ) { x = v3 ; goto X_SET; }
> > x = v_def;
> > X_SET:
> > /* continue with x ... */
>
> I'd find that confusing. Are c1, c2, c3 mutually
> exclusive? If so, why are they separate conditions? If
> more than one is true, the first option is chosen. But
> why?

Thay may or may not be mutually exclusive. My point is,
that whichever the case, the code above behaves identically
to the if-else chain:

if( c1 ) x = v1 ;
else if( c2 ) x = v2 ;
else if (c3 ) x = v3 ;
else x = v_def;

--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments

Re: Do you insist on const-correctness?

<uf1g7o$34tu1$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29330&group=comp.lang.c#29330

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Wed, 27 Sep 2023 17:03:19 +0200
Organization: A noiseless patient Spider
Lines: 84
Message-ID: <uf1g7o$34tu1$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me>
<20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc>
<ueus9s$2ibi2$1@dont-email.me>
<499dbc5e-44f7-45a6-bb0b-69ec6933d0a6n@googlegroups.com>
<uf0r7l$30kt2$1@dont-email.me>
<2aa534e2-1567-43d5-9f36-7c4266e3b9e1n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 27 Sep 2023 15:03:20 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bf84cce5931c1821101b4b3bbf25c309";
logging-data="3307457"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX194AQMdjnCAbWJ9CExdDOHMoc8Gh9EnnTQ="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:4GIwWYW9n13/tJmcipMmJMrjs+s=
In-Reply-To: <2aa534e2-1567-43d5-9f36-7c4266e3b9e1n@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Wed, 27 Sep 2023 15:03 UTC

On 27/09/2023 12:37, Malcolm McLean wrote:
> On Wednesday, 27 September 2023 at 10:05:12 UTC+1, David Brown wrote:
>> On 26/09/2023 17:29, Malcolm McLean wrote:
>>> On Tuesday, 26 September 2023 at 16:11:07 UTC+1, David Brown wrote:
>>>> On 26/09/2023 13:37, Anton Shepelev wrote:
>>>>>
>>>>> Linus 8-chareater-tab Torvalds was IMHO right when he
>>>>> decreed that a function shall not have more then three
>>>>> levels of nesting, his enourmous tab serving as a dumb,
>>>>> primitive, mechanical, and ugly means of enforsing this
>>>>> decree.
>>>> No, Linus was wrong - as he is on many things. (He's right on many
>>>> things too, of course.) We are not using typewriters, we are not using
>>>> 80x25 character displays, and we do not need to have pointless
>>>> limitations. I don't advocate for deeply nested or overly complex
>>>> functions, of course - I advocate for clear code with immediately
>>>> obvious blocks indicated by braces /and/ indents. And if clear code
>>>> requires more than 3 levels of nesting, use that.
>>>>
>>> Rule of three. Three levels of nesting or indirection. Because we live in a three
>>> dimensional world.
>> Rule of seven. Seven levels of nesting or indirection. Because we have
>> seven days in the week.
>>
>> See? Anyone can write complete and utter drivel, with totally
>> nonsensical "justifications".
>>
>> Don't you ever get tired of spouting these insane platitudes, as though
>> you imagine yourself as some kind of holy prophet of programming? Your
>> track record here, and the way you make such pronouncements as if you
>> were revealing the hidden laws of the universe, makes people assume you
>> are wrong. Even though you are sometimes accidentally correct, or at
>> least partially right, the gut reaction is that Malcolm is pontificating
>> again, so its nonsense.
>>
>> Please put a post-it note on the top of your screen - "There is no rule
>> of three". Maybe over time it will sink in.
>>
> It is odd that singificant figures in the programming world seem to have a
> habit of agreeing with me.
> Maybe consider that maybe Malcolm is more correct than he is wrong and
> that you, David Brown, can sometimes learn something.
>

I've considered it, and I have concluded that you are wrong more often
than you are right in these things. Or, to be more precise, you are
wrong in your absolutist ideas ("three is good, four is bad"),
unjustified in your rationalisation for your ideas (they are often
totally absurd), and your appeals to authority never hold up.

I am quite happy to agree that too many levels of nesting makes code
harder to understand. I am happy to agree that for a lot of code, three
levels of nesting (not including indents for functions, classes, etc.)
is usually sufficient. I can even agree that deeper nesting than that
may be a sign that the function would be better if it were organised
somewhat differently, or split up.

But that does /not/ mean that three levels of nesting is any kind of
absolute limit, or cut-off point. It does not mean that three levels is
always clear, or that four levels are always bad - it merely means that
too many levels makes code harder to follow.

There is no mythical "rule of three" here, or anywhere else. You cannot
simply take everything that you can think of that is related to the
number 3, and lump it together as though there is a divine purpose
linking them all. It's the same nutjob mentality that gives us
flat-earthers.

> (Nesting, indirection, and dimensioning are all related to each other).

No, they are not.

None of this means you are always wrong, nor does it mean I can't learn
from things you write. But it does mean that it can take some effort to
separate your fairytale nonsense from your rational and relevant points.

You have plenty of useful and interesting things to say, and things we
/can/ learn from. Please stop distracting from that with your imaginary
rules and even more unrealistic explanations for them.

Re: Do you insist on const-correctness?

<bb817356-d077-49c2-8bf4-c3f9774aabffn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29331&group=comp.lang.c#29331

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:ced:b0:775:76da:672d with SMTP id c13-20020a05620a0ced00b0077576da672dmr17152qkj.3.1695828902762;
Wed, 27 Sep 2023 08:35:02 -0700 (PDT)
X-Received: by 2002:a4a:4109:0:b0:573:540a:fe3b with SMTP id
x9-20020a4a4109000000b00573540afe3bmr755805ooa.0.1695828902460; Wed, 27 Sep
2023 08:35:02 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!border-2.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Wed, 27 Sep 2023 08:35:02 -0700 (PDT)
In-Reply-To: <uf1g7o$34tu1$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:f919:d1df:b42:2927;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:f919:d1df:b42:2927
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com> <20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <20230923190523.1f810ae96368b345fe455268@gmail.moc>
<uera6l$1pr35$1@dont-email.me> <ueronc$1sd54$1@dont-email.me>
<ues2jo$1uh7s$1@dont-email.me> <ueshl5$21lmm$1@dont-email.me>
<uesi26$21hbl$7@dont-email.me> <ueskug$22af7$1@dont-email.me>
<20230926143726.8fd502c51264c4e127203da4@gmail.moc> <ueus9s$2ibi2$1@dont-email.me>
<499dbc5e-44f7-45a6-bb0b-69ec6933d0a6n@googlegroups.com> <uf0r7l$30kt2$1@dont-email.me>
<2aa534e2-1567-43d5-9f36-7c4266e3b9e1n@googlegroups.com> <uf1g7o$34tu1$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <bb817356-d077-49c2-8bf4-c3f9774aabffn@googlegroups.com>
Subject: Re: Do you insist on const-correctness?
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Wed, 27 Sep 2023 15:35:02 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 6
 by: Malcolm McLean - Wed, 27 Sep 2023 15:35 UTC

On Wednesday, 27 September 2023 at 16:03:35 UTC+1, David Brown wrote:
> On 27/09/2023 12:37, Malcolm McLean wrote:
>
> > (Nesting, indirection, and dimensioning are all related to each other).
> No, they are not.
>
You haven't thought this through, have you?


devel / comp.lang.c / Re: Do you insist on const-correctness?

Pages:12345678
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor