Prettier for JS has a simple solution to this specific problem, and that is to follow the code's lead. If the first element of the object or array starts on the same line as the opening bracket, keep it as a single line if it fits. Otherwise, if the first element starts on a new line, put every element on a new line (explode the collection), even if they would all fit on a single line.
Black should consider doing this instead of the open PR to explode collections if there is a trailing comma[0], which feels... wrong.
munge(gidget,
thing1,thing2,thing3);
cmplt(dtypeA,valueA,
dtypeB,valueB);
(Most cases are more subtle (and thus less amenable to "oh, you just need to build a complete static type checker into the formatter") than this, but I wanted an obvious example.) cmplt(
(type_a, value_a),
(type_b, value_b),
)
That's much more clear about the relationship between each pair of values either way, and would get formatted nicely by Black.A better example is a matrix laid out as a grid, which auto-formatters always destroy
mat3x3(
0, a, -b,
-a, 0, c,
b, -c, 0,
) mat3x3(
rowvec3( 0, a, b),
rowvec3(-a, 0, c),
rowvec3( b,-c, 0),
)
Which is probably better, especially with tuples instead of struct{Scalar[3]}. Doesn't fix mat3x3(( 0, a, b),(-a, 0, c),( b,-c, 0))
, though.0: which doesn't have tuples anyway; you could use structs, but it doesn't really work well in context/practice.
1: I don't use python frecently enough to have ready examples of autoformatter stupidity on hand for it.
def cmplt((type_a, value_a), (type_b, value_b)):
return ...
I was sad when they removed argument unpacking in Python 3. I thought it was a really elegant feature of the language.what do you think the result of
def cmplt((type_a, value_a), (type_b, value_b)):
return locals()
is? Now granted, you shouldn't do that, but what you think it is? a dict with 4 keys, right? {'type_a': ..., 'value_b': ...}.That's wrong. It has those four keys, and two more: '.0' and .1', whose values are the packed tuples.
!?!?!
In the example I mentioned, I may sometimes choose to put a small dictionary initialization into a single line if it's just a detail, or may split it into multiple lines if it's important enough for someone reading the code to pay attention to it before going further.
This simply cannot be decided by an automatic formatter without it knowing the code's purpose which, for now, requires a human brain.
Someone mentioned elsewhere that automatic formatters remove the cognitive load of formatting code, but I argue that load is an intrinsic part of programming, because it is (for the most part) the effort of communicating to the next person that comes along.
I have not used Black, but considering it so I am curious about these occasional situations where you want to override auto-formatting
Here's an example snippet by Peter Norvig that is beautifully formatted:
def neighbors4(point):
"The four neighboring squares."
x, y = point
return ( (x, y-1),
(x-1, y), (x+1, y),
(x, y+1))
def neighbors8(point):
"The eight neighboring squares."
x, y = point
return ((x-1, y-1), (x, y-1), (x+1, y-1),
(x-1, y), (x+1, y),
(x-1, y+1), (x, y+1), (x+1, y+1))I you would be fine with 95% of what it does and just want it to ignore the 5% where you care the most, I think the tool could benefit you.
At least that’s how I feel about autoformatting in general.
I think code formatting is very important for readability, and I think many (subtle) choices about how to make a piece of code more readable are very subjective. These types of tools are just incapable to make those choices because they have no concept of intention.
And some rules make this painfully obvious even in less subjective cases. Eg: black formats dictionaries into a single line if they fit one line, which makes nested structures unreadable if they combine bigger and smaller sub-dictionaries.
In the end, I don't have problems with other peoples' formatting choices; as long as they are sufficiently consistent within their own style, which they usually are, I can live just fine with them even when they go against how I'd do things.
I do have problems with tools that reduce code readability for consistency's sake and argue that these provide little benefit, and may even be detrimental to code quality.