Definition order should match declaration order?

37 views
Skip to first unread message

Evan Stade

unread,
Jan 18, 2022, 5:09:41 PM1/18/22
to Chromium-dev, cxx
Hi all,

I think we generally recommend that the definition order for class methods (in a .cc file) should match declaration order for that class's methods (in the .h file, often). However I can't actually find this guidance in the Google or Chromium C++ style guide. Is it in there somewhere?

I see no reason not to follow this rule when adding new code (although if it's not in the style guide, I guess that means it's up to the author), but is it worth it to reorder existing code to fix this issue? Moving methods around obfuscates git history so I'm not sure it's as worth it in that situation.

WDYT?

-- Evan Stade

dan...@chromium.org

unread,
Jan 18, 2022, 5:13:32 PM1/18/22
to Evan Stade, Chromium-dev, cxx
On Tue, Jan 18, 2022 at 5:07 PM Evan Stade <est...@chromium.org> wrote:
Hi all,

I think we generally recommend that the definition order for class methods (in a .cc file) should match declaration order for that class's methods (in the .h file, often). However I can't actually find this guidance in the Google or Chromium C++ style guide. Is it in there somewhere?

It was removed from the Google style guide some years ago.
 

I see no reason not to follow this rule when adding new code (although if it's not in the style guide, I guess that means it's up to the author), but is it worth it to reorder existing code to fix this issue? Moving methods around obfuscates git history so I'm not sure it's as worth it in that situation.

Grouping things that work together may make more sense than by visibility, so I think it depends.
 

WDYT?

-- Evan Stade

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAO4XGS_kwiF_R2hgzxoCoYh%2BRXUFFPymJSjD9xNvhHy3SSUZAg%40mail.gmail.com.

K. Moon

unread,
Jan 18, 2022, 5:15:46 PM1/18/22
to dan...@chromium.org, Evan Stade, Chromium-dev, cxx
I agree with danakj@, and I would also advocate for not reordering existing files unless the owners of the file intended to maintain that ordering (and any existing violations were merely an oversight). I value preserving blame history over consistency between declaration and definition order.

Reply all
Reply to author
Forward
0 new messages