← Back to context

Comment by pornel

2 years ago

Rust is denser than C, but both Rust and C++ can work on similarly high level of abstraction.

It may be just down to rustfmt. It really adds a lot of vertical sprawl. I personally can't stand how much rustfmt makes multi-line code explode.

Default to 80 chars is a travesty IMO, 100 or 128 would be a much better place.

  • rustfmt uses 100-char lines by default (and can be configured to fill more), but that's not the problem with it.

    The problem is that as soon as a whole statement doesn't fully fit on a single line, rustfmt switches from "horizontal" to "vertical" strategy. It then stops caring how much vertical space it takes, inserts line breaks as often as the style allows, and won't try to use the available line width.

    You end up with long runs of finely chopped lines that contain only a single variable or a single call (often with each argument on a separate line too), which looks like having a 20-char line length limit.

    It's either fully oneliner `foo().bar().baz()` or fully vertical

        foo()
        .bar()
        .baz();
    
    

    and you can't have anything in between. It will fight you if you put two calls on the same line.

    • Good, that is the correct way to format code.

      - There are only two strategies and the algorithm to choose between them is trivial for a human to compute. This makes for way better readability, you can reliably predict where the next call/argument is going to be positioned.

      - Refactoring becomes easier - moving an argument is now a simple `Line up` editor action.

      - Source control diffs become more stable across changes.

      ...but it's hard to see the benefits on trivially simple examples like the one you presented. Here's a reformatting I did [1] to illustrate this:

      Original:

             ReturnType<SomeOther<S, TypeConstructor<Nested<S, T, U>, T>>, U, HmmLetsAdd<V, W>>
      

      Reformatted:

          ReturnType<
              SomeOther<
                  S,
                  TypeConstructor<
                      Nested<S, T, U>,
                      T,
                  >,
              >,
              U,
              HmmLetsAdd<V, W>,
          >
      

      Vertically verbose, yes, but that hardly matters. The reformatting gave the code visual structure that's made it easy to understand.

      [1]: https://news.ycombinator.com/item?id=40427723

      2 replies →