Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #82241
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: That's while I love C++ over C |
| Date | 2021-11-06 12:25 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <sm5oo7$cae$1@dont-email.me> (permalink) |
| References | (4 earlier) <sm4eeq$97q$1@redfloyd.dont-email.me> <sm5234$ff1$2@dont-email.me> <sm53gd$nse$1@dont-email.me> <sm53oi$o51$2@redfloyd.dont-email.me> <87r1btygxh.fsf@nosuchdomain.example.com> |
On 06/11/2021 06:46, Keith Thompson wrote:
> red floyd <no.spam.here@its.invalid> writes:
>> On 11/5/2021 10:23 PM, Chris M. Thomasson wrote:
>>> On 11/5/2021 9:59 PM, Chris M. Thomasson wrote:
>>>> On 11/5/2021 4:24 PM, red floyd wrote:
>>>>> On 11/5/2021 3:25 PM, Lynn McGuire wrote:
>>>>>> On 11/5/2021 4:59 PM, Vir Campestris wrote:
>>>>> [insanely ugly, unreadable, and unmaintainable code fragments redacted]
>>>>>>>
>>>>>>> If you wrote that code in anything in my company I would
>>>>>>> downvote the review.
>>>>>>>
>>>>>>> Any of those versions.
>>>>>> I would fire him.
>>>>>
>>>>> I was going to say something along the lines of what either you or Vir
>>>>> said. I might not fire, but I would insist on further training.
>>>>>
>>>>
>>>> Indeed! Further training, or make her read the coding rules of the
>>>> team again.
>>> Ohhh, shit. I vaguely remember using the conditional expression ? :
>>> in a really nasty macro to avoid an if/else... If I could find that
>>> code, you would probably think about firing me! Probably not for the
>>> ? :, but for the damn macro internals! Luckily, it was for my on
>>> personal use. Yikes!
>>
>> As with everything else in C++, there's nothing wrong with the
>> conditional operator ... in the right place. The left hand side of
>> an assignment is NOT the right place. Using the result of a conditional
>> as an rvalue is just fine, I use it all the time.
>
> Why?
>
> Seriously, the C++ standard (unlike the C standard) is clear that a
> conditional expression can be an lvalue if its second and third operands
> are both lvalues of the same type (I'm oversimplifying the rules a bit).
> In other words, the language explicitly allows a conditional expression
> to be the LHS of an assignment. Why do you object to taking advantage
> of that feature?
>
> I don't think I knew about it before seeing this discussion, but now
> that I do, the meaning of
>
> (x == y ? a : b) = c;
>
> seems perfectly clear to me. The alternative:
>
> if (x == y) {
> a = c;
> }
> else {
> b = c;
> }
>
> is more verbose and could be problematic if c is a complicated
> expression; the risk is that a later maintainer might modify one
> instance of c and not the other.
That is a poor argument, IMHO, as it is so easily solved :
auto new_c = complicated_expression;
if (x == y) {
a = new_c;
} else {
b = new_c;
}
Verbose is often good - though certainly not /always/ good. The
important things are the clarity of the code and the /appropriate/
maintainability (if it is extremely unlikely that "c" will ever be
changed, then it is inappropriate to place emphasis on making it easy to
change). It should be easy to see what code does, easy to write
correctly, hard to write incorrectly, and easy to spot when it is incorrect.
It is always difficult to judge the clarity of a simple example code
snippet, because it misses the context. You wonder what would happen if
"c" were more complicated - what if "a" and "b" were more complicated?
They are only written once, so only need to be changed once, but a
complicated expression there would make the conditional operator version
hard to read and easy to misunderstand. What if the logic behind the
code - the reasons for doing the operations, rather than the operations
themselves - were complicated and needed commenting? The expanded
version gives plenty of places to add comments, unlike the conditional
operator version.
What if /you/ are the smartest programmer in your team, and others who
have to understand it are relatively new to C++ ? In a perfect world,
any development team would only consist of people who were familiar with
all the intricacies of the language. We don't live in a perfect world,
and the reality is that the code we write will sometimes have to be
understood by or maintained by people with very different levels of
experience and knowledge. Where you draw the line between "this is
something we expect everyone to understand" and "this is advanced stuff
you might not have seen before" is going to vary enormously - I am not
at all suggesting conditional operators in lvalues are too obscure to
use in general C++ code. But they /are/ too obscure for /some/ projects
and coding styles.
"Always write code as though the person who will maintain it is a
violent psychopath who knows where you live" (I've forgotten the source
of that quotation). Also assume they are not as clever or experienced
as you are.
> And creating a pointer variable:
>
> auto p = x == y ? &a : &b;
> *p = c;
>
> seems silly when you can just use the conditional operator directly.
>
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
That's while I love C++ over C Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-03 20:39 +0100
Re: That's while I love C++ over C Bart <bc@freeuk.com> - 2021-11-03 20:28 +0000
Re: That's while I love C++ over C Vir Campestris <vir.campestris@invalid.invalid> - 2021-11-05 21:59 +0000
Re: That's while I love C++ over C Lynn McGuire <lynnmcguire5@gmail.com> - 2021-11-05 17:25 -0500
Re: That's while I love C++ over C red floyd <no.spam.here@its.invalid> - 2021-11-05 16:24 -0700
Re: That's while I love C++ over C Bart <bc@freeuk.com> - 2021-11-05 23:46 +0000
Re: That's while I love C++ over C Tony Oliver <guinness.tony@gmail.com> - 2021-11-05 17:48 -0700
Re: That's while I love C++ over C Bart <bc@freeuk.com> - 2021-11-06 01:40 +0000
Re: That's while I love C++ over C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-11-06 02:32 +0000
Re: That's while I love C++ over C red floyd <no.spam.here@its.invalid> - 2021-11-05 20:18 -0700
Re: That's while I love C++ over C "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2021-11-06 04:48 +0100
Re: That's while I love C++ over C James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-11-06 00:34 -0400
Re: That's while I love C++ over C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-11-05 22:48 -0700
Re: That's while I love C++ over C red floyd <no.spam.here@its.invalid> - 2021-11-05 22:26 -0700
Re: That's while I love C++ over C Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-06 07:37 +0100
Re: That's while I love C++ over C Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-06 07:36 +0100
Re: That's while I love C++ over C Ike Naar <ike@sdf.org> - 2021-11-06 06:59 +0000
Re: That's while I love C++ over C David Brown <david.brown@hesbynett.no> - 2021-11-06 11:12 +0100
Re: That's while I love C++ over C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-11-06 11:08 +0000
Re: That's while I love C++ over C Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-06 15:03 +0100
Re: That's while I love C++ over C Manfred <noname@add.invalid> - 2021-11-06 19:12 +0100
Re: That's while I love C++ over C Bart <bc@freeuk.com> - 2021-11-06 18:57 +0000
Re: That's while I love C++ over C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-11-06 20:50 +0000
Re: That's while I love C++ over C Bart <bc@freeuk.com> - 2021-11-06 11:44 +0000
Re: That's while I love C++ over C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-11-06 13:18 +0000
Re: That's while I love C++ over C David Brown <david.brown@hesbynett.no> - 2021-11-06 10:58 +0100
Re: That's while I love C++ over C Bart <bc@freeuk.com> - 2021-11-06 12:00 +0000
Re: That's while I love C++ over C Öö Tiib <ootiib@hot.ee> - 2021-11-06 07:04 -0700
Re: That's while I love C++ over C David Brown <david.brown@hesbynett.no> - 2021-11-06 15:50 +0100
Re: That's while I love C++ over C scott@slp53.sl.home (Scott Lurndal) - 2021-11-06 14:43 +0000
[OT] Algol68 Was: That's while I love C++ over C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-11-06 17:13 +0000
Re: [OT] Algol68 Was: That's while I love C++ over C scott@slp53.sl.home (Scott Lurndal) - 2021-11-06 20:16 +0000
Re: [OT] Algol68 Was: That's while I love C++ over C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-11-06 20:25 +0000
Re: That's while I love C++ over C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-11-05 21:59 -0700
Re: That's while I love C++ over C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-11-05 22:23 -0700
Re: That's while I love C++ over C red floyd <no.spam.here@its.invalid> - 2021-11-05 22:27 -0700
Re: That's while I love C++ over C red floyd <no.spam.here@its.invalid> - 2021-11-05 22:28 -0700
Re: That's while I love C++ over C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-11-05 22:46 -0700
Re: That's while I love C++ over C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-11-05 22:46 -0700
Re: That's while I love C++ over C David Brown <david.brown@hesbynett.no> - 2021-11-06 12:25 +0100
Re: That's while I love C++ over C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-11-06 13:27 -0700
Re: That's while I love C++ over C Manfred <noname@add.invalid> - 2021-11-07 00:38 +0100
Re: That's while I love C++ over C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-11-07 00:33 +0000
Re: That's while I love C++ over C David Brown <david.brown@hesbynett.no> - 2021-11-07 15:10 +0100
Re: That's while I love C++ over C James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-11-08 01:47 -0500
Re: That's while I love C++ over C Bart <bc@freeuk.com> - 2021-11-05 23:35 +0000
Re: That's while I love C++ over C Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-06 07:33 +0100
csiph-web