RESOLVED FIXED70285
Style guide should mention the preference of index over iterator
https://bugs.webkit.org/show_bug.cgi?id=70285
Summary Style guide should mention the preference of index over iterator
Ryosuke Niwa
Reported 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.
Attachments
70285 (1.60 KB, patch)
2011-10-17 17:01 PDT, Ryosuke Niwa
no flags
Updated per comment (1.81 KB, patch)
2011-10-19 16:06 PDT, Ryosuke Niwa
darin: review+
Ryosuke Niwa
Comment 2 2011-10-17 17:01:11 PDT
Ryosuke Niwa
Comment 3 2011-10-18 14:50:39 PDT
Any reviewer?
Darin Adler
Comment 4 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.
Ryosuke Niwa
Comment 5 2011-10-19 16:06:19 PDT
Created attachment 111690 [details] Updated per comment
Ryosuke Niwa
Comment 6 2011-10-21 12:50:46 PDT
Ping reviewers again
Darin Adler
Comment 7 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.
Ryosuke Niwa
Comment 8 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.
Ryosuke Niwa
Comment 9 2011-10-21 16:17:55 PDT
Note You need to log in before you can comment on or make changes to this bug.