[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [lmi] Safer is the Yoda style?
From: |
Greg Chicares |
Subject: |
Re: [lmi] Safer is the Yoda style? |
Date: |
Mon, 10 Jun 2019 23:35:59 +0000 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 |
On 2019-06-10 16:58, Vadim Zeitlin wrote:
> On Mon, 10 Jun 2019 16:50:52 +0000 Greg Chicares
> <address@hidden> wrote:
>
> GC> I'll continue using the "Yoda" style, though.
>
> I didn't expect anything differently, but your message about
> experimenting with the "human" style gave me an unexpected glimmer
> of hope.
The one time I thought of using that style, I nearly stepped on a landmine.
> GC> In the code I was writing yesterday, it saved me from an awful
> mistake.
>
> Which (also) wouldn't have been detected by the compiler had you
> made it?
Exactly. It was like this:
[add an issue_age() accessor to class database index, then...]
void foo(int issue_age) {
LMI_ASSERT(issue_age = database.index().issue_age());
To me, that seems to assert that two things are equal. Of course I'm
aware that C doesn't work that way, but C isn't my native programming
language, so I'll keep making that mistake from time to time.
I caught it by reviewing the code before stashing it, but I was
properly horrified. This is the one time I tried taking a step off
the path I trust, and it's the sort of error that could take hours
to find. The latest LMI_ASSERT macro would presumably let the
compiler warn me about this case, but why venture into the unknown
when you already have a perfectly fine habit that's reliable?
This:
LMI_ASSERT(database.index().issue_age() = issue_age);
is obviously an syntax error that the compiler must diagnose; I
add an extra '=' and remind myself again that '=' is assignment
and '==' is comparison, but there's no possible harm because it
cannot compile the wrong way, even with all warnings turned off.