Skip to content
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

Thoughts on improving and enforcing coding style #1474

Open
5 of 6 tasks
AMS21 opened this issue Oct 26, 2023 · 3 comments
Open
5 of 6 tasks

Thoughts on improving and enforcing coding style #1474

AMS21 opened this issue Oct 26, 2023 · 3 comments
Labels

Comments

@AMS21
Copy link
Contributor

AMS21 commented Oct 26, 2023

Here are some general ideas to improve coding style.

Implementing these would generally be split into 2 steps.

  1. Running the option on the entire code base.
  2. Creating a GitHub workflow which run on every commit/pull request to verify it in the future.

To address one problem immediately. Mass formatting commits tend to mess up the git blame. But fortunately for this very problem the --ignore-rev option was created which allows you to ignore certain commits and even load these from a file.

And GitHub supports this commit ignoring out of the box see this.

So all of these mass formatting commits would be added to the .git-blame-ignore-revs file and should not show up in the git blame. For local use you might have to set it up manually like this.

git config --local blame.ignoreRevsFile ".git-blame-ignore-revs"

What are the community thoughts on this?

I have implemented all of these options in past so it shouldn't be a big problem.

@Xottab-DUTY Xottab-DUTY added Code Quality Developer Experience Engine's programmer experience labels Oct 26, 2023
@Xottab-DUTY
Copy link
Member

Xottab-DUTY commented Oct 26, 2023

Agree on everything, except details about clang-format'ing the code:
We had a discussion in #278 about the code style modification, and I also suggested to change code style for the entire code base to snake_case. The team didn't like the latter, and first kinda did end in nothing.

So, now, we have a very permitting mixed code style: since the original code already has mixed snake_case and PascalCase and Hungarian notation, we allow every developer to do the same. Generally, sticking to the code style of the part of the code, or the code style of the file you are working on. But developer is also allowed to stick to the his personal preference. It just should not like an alien to the existing X-Ray code base =)
Because of that, clang-format config should be modified to allow everything above and only enforce few things:

  1. Always using spaces (except for comments, ignore tabs in C/C++ comments)
  2. Opening bracket { should be always on a new line
  3. Removing trailing whitespace (for the GitHub action case, it should allow it for existing code, but disallow it for new code)
    (I will added more items to this list later as I recall them)

Replacing tabs with spaces

This was done in 2017 with clang-format, but since then a little number of tabs could remain or even be added. It's good to check the code again.

Source files should be encoded with UTF-8

The same. All sources were converted to UTF-8 somewhere in 2017, but it's good to check it again and even enforce that using GitHub workflow.

@Xottab-DUTY
Copy link
Member

@OpenXRay/xray-contributors

@AMS21
Copy link
Contributor Author

AMS21 commented Oct 28, 2023

So now the hard part: clang-format

We could run clang-format on the entire code base but I don't think this would be a good idea the diff is absolutely massive and a lot of the original code is actually formatted in a nice readable way just in a bit of a different style.

Instead I would suggest running the clang-format-diff tool on commits and pull requests which would only check lines that were actually changed. So we would only enforce our formatting rules on new code and leave the old as is.

Now for the actually formatting rules there are some which I liked to at least discuss.

swtich (value)
{
    case A: {
        ...
        break;
    }
}

Is this intended?

Also BeforeWhile should should probably also be set to true.
Also theres SplitEmptyFunction, SplitEmptyRecord and SplitEmptyNamespace which when set to false allows them to be put on a single line.

  • BreakAfterAttributes
    I've seem code formatted like this already but can also be set to leave which would basically ignore this option.

  • BreakConstructorInitializers
    There is BreakConstructorInitializersBeforeComma inside the .clang-format but this is not a real option.

  • CompactNamespaces
    Looking at the code I think this should be set to false

  • EmptyLineAfterAccessModifier
    From what I've seen either Nerver or Leave

  • EmptyLineBeforeAccessModifier
    Very similar I guess either Always or Leave

  • FixNamespaceComments
    I don't have a strong opinion either way. It can be nice to see which closing brace belongs to which namespace but I don't think we use namespaces that much anyways.

  • IndentPPDirectives
    Can be used to easier see where which #if, #else, etc. block ends. But I don't think this is a very common style inside the code base. Should be set together with PPIndentWidth which should then probably also 4.

  • InsertBraces
    I think this should be set to false to align with the current code style. This allows code like this:

if (x)
    return;

instead of

if (x)
{
    return;
}

and also consider setting RemoveBracesLLVM which would not allow the second example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: In Progress
Development

No branches or pull requests

2 participants