Bug 73502 - Use const for local variables in KURL::init() and size_t in copyASCII()
Summary: Use const for local variables in KURL::init() and size_t in copyASCII()
Status: RESOLVED WONTFIX
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: David Kilzer (:ddkilzer)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-11-30 17:00 PST by David Kilzer (:ddkilzer)
Modified: 2011-12-09 12:49 PST (History)
7 users (show)

See Also:


Attachments
Patch v1 (3.07 KB, patch)
2011-11-30 17:00 PST, David Kilzer (:ddkilzer)
no flags Details | Formatted Diff | Diff
Patch (3.15 KB, patch)
2011-12-08 15:56 PST, David Kilzer (:ddkilzer)
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description David Kilzer (:ddkilzer) 2011-11-30 17:00:17 PST
Use const pointers in KURL::init() and size_t in copyASCII()
Comment 1 David Kilzer (:ddkilzer) 2011-11-30 17:00:36 PST
Created attachment 117300 [details]
Patch v1
Comment 2 WebKit Review Bot 2011-11-30 17:04:11 PST
Attachment 117300 [details] did not pass style-queue:

Failed to run "['Tools/Scripts/check-webkit-style', '--diff-files', u'Source/WebCore/ChangeLog', u'Source/WebCor..." exit_code: 1

Source/WebCore/ChangeLog:1:  ChangeLog entry has no bug number  [changelog/bugnumber] [5]
Total errors found: 1 in 2 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 3 Darin Adler 2011-11-30 17:05:15 PST
Why the emphasis on “pointers” here? The const is just a local variable. The fact that it’s a pointer is not all that interesting.
Comment 4 Antti Koivisto 2011-12-01 13:30:54 PST
Comment on attachment 117300 [details]
Patch v1

r=me
Comment 5 Antti Koivisto 2011-12-01 13:31:36 PST
The title could be better as it is not pointers only.
Comment 6 David Kilzer (:ddkilzer) 2011-12-08 12:10:32 PST
(In reply to comment #3)
> Why the emphasis on “pointers” here? The const is just a local variable. The fact that it’s a pointer is not all that interesting.

(In reply to comment #5)
> The title could be better as it is not pointers only.

Fixed title.  Will post another patch to incorporate David Levin's feedback on webkit-dev.
Comment 7 David Kilzer (:ddkilzer) 2011-12-08 15:56:56 PST
Created attachment 118482 [details]
Patch
Comment 8 David Kilzer (:ddkilzer) 2011-12-08 15:58:05 PST
(In reply to comment #7)
> Created an attachment (id=118482) [details]
> Patch

This addressed the title and ChangeLog wording fixes highlighted by Antti and Darin, and makes baseCharacters a const pointer per email from David Levin on webkit-dev[1].

[1] <message://%3cCACmjMJQzEqCvpODqk--JSmHnX1BEyQDsYMo_=Q1aLddX4crd8A@mail.gmail.com%3e>
Comment 9 Darin Adler 2011-12-08 16:18:19 PST
Comment on attachment 118482 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=118482&action=review

> Source/WebCore/platform/KURL.cpp:262
> -static void copyASCII(const UChar* src, int length, char* dest)
> +static void copyASCII(const UChar* src, const size_t length, char* dest)
>  {
> -    for (int i = 0; i < length; i++)
> +    for (size_t i = 0; i < length; i++)
>          dest[i] = static_cast<char>(src[i]);
>  }

Why on earth would we use const here?
Comment 10 David Kilzer (:ddkilzer) 2011-12-08 20:34:32 PST
(In reply to comment #9)
> (From update of attachment 118482 [details])
> View in context: https://bugs.webkit.org/attachment.cgi?id=118482&action=review
> 
> > Source/WebCore/platform/KURL.cpp:262
> > -static void copyASCII(const UChar* src, int length, char* dest)
> > +static void copyASCII(const UChar* src, const size_t length, char* dest)
> >  {
> > -    for (int i = 0; i < length; i++)
> > +    for (size_t i = 0; i < length; i++)
> >          dest[i] = static_cast<char>(src[i]);
> >  }
> 
> Why on earth would we use const here?

Because the length parameter should never change within the method.

Is there a technical reason that it shouldn't be used, or is this more of a style/personal preference issue?
Comment 11 Darin Adler 2011-12-09 10:33:51 PST
(In reply to comment #10)
> (In reply to comment #9)
> > (From update of attachment 118482 [details] [details])
> > View in context: https://bugs.webkit.org/attachment.cgi?id=118482&action=review
> > 
> > > Source/WebCore/platform/KURL.cpp:262
> > > -static void copyASCII(const UChar* src, int length, char* dest)
> > > +static void copyASCII(const UChar* src, const size_t length, char* dest)
> > >  {
> > > -    for (int i = 0; i < length; i++)
> > > +    for (size_t i = 0; i < length; i++)
> > >          dest[i] = static_cast<char>(src[i]);
> > >  }
> > 
> > Why on earth would we use const here?
> 
> Because the length parameter should never change within the method.
> 
> Is there a technical reason that it shouldn't be used, or is this more of a style/personal preference issue?

It is a style issue.

Almost all arguments of almost all functions are not changed within the function. For example, this function could be written like this:

    static void copyASCII(const UChar* const src, const size_t length, char* const dest)

This would indicate that src, length, and dest are all unchanged in the implementation of the function. I think that is harder to read than the existing function. And I am not sure why length stands out from the other arguments as worthwhile to call out as unchanged. Does that help people reading the code? It seems unnecessarily cluttered to include const all the time. If const was the default, then I would agree that we should leave all arguments and local variables const except for the ones we plan to modify, but since it’s not the default I prefer that we not put const everywhere.

Stating const when there is something unclear and interesting to be clarified or emphasized is a different story. We’ve been discussing cases like that, for example I think you and Antti have been highlighting a case where there is a local variable in a large complex function and it might be useful to emphasize the point that it is never changed.

For a two line function, declaring the arguments const seems not to clarify sufficiently to be worth the clutter.

I’d like some sort of rule, even if a subjective one, about when we use it. It doesn’t have to be a firm machine-enforceable rule, but I do want consistency and not decisions made differently based on different programmers’ whims. I find it easier to work on WebKit because we use on a consistent basic coding style and adhere to it most of the time.

The rule I suggested until now was “don’t use const for the values of arguments or of local variables” -- we’d still use const for members and of for the types the references refer to and pointers point to -- but I believe you wrote this bug at least in part because you are dissatisfied with that rule. So I’d like to make sure we have a new one.
Comment 12 David Kilzer (:ddkilzer) 2011-12-09 12:46:19 PST
(In reply to comment #11)
> (In reply to comment #10)
> > Is there a technical reason that it shouldn't be used, or is this more of a style/personal preference issue?
> 
> It is a style issue.
> 
> Almost all arguments of almost all functions are not changed within the function. For example, this function could be written like this:
> 
>     static void copyASCII(const UChar* const src, const size_t length, char* const dest)

Ahh!  I agree that would detract the readability of the code.

> I’d like some sort of rule, even if a subjective one, about when we use it. It doesn’t have to be a firm machine-enforceable rule, but I do want consistency and not decisions made differently based on different programmers’ whims. I find it easier to work on WebKit because we use on a consistent basic coding style and adhere to it most of the time.
> 
> The rule I suggested until now was “don’t use const for the values of arguments or of local variables” -- we’d still use const for members and of for the types the references refer to and pointers point to -- but I believe you wrote this bug at least in part because you are dissatisfied with that rule. So I’d like to make sure we have a new one.

Another way to look at this is that if KURL::init() were split up into smaller functions/methods, the need for making local variables const would greatly decrease since one could read the entire block of code to see how all of the variables are used (and thus which ones should not be changed because they're used for calculating lengths).

Or to put it yet another way, my desire to add const to local variables is a really a code smell that probably means the method is too large to easily understand/read.

However, refactoring KURL::init() is not at the top of my priority list, so I'm just going to close this bug as RESOLVED/WONTFIX.

Thanks for the explanation.
Comment 13 David Kilzer (:ddkilzer) 2011-12-09 12:46:37 PST
Comment on attachment 118482 [details]
Patch

Clearing flags and marking obsolete.
Comment 14 Ryosuke Niwa 2011-12-09 12:49:38 PST
(In reply to comment #10)
> (In reply to comment #9)
> > (From update of attachment 118482 [details] [details])
> > View in context: https://bugs.webkit.org/attachment.cgi?id=118482&action=review
> > 
> > > Source/WebCore/platform/KURL.cpp:262
> > > -static void copyASCII(const UChar* src, int length, char* dest)
> > > +static void copyASCII(const UChar* src, const size_t length, char* dest)
> > >  {
> > > -    for (int i = 0; i < length; i++)
> > > +    for (size_t i = 0; i < length; i++)
> > >          dest[i] = static_cast<char>(src[i]);
> > >  }
> > 
> > Why on earth would we use const here?
> 
> Because the length parameter should never change within the method.
> 
> Is there a technical reason that it shouldn't be used, or is this more of a style/personal preference issue?

This is a clear cut case where the constness of a variable is self-evident (the function is only 2 lines!), and there's almost no benefit in explicitly marking it as const.