• FizzyOrange@programming.dev
      link
      fedilink
      arrow-up
      0
      ·
      26 days ago

      It’s because it uses a global optimisation algorithm designed to minimize line breaks. In practice that turned out to be not at all what you want.

      Most autoformatters use the Prettier algorithm which is both way simpler and much better. The algorithm is basically “can this AST nodes fit all on one line? If so do that. If not spread its children over multiple lines and recurse into them.”

      It gives way nicer looking, more predictable, and more diff friendly results, even if it might use more lines of code.

      Probably the most common thing is how lists of things like function arguments or arrays are handled when they don’t fit on one line. Prettier will do this:

      auto a = foo(
        X,
        Y,
        Z
      );
      

      But clang format will do something like

      auto a = foo(X, Y,
                   Z);
      

      Basically they optimised for the wrong metric.

      In theory you can configure it to be better but I’ve found that anything that strays too far from the LLVM style is super buggy because the developers aren’t using it, so I wouldn’t recommend changing too much.