On Tuesday 2017-05-09 11:58 +0200, Emilio Cobos Álvarez wrote:
> I think references help to encode that a bit more in the type system,
> and help reasoning about the code without having to look at the
> implementation of the function you're calling into, or without having to
> rely on the callers to know that you expect a non-null argument.
> 
> Personally, I don't think that the fact that they're not used as much as
> they could/should is a good argument to prevent their usage, but I don't
> know what's the general opinion on that.

I have two general concerns about references:

 (1) When reading the calling code that calls a function taking a
     non-const reference, it's not obvious that the function might
     mutate the value, particularly in code that uses references
     rarely, or for types that are rarely passed as references.

 (2) Code that mixes functions that take references and functions
     that take pointers, for the same time, is a mess, because you
     constantly have to worry about whether to write * or &.  Thus,
     in Gecko, we generally use pointers or references per-type.
     XPCOM interfaces are passed by pointer.  String classes are
     passed by reference.  etc.

I think per-type conventions for using references versus pointers
helps with both (1) and (2), and I consider it part of Gecko style
even if it's not well-documented.

I'm also not sure how much references help with enforcing
non-null-ness.  People will just write a "*" whether or not their
existing code guarantees a non-null pointer; at least that was my
experience in the early days of Gecko.  Perhaps we have better code
review now... but I'm somewhat skeptical of that given that I think
we've been relying on tests more and review less?

-David

-- 
𝄞   L. David Baron                         http://dbaron.org/   𝄂
𝄢   Mozilla                          https://www.mozilla.org/   𝄂
             Before I built a wall I'd ask to know
             What I was walling in or walling out,
             And to whom I was like to give offense.
               - Robert Frost, Mending Wall (1914)

Attachment: signature.asc
Description: PGP signature

_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to