From: Simon Marchi <simark@simark.ca>
To: Tom de Vries <tdevries@suse.de>, Tom Tromey <tom@tromey.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [committed][gdb/symtab] Make find_block_in_blockvector more robust
Date: Fri, 23 Oct 2020 11:42:25 -0400 [thread overview]
Message-ID: <59e4b623-6ef4-d7eb-8f9a-ecef76d066f8@simark.ca> (raw)
In-Reply-To: <8204bf8a-40f7-de0f-96a4-714f4db06d64@suse.de>
On 2020-10-23 10:08 a.m., Tom de Vries wrote:
> Thanks for the feedback.
>
> I agree it would make it easier to read, but I'm afraid it would make it
> harder to understand.
>
> In general I prefer range tests to conform to this layout:
> ...
> min cmp-op-1 val && val cmp-op-2 max
> ...
> which IMO is the best approximation of the non-C:
> ...
> min cmp-op-1 val cmp-op-2 max
> ...
> and then use the range expression as a whole, either negated or not.
I think we all picture things a bit differently in our minds, which
means we prefer some form over the other, and that's fine.
Not trying to convince you to change anything, but just for the sake of
discussion, this is how I picture it. When reading conditions, I
translate to natural language in my head. Between:
if the pc is smaller than the start of the block, then abort
and
if the start of the block is not smaller or equal than the pc
In the first case, I understand in an instant "ok, it means if the pc is
before the range". For the second, it takes me a little pause: "ok, so
if the start of the block is smaller or equal to the pc, it means we're
in range, so if _not_ that, it means we're out of range.".
>
> But indeed, the negated case is a bit harder to read, something I
> sometimes fix by using a variable with a somewhat helpful name.
>
> This follows my preferred layout for the range expression (albeit split
> up into two variables), while not negating non-trivial expressions:
> ...
> while (bot >= STATIC_BLOCK)
> {
> b = BLOCKVECTOR_BLOCK (bl, bot);
> bool start_ok = BLOCK_START (b) <= pc;
> bool end_ok = pc < BLOCK_END (b);
> if (!start_ok)
> return NULL;
> if (end_ok)
> return b;
> bot--;
> }
> ...
I like this version with variable names. The variable names convey some
information about the intention of the code, and act as some kind of
"checkpoints". If you understand what these boolean variables are meant
to represent (hence the name must be clear), you don't need to parse the
condition themselves to understand the algorithm. If you have some
reason to believe the conditions are erroneous, then you can dig into
them, but otherwise you can skip them and save some mental cycles. Of
course, it gets more valuable as the size/complexity of the conditions
increase.
Simon
next prev parent reply other threads:[~2020-10-23 15:42 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-22 15:24 Tom de Vries
2020-10-22 18:56 ` Tom Tromey
2020-10-22 21:21 ` Tom de Vries
2020-10-22 21:42 ` Simon Marchi
2020-10-23 14:08 ` Tom de Vries
2020-10-23 15:42 ` Simon Marchi [this message]
2020-10-23 12:42 ` Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=59e4b623-6ef4-d7eb-8f9a-ecef76d066f8@simark.ca \
--to=simark@simark.ca \
--cc=gdb-patches@sourceware.org \
--cc=tdevries@suse.de \
--cc=tom@tromey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox