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
Cleanup issues of PR 8228 #8259
Conversation
doc = docs[idx/2]; | ||
} |
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.
Just for curiosity, why are curly braces necessary for single-line ifs? Up to now i did not add them because I saw no need and also because most of FC's code don't have them.
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.
Forums discussion of clang-format: https://forum.freecadweb.org/viewtopic.php?f=10&t=72183&p=629874
The gist of it is that it's widely considered a best practice due to some very famous bugs that would have been prevented had this formatting strategy been used.
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 the pointer. In this discussion I miss the info what can be harmful of omitting the braces. I don't mean when there are comments and then a single statement, but just a single statement alone, so
if (blah)
do something;
else if (blub)
do another thing;
...
Especially when having "else if" this way improves in my opinion the readability, especially on smaller screen since it saves lines.
Besides this, I recently reverted the automatic addition of braces, not because I am opposed of requiring the braces, but it was a nightmare to work with the VC IDE with this. At first, the addition seemed convenient but on longer coding sessions VC is not able to do this right when copy/pasting code around. Often it adds a closing brace despite it is already there. This cost a lot of time since the compilation error messages don't help. So one has to carefully re-read to find additions braces. On the other hand sometimes it does not add the closing brace. Still a mystery for me. What about you, are you on VC 2022 as well? When you enabled automatic brace insertion, and code around for a while, do you encounter problems? I had a look today in forums but cannot find a way to use a clang file and only overwrite one of its setting.
I see no workaround because I cannot turn of the formatter because then the code would not be formatted at all. And having a custom clang file only for me is error-prone (different PCs, keeping it up to date.)
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.
from what you are saying it sounds like it's messing up the file while you are coding, that does sound annoying and error prone, can't you run clang format just before committing?
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 the pointer. In this discussion I miss the info what can be harmful of omitting the braces
my first guess is that later someone might forget to put braces when trying to add more lines inside the if statement, I've certainly done so myself 🤦♂️
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.
In my opinion, format-as-you-type is more trouble than it is worth, in any IDE. My strategy is to literally not worry about the format at all while I am coding, and just make the code look how I like it to look. Then, before committing, I select and auto-format (or in the case of Python, use Black as a pre-commit hook).
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.
"In this discussion I miss the info what can be harmful of omitting the braces."
The most famous case is the "Apple goto fail". There is also the python mantra "explicit is better than implicit". From a maintainability standpoint, anything that reduces the chances of misinterpreting code is a good thing.
Interesting to see the experimental clang-format spoken of like it is official. Nice! Just a couple of cautions though: It was previously pointed out that broadscale reformatting should be avoided for now to avoid further complicating the pending toponaming merge. Format will eventually (I believe) be included in CI. After that local issues in the IDE become irrelevant. Do whatever you want! The point is that local issues are temporary and should not block achieving the best final outcome. |
I dont use VC but CLion and VSCode both instantly alert mismatched braces when I mess up. Is VC2022 is not similar? Odd these get through to compile. |
The point is that it is not experimental. Since VC 2019 the clang file of a project is the default formatter. You cannot disable this, except of
|
At the very least you could identify your problem and maybe others have something to solve. Give some example etc. So far all we've heard is you don't like what happens. |
Where did you hear this? I even approved the change to add auto brace insertion. It took me a lot of time to realize what is going on, why I got frequent and hard to understand compilation errors. Thus I reverted the particular change to be productive again and wrote about this in the commit. I know, this was not according to our rules. But following the rules would have meant that I would not be productive for days, have to start a debate, bring examples etc. And as you see others were affected too. Thus it was quicker to "just resolve" the issue. |
You need to seriously revise your workflow. If you are using a moden IDE there is no way a format problem should remain undiscovered until compilation. |
So Visual Studio 2022 is not modern? Exactly because I knew that I will get statements like this: |
If you have some if-else if checks on the same level and any of it requires braces then by most style guidelines it's strongly recommended to add curly braces to all of them. Example:
should become
If you have an if/else if/else with a comment before the actual statement then it's strongly recommended to add curly braces.
should become
If you have nested if-checks then for the outer if's curly braces are strongly recommended.
should become
or (preferred)
berniev's rule is even simpler because he recommends to always use curly braces. Unfortunately a lot style guidelines say something else where for single line statement the curly braces should be dropped. |
Many thanks. What you describe is how most of FC's code is written and also how I try to code. The problems with changing the rules are:
This gives you a lot of changes in PRs that have nothing to do with the actual intended code change, making it harder for the reviewers or you have to undo the automatical additions to keep the diffs small. Both ways consume spare time either of the coder or the reviewer.
All in all, the whole issue consumes a lot of our spare time, for the discussion and also to handle the code. Therefore I don't see why we need to change or style when it gives us no benefit, but additional work. We should better use our time to focus to write good code (good to understand for others, less memory consumption, fast etc.) |
I don't use it a lot any more but sounds like the IDE is not very smart. Maybe you should open a new thread in the forum to discuss this issue. |
Re single line statements, a slight clarification:
and
Should the first version be allowed? I would argue it should not be allowed as it hides content and makes debugging more difficult. If it is allowed then perhaps braces are superfluous. The second case it seems has concensus for always brace. |
I tried Visual Studio 2022 for Mac, but deleted it. No support for C++. I think VS priority was always C#. |
Does CLion do a good job with autoformatting? I'm getting ready to migrate (I really dislike using C++ in Xcode). |
CLion is, like other Jetbrains products I have used for years, very slick. Given the huge hours we spend at the screen, a hundred bucks or whatever is peanuts. I continue to be amazed how good they are. BUT CLion has some indexing/speed issues with FreeCAD - I'm talking sometimes wait 45 minutes with all cores flat out. This renders much of the refactoring assistance (one of my favourite features) unusable. I spent heaps on extra RAM - made no difference. I have several reports in. Maybe on your M1 you'll do better. |
related: #8232 |
@chennes clarification re CLion: The formatting is not entirely 'auto', more 'helpful'. Still hit format button from time to time. |
In my opinion not. As I also argued in the forum, I don't see the need to change the formatting when > 95% of your code use already the linebreak.
Where do you see this? |
Thank you for creating a pull request to contribute to FreeCAD! Place an "X" in between the brackets below to "check off" to confirm that you have satisfied the requirement, or ask for help in the FreeCAD forum if there is something you don't understand.
Please remember to update the Wiki with the features added or changed once this PR is merged.
Note: If you don't have wiki access, then please mention your contribution on the 1.0 Changelog Forum Thread.