[webkit-dev] Using C++ constant local variables in WebKit
Ryosuke Niwa
rniwa at webkit.org
Tue Nov 29 18:42:28 PST 2011
As I stated on the other thread, I'm against using const pointers. However,
I see benefits in using const local variables.
On Tue, Nov 29, 2011 at 6:19 PM, Darin Adler <darin at apple.com> wrote:
>
> I thought we were discussing local variables in general, not pointer-typed
> ones specifically.
>
> * Pros
> - Documents use of variable.
>
> I would say “documents the fact that the variable’s value is not changed”.
> I think it’s overstating things to say it “documents use”.
>
Well, we can always call some function with a pointer to it, and the callee
can const_cast it. Having said that, declaring it explicitly const makes it
clear that it's not intended to be used like that.
For example, when I was writing a patch for
https://bugs.webkit.org/show_bug.cgi?id=69267, I encountered a line:
resolver.setPosition(oldEnd);
at
http://trac.webkit.org/browser/trunk/Source/WebCore/rendering/RenderBlockLineLayout.cpp?rev=101180#L1287
It took me a while before I figured out that oldEnd is never modified and I
can do some optimizations here. Presumably I still have to manually look
for all lines of code that touches oldEnd even if oldEnd was const due to
const_cast. However, it would have at least signaled me the intent.
> - Prevents misuse of variable in a later patch (by a different author)
> through enforcement of const-ness.
>
>
> Prevents one specific type of misuse: Setting the variable to another
> value. And that may not be misuse despite the fact that the original author
> didn’t plan on changing it.
>
Right, but it tells us the intent of the author, and appears to be useful
even if the variable started with prefixes like "old", "original", and
"previous".
- Ryosuke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20111129/1be3036d/attachment.html>
More information about the webkit-dev
mailing list