Hello,
I wanted to note that since
r853633 (from danakj@ in Feb 2021)
the Chromium C++ Style Guide "recommends using a reference instead of a pointer when it cannot be null". This is relatively new guidance and so we have quite a bit of old code that defaults to using pointers as parameter types (partly because
until 2020 using non-const references in parameter types was discouraged by the Google C++ Style Guide). There is probably no need to proactively convert the old code (given that changing parameter types leads to sprawling, cascading changes), but in some of my CLs I plan to tweak the parameter types where it seems beneficial.
A recent example where the new guidance has been helpful to me is
https://crrev.com/c/3221594 and avi@
suggested that I communicate this more broadly. That CL starts to ask callers of BuildMenu / ShowContextMenu / HandleContextMenu to pass a reference - this is helpful because 1) it clearly documents that the argument cannot be null and 2) forces the callers to make an explicit dereference when converting a pointer to a reference (hopefully thoughtfully analyzing whether such pointer can ever be null). The second point was particularly helpful for
https://crrev.com/c/3221594, because it helped us confidently assess that in all the call chains there is always a non-null frame available. The first point was in theory achievable with pointers (documenting non-null-ness via comments and/or DCHECKs) but references express the intent more explicitly (leaning on the language and compiler support).
(I also note that pointer-vs-reference is related, but mostly orthogonal to the question of whether const-references can be used or not; for now `const` is rarely supported in //content/public API and therefore non-const pointer and references are typically used)
Thanks,
-Lukasz