Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

and is bad?

56 views
Skip to first unread message

fir

unread,
Oct 28, 2022, 2:03:53 PM10/28/22
to
i wrote on this in a near thread but to emphasize it

if you have and in if-expressions it seems that this
and is worse in readability than such form as


// a==2 -> !b -> c<0 -> {}

if(a==2) if(!b) if(c<0) {}

this is becouse the "and expressions" make some 'chain'
of tests and 'and' kinda hides the nature of it - so conclusion
and is bad,, the chain form is better

Ben Bacarisse

unread,
Oct 28, 2022, 3:04:37 PM10/28/22
to
Everyone reading C should know how && and || work so they should
therefore see the "chain" of tests in

if (a == 2 && !b && c < 0) { ... }

Using unnecessary nested ifs makes adding an else clause fiddly.
Whereas

if (a == 2 && !b && c < 0) {
...
}
else return ERROR;

is obvious, the nested ifs need an extra {} pair:

if (a == 2) { if (!b) if (c < 0) {
...
}}
else return ERROR;

--
Ben.

fir

unread,
Oct 28, 2022, 3:28:30 PM10/28/22
to
piątek, 28 października 2022 o 21:04:37 UTC+2 Ben Bacarisse napisał(a):
> fir <profes...@gmail.com> writes:
>
> > i wrote on this in a near thread but to emphasize it
> >
> > if you have and in if-expressions it seems that this
> > and is worse in readability than such form as
> >
> >
> > // a==2 -> !b -> c<0 -> {}
> >
> > if(a==2) if(!b) if(c<0) {}
> >
> > this is becouse the "and expressions" make some 'chain'
> > of tests and 'and' kinda hides the nature of it - so conclusion
> > and is bad,, the chain form is better
> Everyone reading C should know how && and || work so they should
> therefore see the "chain" of tests in
>
> if (a == 2 && !b && c < 0) { ... }
>

but this form in unclear imo .. this && is liek arithmetical operators +-*/
and suggest this is this kind of expression where this and as it was stated its
rather stream of tests

this observations possibly may also be used to get some more thoughts
(not yet know which ones)

Ben Bacarisse

unread,
Oct 28, 2022, 5:27:00 PM10/28/22
to
fir <profes...@gmail.com> writes:

> piątek, 28 października 2022 o 21:04:37 UTC+2 Ben Bacarisse napisał(a):
>> fir <profes...@gmail.com> writes:
>>
>> > i wrote on this in a near thread but to emphasize it
>> >
>> > if you have and in if-expressions it seems that this
>> > and is worse in readability than such form as
>> >
>> >
>> > // a==2 -> !b -> c<0 -> {}
>> >
>> > if(a==2) if(!b) if(c<0) {}
>> >
>> > this is becouse the "and expressions" make some 'chain'
>> > of tests and 'and' kinda hides the nature of it - so conclusion
>> > and is bad,, the chain form is better
>> Everyone reading C should know how && and || work so they should
>> therefore see the "chain" of tests in
>>
>> if (a == 2 && !b && c < 0) { ... }
>
> but this form in unclear imo .. this && is liek arithmetical operators +-*/
> and suggest this is this kind of expression where this and as it was stated its
> rather stream of tests

I understood that you think it is unclear and why.

--
Ben.

olcott

unread,
Oct 28, 2022, 5:39:59 PM10/28/22
to
// seems to be the clearest
if (a == 2 && b==0 && C < 0)

--
Copyright 2022 Pete Olcott "Talent hits a target no one else can hit;
Genius hits a target no one else can see." Arthur Schopenhauer

fir

unread,
Oct 28, 2022, 6:16:58 PM10/28/22
to
it could be clearer so from this reason i said it is not fully well done
it could be better it those parts in chain would be
on the same level as if i think
if (a==2) (!b) (c <0 ) { ... } //though it is not good
and orr probbaly could be nested
so maybe something like that is better

if (a == 2) if(!b) if(c <0 or f>9 ) { ... }

those 'or' parts also seen to9 be separated links in chain and this is not fully clear here too


Paul N

unread,
Oct 29, 2022, 12:51:01 PM10/29/22
to
Is it intentional that these two bits of code don't do the same thing?

Bonita Montero

unread,
Oct 29, 2022, 1:01:00 PM10/29/22
to
Am 28.10.2022 um 21:28 schrieb fir:

> but this form in unclear imo .. this && is liek arithmetical operators +-*/
> and suggest this is this kind of expression where this and as it was stated its
> rather stream of tests

&& is shortcutting and it is a sequence-point.
Everyone does know this.


Ben Bacarisse

unread,
Oct 29, 2022, 3:34:06 PM10/29/22
to
Gosh, I wish it were (and thanks for even considering that possibility)
because I could then claim I'd done it deliberately to show how hard it
is to get the nested if case right!

So (though sadly by accident) my point is stronger. If some coding
style forced me to use there nested ifs, I might have to write this just
to add an else clause:

if (a() == 2) if (!b()) if (c() < 0) {
...
goto done;
}
return ERROR;
done:

Of course, what I /would/ do is write the nested ifs as using the
clearer && operator.

--
Ben.
0 new messages