New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang-format] change format for constructor init and switch/case indent #16701
[clang-format] change format for constructor init and switch/case indent #16701
Conversation
f6e339b
to
b7825d1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for picking this up.
b7825d1
to
b3eea1f
Compare
Ok, updated as per review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
b3eea1f
to
45470ae
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks for me better too.
: m_bBoolArg(bBoolArg), | ||
m_iIntegerArg(iIntegerArg), | ||
m_strSomeText(strSomeText), | ||
m_myOtherClass(myOtherClass) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I like the following style (BreakConstructorInitializers: BeforeComma
) is that in most cases it doesn't touch the previous line to add a comma if another initializer is added. If the majority prefers to keep the comma on the previous line that's okay for me.
MyClass::CMyClass(bool bBoolArg,
int iIntegerArg,
const std::string& strSomeText,
const std::shared_ptr<CMyOtherClass>& myOtherClass)
: m_bBoolArg(bBoolArg)
, m_iIntegerArg(iIntegerArg)
, m_strSomeText(strSomeText)
, m_myOtherClass(myOtherClass)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is "doesn't touch the previous line to add a comma if another initializer is added" really worth this very special formatting style? My personal opinion: It's not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is it "very special"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only my personal feeling, which got inspired from other C++ projects I know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Rechi has a good point about keeping line history clean. However, I'm also with ksooo on this specific case, where I feel the leading comma is ugly enough to warrant an exception to the clean line mantra.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I strongly prefer the leading comma because it provides much cleaner diffs.
The original clang-format was designed to minimize the formatting changes it would do to existing code. I personally prefer the comma before format as it lines up nicely with the colon. It also has the nice property of not touching the previous line when changing something. If there's consensus on this style then go for it 😀 |
👍 |
I can also update for guidelines for cpp14 as we support since #13034, right? |
45470ae
to
268e698
Compare
Changed wording around c++14 usage and updated additional review points raised. |
BTW did you know that git blame has an ignore revision option? I found this out recently and I think it could help getting over the reluctance to clean up the whole repos formatting. It could be done manually or we could add a file that lists cosmetic revisions that one can point to to ignore them |
@Paxxi if the git blame option could be set on a repo level that would be cool. |
268e698
to
c6e1ff6
Compare
It can't afaik. Best we can do is include a file with the commits and then tell people to make an alias or manually use
|
docs/CODE_GUIDELINES.md
Outdated
## 10. Other conventions | ||
### 11.6. Constructor Initialzation Lists | ||
|
||
For lines up to length `ColumnLimit` (in `.clang-format`) everything will be on one line, excluding the braces which must be on the following lines. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now one has to open another file to get the value of ColumnLimit
. Why not add the maximum line length to the code guidelines?
From where to where is the number of characters actually counted to compare with ColumnLimit
?
Will the maximum line length be only used to decide if the everything goes into one line for constructors and ignored everywhere else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it matter to spend effort on the coding guidelines that touches formatting?
Nobody should have to care about reading it or thinking about the formatting.
The columnlimit is currently 100. I find it's a decent line length while still fitting two editors side by side on a 1080p screen for diffs. It does cause issues with trailing comments because clang-format doesn't know how to break a comment and does unnatural formatting to fit the comment.
The columnlimit is from 0-100, it doesn't care about indentation and such and is applied everywhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could state in Section 1 what the columnlimit is and where it's applied. Then the constructor section can remain clean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, definition of line length at start of formatting section now.
c6e1ff6
to
1eeec97
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm mostly indifferent. The current style is (in the cited points) not inferior to me, but I won't stand in the way if people have a strong opinion/preference.
A thing I don't particularly like is the putting everything on one line thing. It makes it a tiny bit harder to see that a constructor initialization is taking place (since it is at the end of the line some times and in the next line other times).
That’s a fair point. This is what I aimed for initially however it’s doesn’t appear to be possible with the current version of clang-format. |
Thanks for that.
Same here, but as @phunkyfish said currently not possible with clang-format. |
Will merge this tomorrow night unless there are any objections. |
I think this PR is the right approach. Thanks @phunkyfish! |
One thing I have to say, unfortunately, which brings a little confused. Only at the moment e.g.: MyClass::CMyClass(bool bBoolArg,
int iIntegerArg,
const std::string& strSomeText,
const std::shared_ptr<CMyOtherClass>& myOtherClass)
: m_bBoolArg(bBoolArg),
m_iIntegerArg(iIntegerArg),
m_strSomeText(strSomeText),
m_myOtherClass(myOtherClass)
{
} becomes by one value removal, changed to: MyClass::CMyClass(bool bBoolArg,
int iIntegerArg,
const std::string& strSomeText,
const std::shared_ptr<CMyOtherClass>& myOtherClass)
: m_bBoolArg(bBoolArg), m_iIntegerArg(iIntegerArg), m_strSomeText(strSomeText)
{
} Would the one-line also refer to the fact that it is only one value and is together with the rest in a row, would be OK (thats why accepted before). MyClass::CMyClass(bool bBoolArg) : m_bArg(bBoolArg)
{
} Only with more than one would be always better like this: MyClass::CMyClass(bool bBoolArg, int iIntegerArg)
: m_bArg(bBoolArg),
m_iArg(iIntegerArg)
{
} |
@AlwinEsch clang-format 9 and newer have a new option, |
This is a good improvement. However something like this would still be possible: MyClass::CMyClass(bool bBoolArg, int iIntegerArg) : m_bArg(bArg), m_iArg(iArg)
{
} Happy to PR this tomorrow. No changes would be required to the docs as this addition will make what's in the doc true always. Nice find @Rechi. |
PR: #16962 |
Description
Proposing a new format for constructor init lists. Colon on newline and break after comma for longer lines.
For Short lines is would look like this having everything on one line (a short line is defined by the
ColumnLimit
in .clang_format, current value is100
:And for longer lines:
Also proposing an indent for switch/case statements:
Also updated wording for c++14 usage.
Motivation and Context
The current format does not read well and is a bit ugly.
E.g.:
Same goes for switch/case:
How Has This Been Tested?
Screenshots (if appropriate):
Types of change
Checklist: