[webkit-dev] Do we have a style preference about const member functions?

Peter Kasting pkasting at chromium.org
Wed Jun 1 13:20:28 PDT 2011


On Tue, May 31, 2011 at 11:31 PM, Maciej Stachowiak <mjs at apple.com> wrote:

> The following would be valid but would require us to cast away const all
> over the place and would therefore in my opinion be a net negative:
>
>  const Node* previousSibling() const { return m_previous; }
>
> And what's in our code now violates the intent of const, and so to me at
> least is clearly a bug:
>
>  Node* previousSibling() const { return m_previous; }
>

I agree that these are both bad.

Well one big problem right now (just from scanning the core DOM classes) is
> that we have a lot of clearly broken use of const. We could (a) leave it
> as-is, (b) remove the incorrect use of const, or (c) deploy proper
> const-correctness. it seems like you are against (b), but I cannot tell if
> you advocate (a) or (c).
>

Misusing const is IMO worse than not using const.  I would be in favor of
removing cases like the last one you cite above, as well as working to
remove const_casts, at least where they indicate problems.

It would be nice to make things more properly const-correct, and make
callers use const accessors when possible (e.g. use the const form of tree
iteration when walking a tree merely to read data and not write it), so I
don't think we should forbid good use of const, but it's also time-consuming
to retrofit.

Perhaps we could at least encourage const-correctness for newly-written
classes?  By this I mean both adherence to the logical-constness rules you
stated earlier as well as not adding non-const accessors and members unless
needed -- otherwise it's easy to just err on the side of never using const
anywhere.

I also think we should not discourage people from using "const" on
variables.  I've gotten review comments in the past that have asked me to
remove const qualifiers on locals, as well as sometimes on arguments and
members, and I think this is a mistake.  "We aren't const-correct elsewhere
in WebCore" is not a good reason to forbid const.

PK
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20110601/1f2bda88/attachment.html>


More information about the webkit-dev mailing list