Bug 70285 - Style guide should mention the preference of index over iterator
Summary: Style guide should mention the preference of index over iterator
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKit Website (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Ryosuke Niwa
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-10-17 16:43 PDT by Ryosuke Niwa
Modified: 2011-10-21 16:17 PDT (History)
3 users (show)

See Also:


Attachments
70285 (1.60 KB, patch)
2011-10-17 17:01 PDT, Ryosuke Niwa
no flags Details | Formatted Diff | Diff
Updated per comment (1.81 KB, patch)
2011-10-19 16:06 PDT, Ryosuke Niwa
darin: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ryosuke Niwa 2011-10-17 16:43:06 PDT
Per discussion on webkit-dev, we should mention that we prefer using index over iterator to go though items in a Vector.
Comment 2 Ryosuke Niwa 2011-10-17 17:01:11 PDT
Created attachment 111351 [details]
70285
Comment 3 Ryosuke Niwa 2011-10-18 14:50:39 PDT
Any reviewer?
Comment 4 Darin Adler 2011-10-18 15:02:03 PDT
Comment on attachment 111351 [details]
70285

For the index case we chose to evaluate size every time through the loop. For the iterator case we chose to cache the end iterator. That’s an arbitrary difference not related to index vs. iterator. I think a local variable named size would be a better way to do things.

I don’t think this makes it clear enough that there are data structures where iterators are present, but indexing is not. Nor does it answer the question of why we offer iterators if indexing are preferred.

Also, this doesn’t contain any of the rationale; perhaps it should.
Comment 5 Ryosuke Niwa 2011-10-19 16:06:19 PDT
Created attachment 111690 [details]
Updated per comment
Comment 6 Ryosuke Niwa 2011-10-21 12:50:46 PDT
Ping reviewers again
Comment 7 Darin Adler 2011-10-21 14:57:44 PDT
Comment on attachment 111690 [details]
Updated per comment

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

> Websites/webkit.org/coding/coding-style.html:712
> +<li>Prefer index over iterator in Vector iterations for a terse, easier-to-read code, and to avoid accessing freed memory

I don’t see the end of this <li> element.

It’s overselling this technique to say that it avoids accessing freed memory, because using a too high index can still access freed memory.

> Websites/webkit.org/coding/coding-style.html:717
> +size_t frameViewSize = frameViews.size();

frameViewSize is not the right name here. Maybe frameViewsSize or frameViewsCount or size or count.
Comment 8 Ryosuke Niwa 2011-10-21 15:10:06 PDT
(In reply to comment #7)
> (From update of attachment 111690 [details])
> View in context: https://bugs.webkit.org/attachment.cgi?id=111690&action=review
> 
> > Websites/webkit.org/coding/coding-style.html:712
> > +<li>Prefer index over iterator in Vector iterations for a terse, easier-to-read code, and to avoid accessing freed memory
> 
> I don’t see the end of this <li> element.

It's the pattern used throughout the file. I should probably correct that once for all.

> It’s overselling this technique to say that it avoids accessing freed memory, because using a too high index can still access freed memory.

Okay, I'll remove that.

> > Websites/webkit.org/coding/coding-style.html:717
> > +size_t frameViewSize = frameViews.size();
> 
> frameViewSize is not the right name here. Maybe frameViewsSize or frameViewsCount or size or count.

Oops, typo. It was supposed to be frameViewsSize but I guess frameViewsCount sounds better. Will change before landing.
Comment 9 Ryosuke Niwa 2011-10-21 16:17:55 PDT
Committed r98166: <http://trac.webkit.org/changeset/98166>