Guidance around const by-value function parameters #1358

Open
opened 2026-01-30 22:23:34 +00:00 by claunia · 6 comments
Owner

Originally created by @dlong11 on GitHub (May 25, 2019).

Clarification is needed around const by-value function parameters. I noticed in a lot of the code, by-value function parameters are marked const. Some code doesn't follow this rule though.

Examples in interactivity/win32/Clipboard.cpp

void Clipboard::Copy(bool fAlsoCopyHtml)

void Clipboard::StoreSelectionToClipboard(bool const fAlsoCopyHtml)

In the CppCoreGuidelines
Con.1: By default, make objects immutable

They have an exception
Exception
Function arguments are rarely mutated, but also rarely declared const. To avoid confusion and lots of false positives, don't enforce this rule for function arguments.

void g(const int i); // pedantic

What is the team's stance on this?

This issue is just a reminder when we create the styleguide in #890 to include something around the team's rule.

Originally created by @dlong11 on GitHub (May 25, 2019). Clarification is needed around const by-value function parameters. I noticed in a lot of the code, by-value function parameters are marked const. Some code doesn't follow this rule though. Examples in interactivity/win32/Clipboard.cpp void Clipboard::Copy(bool fAlsoCopyHtml) void Clipboard::StoreSelectionToClipboard(bool const fAlsoCopyHtml) In the CppCoreGuidelines [Con.1: By default, make objects immutable](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#con1-by-default-make-objects-immutable) They have an exception Exception Function arguments are rarely mutated, but also rarely declared const. To avoid confusion and lots of false positives, don't enforce this rule for function arguments. > void g(const int i); // pedantic What is the team's stance on this? This issue is just a reminder when we create the styleguide in #890 to include something around the team's rule.
claunia added the Issue-DocsProduct-MetaArea-CodeHealth labels 2026-01-30 22:23:34 +00:00
Author
Owner

@dlong11 commented on GitHub (May 25, 2019):

I just noticed another minor issue with the posted code from Clipboard.cpp. The StoreSelectionToClipboard method doesn't follow NL.26: Use conventional const notation.

@dlong11 commented on GitHub (May 25, 2019): I just noticed another minor issue with the posted code from Clipboard.cpp. The StoreSelectionToClipboard method doesn't follow NL.26: Use conventional const notation.
Author
Owner

@adiviness commented on GitHub (May 25, 2019):

Generally we put const wherever we can, including function parameters. The exception to that is if the param is going to be modified by the function, it makes more sense to modify the copied by value variable than to make an internal copy to modify.

I’m of the opinion that we should follow the conventional const notation.

@adiviness commented on GitHub (May 25, 2019): Generally we put const wherever we can, including function parameters. The exception to that is if the param is going to be modified by the function, it makes more sense to modify the copied by value variable than to make an internal copy to modify. I’m of the opinion that we should follow the conventional const notation.
Author
Owner

@binarycrusader commented on GitHub (May 30, 2019):

I have to agree with @adiviness, I apply const liberally everywhere I can. const declares intent and the compiler enforces that intent. I know not everyone loves it, but for me it's proved helpful.

@binarycrusader commented on GitHub (May 30, 2019): I have to agree with @adiviness, I apply const liberally everywhere I can. const declares intent and the compiler enforces that intent. I know not everyone loves it, but for me it's proved helpful.
Author
Owner

@dlong11 commented on GitHub (May 30, 2019):

I have no opinion on this. I just want to make sure this kind of stuff is added to the style guide if this is the team's rule. Having this expectation documented helps when writing and reviewing code. It is so much easier to say "The style guide suggests using const on by-value function params ....." Prevents bikeshedding. You guys can discuss whether this is a rule, recommendation, preference, etc... 😄

@dlong11 commented on GitHub (May 30, 2019): I have no opinion on this. I just want to make sure this kind of stuff is added to the style guide if this is the team's rule. Having this expectation documented helps when writing and reviewing code. It is so much easier to say "The style guide suggests using const on by-value function params ....." Prevents bikeshedding. You guys can discuss whether this is a rule, recommendation, preference, etc... 😄
Author
Owner

@dlong11 commented on GitHub (Jun 5, 2019):

I missed linking this to the Coding Style issue. #890

@dlong11 commented on GitHub (Jun 5, 2019): I missed linking this to the Coding Style issue. #890
Author
Owner

@zadjii-msft commented on GitHub (Aug 22, 2023):

I'm tempted to just close this and fold it back up into "we should write some coding style docs"...

@zadjii-msft commented on GitHub (Aug 22, 2023): I'm tempted to just close this and fold it back up into "we should write some coding style docs"...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#1358