0

We are a small team contributing to our startup project, on a daily basis the code changes and multiple PR are raised daily. The general issue we face while reviewing these PRs is the code formatting changes due to different IDE or the same IDE with different code formatting configurations.

Is there a possible solution that is simple and easy to implement for all?

4 Answers 4

4

This is a common problem, so that there are common solutions.

  • Select a (command-line) tool to auto-format your code. It doesn't really matter what the tool is and what style it enforces, as long as everyone can use it. It should be a single command to re-format all files in your project.
    • For example, in JavaScript I'd look at prettier, in Python I'd look at black, in C++ I'd look at clang-format.
    • Some of these tools offer no configuration options, preempting debates about which coding style your team should agree on. Coding style really doesn't matter that much as long as it's consistent.
  • Integrate that auto-formatting tool into your test suite. Many auto-formatters have a “check” mode that doesn't modify the code and instead warns if the code is ill-formatted.
    • If you have other linters, you may have to configure the linter and formatter to enforce compatible rules – I've often seen conflicts here.
    • Have some CI system that runs the test suite (including linters) for each pull request. Don't bother reviewing PRs if the test suite fails. As much of the PR review as feasible should be outsourced to automated tools – a human review should never have to bother with minutiae like variable naming conventions, line lengths, or comma placement.
  • Encourage developers to add a pre-commit Git hook to run that check before they create a commit.
  • Encourage developers to configure their editors/IDEs so that the IDE's preferred style matches that of your auto-formatting tool. They might also be able to use the selected auto-formatter directly.
    • Basic code style decisions like tab size can be configured via the widely supported EditorConfig file format.
    • Configuring their own editor/IDE is in everyone's own interest so that their PRs can get reviewed – see the previous point about only reviewing PRs with a passing test suite.
4
  • "Encourage" may use a 2x4 cluestick, if needed.
    – gnasher729
    Commented Aug 11, 2022 at 13:26
  • Another option: stop auto-reformatting other peoples' code Commented Aug 11, 2022 at 15:10
  • 1
    @user253751 That is not feasible. For example, I commonly write really trash code, then run auto-format to make it presentable. Can't do that if other parts of the file use an inconsistent style. Also, the concept of “other people's code” only exists in dysfunctional organizations. The codebase should be owned by the team as a whole. It shouldn't matter if I or someone else wrote any part. In any project that I've been in, introducing auto-formatting was a net-positive and streamlined collaboration.
    – amon
    Commented Aug 11, 2022 at 15:40
  • 1
    @amon another option (corrected): stop auto-reformatting code that isn't part of the check-in, as it adds noise to check-ins Commented Aug 11, 2022 at 15:43
3
  • Make your CI system enforce a formatting style
  • Create configuration files for your two most popular IDEs which create that style (change "two" to "one" if you wish)
  • Anyone that wishes to use a different IDE can, but they're on their own when it comes to the formatting style.
0

There is nothing more annoying than your tool making changes behind your backs.

Just make sure that everyone uses exactly the same settings. When you think that's done let developer A take a file, copy all, delete everything, paste. That will usually reapply all the formatting that your IDE wants to apply. It will change your file.

With the saved file, do the same again and pray that there are zero changes. Pass that file to each other developer in turn, let everyone do the same thing, and make sure everyone has their IDE set up identical this way.

If someone has an IDE that cannot be made to play nice, disallow its use. And now where everyone is in the same place, check out every single file, copy/delete/paste, check everything in and you are done.

-1

set your compare tool to ignore whitespace changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.