7 ms·
Black is a simple tool. It tries to implement a single code style well. It's not configurable. We tried YAPF before and could never roll it out for everybody
by ambivalence 8y ago
Black is a simple tool. It tries to implement a single code style well. It's not configurable.
We tried YAPF before and could never roll it out for everybody. I even contributed the "facebook" style to the tool. There were a few reasons why YAPF didn't work out for us but the most important were:
- YAPF would at times not produce deterministic formatting (formatting the same file the second time with no changes in between would create a different formatting); Black treats this as a bug;
- YAPF would not format all files that use the latest Python 3.6 features (we have a lot of f-strings, there's cases of async generators, complex unpacking in collections and function calls, and so on); Black solves that;
- YAPF is based on a sophisticated algorithm that unwinds the line and applies "penalty points" for things that the user configured they don't like to see. With a bit of dynamic programming magic it arrives at a formatting with the minimal penalty value. This works fine most of the time. When it doesn't, and surprised people ask you to explain, you don't really know why. You might be able to suggest changing the penalty point value of a particular decision from, say, 47 to 48. It might help with this particular situation... but break five others in different places of the codebase.
- vjeux 8y agoAnother issue is that yapf uses an algorithm that is quadratic, so you end up with code you actually see in practice take seconds to minutes to format. The authors have been adding specific workarounds for some cases but it's a general issue with the approach: - https://github.com/google/yapf/issues/264 https://github.com/google/yapf/issues/264 - https://github.com/google/yapf/issues/39 https://github.com/google/yapf/issues/39 Black algorithm doesn't explode in such way. It's also a lot faster overall which makes it possible and reasonable to enable a good format on save experience.
- aagimene 8y ago> It's not configurable. Except for the line length: > if you're paid by the line of code you write, you can pass --line-length with a lower number.
- ambivalence 8y agoYeah, I wasn't willing to die on that hill, especially that I'm introducing a new default value that wasn't popular before.
- klodolph 8y agoWhen I read that line in the documentation, it hurt a little. I'm going to be honest here. I did not feel good with someone saying that an 80-column limit is just there to pad my paycheck, rather than an informed decision I made. I realize that it was tongue in cheek, but it still felt bad.
- sametmax 8y agoIf that hurt you, you need bigger problems in life.
- deleted 8y ago[deleted]
- klodolph 8y agoWhy are you being so dismissive? It seems like you are making this into some kind of contest to see who has bigger problems. I am just being honest, and I think I am being helpful because other people will also react negatively to the way the documentation is written. I support the project and want it to be successful, providing feedback like this achieves progress towards that goal, in my estimation.
- ubernostrum 8y agoI like a line length rule because I use an editor that can show me multiple files side-by-side, and keeping things to a certain length ensures that I can productively do that. And my terminal happens to be around 240 characters wide for the font size I use and the size of my laptop's screen, which means if I limit to 80 characters or less I can snugly fit three files, or if I set to 100 I can get two with some breathing room. Plus, if a line really is going over those lengths, then it's often a code smell: maybe I've got code that's too complex and ending up deeply-nested, or maybe I've got functions or methods taking way too many arguments, and hitting a line-length rule will warn me about that.
- omoikane 8y agoI do like Black's line wrapping rules of keeping things on separate lines to minimize diffs. I really hate auto formatting tools that tries to compress lines vertically through binpacking techniques that produce unaligned code.
- gvx 8y agoRE: non-deterministic formatting: I'm not familiar with YAPF, but what kind of behaviour does it have? Like if you run it on two copies of the same file you might get different results, or that it's not idempotent (i.e. sometimes YAPF(YAPF(source)) != YAPF(source))?