Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
To: Pedro Alves <palves@redhat.com>
Cc: Simon Marchi <simon.marchi@ericsson.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH 2/6] invoke_xmethod & array_view
Date: Mon, 26 Nov 2018 17:18:00 -0000	[thread overview]
Message-ID: <43be2c078bbfbcccd8ab3ec181a8813c@polymtl.ca> (raw)
In-Reply-To: <8947e861-0974-f6b4-f5a8-a181df210e08@redhat.com>

On 2018-11-21 07:46, Pedro Alves wrote:
> I changed it, though I'm not a big fan of that rule.
> 
> As I wrote at <https://gcc.gnu.org/ml/gcc/2018-07/msg00190.html>, I 
> used to
> prefer avoiding non-const reference output parameters too back in my
> earlier C++ days, but I no longer do so much nowadays.
> 
> The main point of avoiding non-const reference parameters is that
> supposedly they make function callers unclearer, with no
> visual indication that the function modified the argument.  The
> idea is that "&foo" instead of "foo" is that visual distinction.
> But, that point can easily become moot because the visual distinction
> can be easily lost with pointers too:
> 
>  // the usual argument is that using pointers for output parameters 
> shows
>  // clearly that bar is going to be modified:
>  function (foo, &bar);
> 
>  // but well, we often works with pointers, and if "bar" is a pointer,
>  // we don't get any visual clue anyway either:
>  function (foo, bar);
> 
>  // which suggests that what really helps is seeing the output
>  // variable nearby, suggesting to define it right before the
>  // function call that fills it in, and I would go as far
>  // as saying that clearer symbol names help even more.  For e.g.:
>  B bar_out;
>  fill_bar (foo, bar_out);

Well, I think that doing both (passing a pointer to the function, and 
defining the variable as close as possible) helps, they are not mutually 
exclusive.  As for the name, it depends on the situation...  if you are 
then going to pass bar_out as input to another function, then it's 
misleading.

I think what helps the most is the fact that arguments passed by 
non-const references are highlighted red in my IDE :)

> I think that symbol and function naming above is much more important
> than "&bar" vs "bar".  A function called "fill_bar" is clearly going
> to write to its "bar_out" argument.

As mentioned above, an "out" argument passed to a function call may be 
an "in" argument to the next one.  I agree that good naming is 
important, but there is no one trick that will make the code "100%" 
readable.  They all help a little bit towards that goal.

> Also, a pointer can be null, while a reference can not.  So
> a reference parameter automatically implies that you can't pass
> a NULL pointer to the function (which makes the function's
> implementation a little bit clearer too), while with a pointer 
> parameter
> you have to document that, and maybe assert it. With a reference, the
> compiler is free to optimize accordingly (assume non-null), while with
> a pointer, you have to use gcc's attribute nonnull if you want that,
> which no one does.
> 
> Also, for std::vector parameters in particular, passing by pointer
> leads to uglier code in the function's implementation, like e.g.,
> 
>  (*vec)[idx] = 0;
> 
> instead of:
> 
>  vec[idx] = 0;
> 
> We end up with a few instances like that in the series, though 
> admittedly
> not that many, otherwise I think I'd add something like:
> 
>   auto &vec = *vec_param;
> 
> at the top of the function and then use vec throughout.

I acknowledge all these downsides.  My opinion (as of today) is that 
they are reasonable trade-offs.  I'm in the camp of "code is written 
once, read many times".  I think that when writing it, it's not 
difficult to look up what the function you are calling expects (can a 
pointer be null).  But when reading a function, if the little & sign can 
save a round trip to the called function's doc, I think it's worth it.

> So in sum, I nowadays tend to look at reference vs parameter more from
> the "pointer: optional, can be NULL", vs "reference: non-optional" 
> angle.
> Though, given GDB's history, we definetely use pointers pervasively
> in function parameters even when they're not supposed to be optional,
> that's for sure.

For "in" parameters, I think it's a no-brainer to do that.

> Maybe we could come up with some middle ground rule, like always
> putting in-out and output parameters last, and stressing to use
> clear symbol names.

Hmmm maybe, I would have to see it in practice to judge how feasible it 
is.

> Anyway, I don't want to block on that discussion (my 1 month round trip
> time not helping! :-D), so I did the change.

Thanks!  Note that I have introduced this rule kind of unilaterally 
after having no response when I proposed it:

https://sourceware.org/ml/gdb-patches/2018-03/msg00145.html
https://sourceware.org/ml/gdb-patches/2018-05/msg00450.html

I thought it was reasonable to do so because it's not a huge commitment 
and easily reversible if people ended up disagreeing.  So it's not as if 
it's set in stone.

Simon


  reply	other threads:[~2018-11-26 17:18 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20181015151115.6356-1-palves@redhat.com>
2018-10-15 15:11 ` [PATCH 5/6] valops.c: Some more gdb::array_view Pedro Alves
     [not found]   ` <4fcedad5-fb6a-11ed-d24f-9e9a3f6339e7@ericsson.com>
2018-11-21 12:48     ` Pedro Alves
2018-10-15 15:11 ` [PATCH 2/6] invoke_xmethod & array_view Pedro Alves
2018-10-17 16:21   ` Simon Marchi
2018-11-21 12:47     ` Pedro Alves
2018-11-26 17:18       ` Simon Marchi [this message]
2018-11-26 17:54         ` Pedro Alves
2018-11-26 18:28           ` Simon Marchi
2018-10-15 15:11 ` [PATCH 3/6] Eliminate make_symbol_overload_list-related globals & cleanup Pedro Alves
2018-10-17 17:25   ` Simon Marchi
2018-11-21 12:47     ` Pedro Alves
2018-10-15 15:11 ` [PATCH 1/6] Use gdb:array_view in call_function_by_hand & friends Pedro Alves
2018-10-17  1:45   ` Simon Marchi
2018-11-21 12:39     ` Pedro Alves
2018-10-15 15:16 ` [PATCH 4/6] C++ify badness_vector, fix leaks Pedro Alves
     [not found]   ` <00ee88d8-857d-d85a-db78-b7a05b62ea0b@ericsson.com>
2018-11-21 12:47     ` Pedro Alves
2018-10-15 15:18 ` [PATCH 6/6] valops.c: Overload resolution code: Rename parameters/locals Pedro Alves

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=43be2c078bbfbcccd8ab3ec181a8813c@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --cc=gdb-patches@sourceware.org \
    --cc=palves@redhat.com \
    --cc=simon.marchi@ericsson.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