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
Formatting with .clang-format #23
Comments
I like the clang-format GitHub Action, which (I think) should provide sufficient information/hints for people who do not use it for development, since the build log is public. As long as it doesn't automatically commit to the PR branch, I'm for it. (Automatic commits prevent us from using the possibility of doing a "rebase and merge", which I prefer when the commit history is clean enough.) |
I briefly looked through the code in search for lines >80 chars long. There will be quite a few lines that will be wrapped, including a number of for-loops and function calls to system APIs. For example: for (nfdfiltersize_t filterIndex = 0; filterIndex != filterCount ; ++filterIndex)
{
// this is the spec to parse (we don't use the friendly name on OS X)
const nfdnchar_t *filterSpec = filterList[filterIndex].spec;
const nfdnchar_t *p_currentFilterBegin = filterSpec;
for (const nfdnchar_t *p_filterSpec = filterSpec; *p_filterSpec; ++p_filterSpec)
{
// ...
}
// add the extension to the array
NSString *filterStr = [NSString stringWithUTF8String:p_currentFilterBegin];
[buildFilterList addObject:filterStr];
} The two for-loops and the call to Even lines like this one will need to be split: nfdnchar_t *tmp_outStr = NFDi_Malloc<nfdnchar_t>(sizeof(nfdnchar_t) * charsNeeded); Rustfmt does seem to have a default max width of 100, but with smaller limits for things like function calls and array literals; Rust is probably the programming language closest to C++ and Rustfmt is the default formatter for Rust so perhaps its choices should bear some weight. Would something like this be possible in ClangFormat? |
AFAIK clang-format does not have anything similar. Yeah, breaking up the for loops on multiple lines is kind of awkward. This specific example could be written like: for (nfdfiltersize_t index = 0; index != filterCount; ++index) {
// this is the spec to parse (we don't use the friendly name on OS X)
const nfdnchar_t* filterSpec = filterList[index].spec;
const nfdnchar_t* p_currentFilterBegin = filterSpec;
for (const nfdnchar_t* spec = filterSpec; *spec; ++spec) {
// ...
}
// add the extension to the array
NSString* filterStr =
[NSString stringWithUTF8String:p_currentFilterBegin];
[buildFilterList addObject:filterStr];
} A little bit terser variable names, but in this situation I think it works. Assignments are often pretty simple to split, I have them set to put the whole expression on a new line, like: nfdnchar_t* tmp_outStr =
NFDi_Malloc<nfdnchar_t>(sizeof(nfdnchar_t) * charsNeeded); There may be other places where it is harder while keeping it readable. |
Admittedly, my variable naming is somewhat verbose at points. If you do want to help, perhaps you could run clangformat and make a PR so that I can take a look? GitHub Actions is triggered for PRs so you could also modify the workflow (cmake.yml) to include a check (or perhaps it might be better to add a separate job for the check instead of appending to the existing jobs, so that we can easily see from GitHub if a PR is only failing because of clangformat). If you're not sure how to set up the workflow, then just ignore this and I'll add it in a separate PR in the future. |
Yes, I'll get to it when I have a bit more time. Haven't learned how to use github actions, mostly because I don't want to become too dependent on github for my personal workflow (propietary and owned by Micro$oft ;^) ) |
Ok, I have used clang-format in https://github.com/ErikPrantare/nativefiledialog-extended/tree/clang-format I think some of the places where loops wrap around could benefit from some general refactoring, Is C++98 compatibility a requirement? |
From a quick glance, there are some things that surprised me:
Also one more thing: Is there anything special about your Looking at the formatted code, I feel that 80 lines is definitely too short, and the formatter is being somewhat too strict about making things align. The formatted version feels somewhat more difficult to read, for me at least, because it is harder to tell where one statement ends and the next one begins. While I like the parts about putting the correct spaces in front of lines and in between tokens, as well as the proper C++-ish left aligned pointers (i.e. You've however convinced me of the usefulness of having an automatic formatter though. In the future, I might play around with clang-format on my own, and try to find a style that I like best. |
No problem, I picked my own .clang-format mostly as a proof-of-concept, Just some motivations for the things you pointed out, to take into consideration when you create your .clang-format: The reason for having the return type on its own line is to always align the function names. int myFunc1();
[[nodiscard]] std::pair<int, std::vector<std::variant<bool, int>>> myFunc2();
//becomes
int
myFunc1();
[[nodiscard]] std::pair<int, std::vector<std::variant<bool, int>>>
myFunc2(); The 80 column restriction can help by being an indicator that whatever you're writing is too complex. So yeah, things to consider. |
Thanks for the tips. Indeed it might be useful to align the function names, though maybe it's less important for when the return type is short. |
It would be nice to use clang-format to format the code base.
One issue brought up would be the raised barrier of entry to contribute.
I found an action that automatically checks(but doesn't format): https://github.com/marketplace/actions/clang-format-lint.
Could be useful for people without clang-format mayhaps?
Also, I vouch for trying 80 columns.
It's the most common width to use, and it's always possible to relax this limit in the future as it's all automatically formatted.
Imo it makes it easier to read from top to bottom :)
The text was updated successfully, but these errors were encountered: