News for hackers. Stuff that matters.
Re:Black, the uncompromising Python code formatter, is stable by Mockapapella on Saturday January 29, 2022 @04:18PM (#30130582)
Fuck I love Black -- makes working with other developers amazing once you stick it in a precommit. The quote from Dusty Phillips on the homepage is perfect and has stuck with me for years. I don't have to debate with developers about their individual preferences over what's best because we can just use Black and be done with it.
Re:Black, the uncompromising Python code formatter, is stable by mumblemumble on Saturday January 29, 2022 @04:33PM (#30130741)
I love it even though I dislike quite a few of its formatting rules.

Because the only thing worse than a slightly wonk formatting convention is spending any time at all implementing, arguing about, or otherwise worrying about formatting conventions.

Re:Black, the uncompromising Python code formatter, is stable by unfunco on Saturday January 29, 2022 @05:04PM (#30131009)
Rob Pike's proverbs about Go fmt springs to mind: "Go fmt's style is nobody's favourite, but go fmt is everybody's favourite."
Re:Black, the uncompromising Python code formatter, is stable by ploxiln on Saturday January 29, 2022 @06:12PM (#30131571)
gofmt, on the other hand, gives you a lot more flexibility to wrap lines how you deem appropriate. I like gofmt way more than black.
Re:Black, the uncompromising Python code formatter, is stable by Mawr on Sunday January 30, 2022 @03:42AM (#30135118)
Exactly, gofmt is actually almost perfect. Ironically, it makes Rob's quote less meaningful.

I think gofmt's brilliance comes down to it not being overly pedantic, it doesn't have an opinion on how all code should be formatted, it only fixes what it sees as clear mistakes.

Re:Black, the uncompromising Python code formatter, is stable by javajosh on Saturday January 29, 2022 @04:52PM (#30130921)
Word. I got major pushback trying to fix this problem at my last company, which is (in part) why I'm not there.
Re:Black, the uncompromising Python code formatter, is stable by solarmist on Saturday January 29, 2022 @09:12PM (#30133136)
Yup! This.
Re:Black, the uncompromising Python code formatter, is stable by globular-toast on Sunday January 30, 2022 @07:46AM (#30136690)
Before I introduced Black in my workplace, all code reviews were peppered with stuff like "should be indented", "use single quotes" etc. Made it impossible to talk about the stuff that actually matters.

When introducing it there was some resistance. There's always that one guy who tried it once and it reformatted that perfectly hand-formatted function. My answer to them was suck it up. In a tiny fraction of cases Black might make a suboptimal choice, but the rest of your code is so shit it outweighs this negative by 100x.

Re:Black, the uncompromising Python code formatter, is stable by throwaway894345 on Saturday January 29, 2022 @05:00PM (#30130977)
Agreed. My only grievance with Black was that it was really slow compared to gofmt or rustfmt, but that’s a perennial problem with Python tooling.
Re:Black, the uncompromising Python code formatter, is stable by ambivalence on Saturday January 29, 2022 @05:40PM (#30131313)
This is now improved. Black is compiled with mypyc, should be roughly 2X faster than before.
Re:Black, the uncompromising Python code formatter, is stable by throwaway894345 on Saturday January 29, 2022 @05:50PM (#30131405)
Oh, good to know!
Re:Black, the uncompromising Python code formatter, is stable by Hnrobert42 on Sunday January 30, 2022 @07:30AM (#30136579)
Is there a way to pass hooks with a repo, or do you have to give people instructions about how to install them and hope they do it?
Re:Black, the uncompromising Python code formatter, is stable by goerz on Sunday January 30, 2022 @10:11AM (#30137831)
Not directly, unfortunately. Sometimes you can hook it into the tooling, like the Makefile or the test runner.

Other than that, enforce it in CI: patches that don’t match the code style can’t be merged

Re:Black, the uncompromising Python code formatter, is stable by crlees on Saturday January 29, 2022 @04:22PM (#30130618)
Here here! Same.
Re:Black, the uncompromising Python code formatter, is stable by teddyh on Saturday January 29, 2022 @04:32PM (#30130735)
NB: It’s “Hear, hear”, not “Here, here”.
Re:Black, the uncompromising Python code formatter, is stable by crlees on Saturday January 29, 2022 @04:45PM (#30130859)
Thanks mate.
Re:Black, the uncompromising Python code formatter, is stable by Alex3917 on Saturday January 29, 2022 @04:59PM (#30130968)
The problem I have with it is that, to me, these are super readable:

    x = [ 1, 2, 3 ]
    y = { 'a': 1, 'b': 2 }
Whereas these are not:

    x = [1, 2, 3]
    y = {'a': 1, 'b': 2}
To me, declaring inline lists and dicts without the leading and trailing space makes it more difficult to see what the data structure is, and what it contains. This is especially true when you have lists or dicts declared inline as arguments to functions, e.g.:

    my_function_call([ 1, 2, 3 ])
versus:

    my_function_call([1, 2, 3])
While it's generally not good style to declare data structures inside function calls, at least with the space it's still somewhat apparent what's happening, whereas without the space it just looks like three positional arguments. I love the idea of black, but in practice I'm not going to use something that changes my own code into something that's more difficult for me to read.
Re:Black, the uncompromising Python code formatter, is stable by NavinF on Saturday January 29, 2022 @05:17PM (#30131142)
That’s gotta be a really unpopular opinion since I’ve never seen anyone write “[ 1, 2, 3 ]” instead of “[1, 2, 3]” in the last 10 years or so.

If anything I’m seeing the opposite trend with a few devs writing “[1,2,3]” so that large constant literals don’t overflow the line.

Re:Black, the uncompromising Python code formatter, is stable by LegionMammal978 on Saturday January 29, 2022 @05:51PM (#30131416)
I've recently been working with an old C++ codebase that formats every function call like this:

  Foo<Bar,Baz> value = some.method (1, 2, 3 + 4) ;
  // or with no parameters:
  value = some.method() ;
I've never seen it anywhere else, and it took me a good week to get used to it.
Re:Black, the uncompromising Python code formatter, is stable by goblin89 on Saturday January 29, 2022 @09:57PM (#30133433)
You might not have been writing JavaScript. For objects, spacing properties from curly brackets is pretty much the norm, Prettier does that by default. For lists I see it rarer but I personally use it instinctively for consistency with objects, especially with things like useState destructuring and ([ complex, ...splatting, ...combinations ]) into function calls (which admittedly becomes uncomfortably terse).
Re:Black, the uncompromising Python code formatter, is stable by goblin89 on Sunday January 30, 2022 @06:56AM (#30136271)
And here’re some fresh examples with lists just encountered in the wild: https://developer.matomo.org/guides/tracking-javascript-guid...
Re:Black, the uncompromising Python code formatter, is stable by mech422 on Saturday January 29, 2022 @10:02PM (#30133456)
FWIW, I do "[ 1,2,3 ]". So we exist, perhaps you just haven't bumped into us :-)
Re:Black, the uncompromising Python code formatter, is stable by spiderice on Saturday January 29, 2022 @05:07PM (#30131051)
My only response to this would be “get used to it”.

I used to think 2 space tabs made code harder to read than 4. Now I used 2 spaces and I’ve gotten used to it and can read it fine. And I have no doubt you could learn to read the black formatted code just as easily. I can, since that is how I format my code anyway.

Re:Black, the uncompromising Python code formatter, is stable by bfung on Saturday January 29, 2022 @07:16PM (#30132105)
Agreed - end of day, code formatting is not that important in the context of how end users utilize the product.

Read a lot of different code and programming languages. They all look pretty much the same after awhile, formatting and syntax.

Re:Black, the uncompromising Python code formatter, is stable by solarmist on Saturday January 29, 2022 @09:12PM (#30133130)
I think it just doesn't matter anymore because almost all editors support code folding and visual markers for it, making it MUCH easier regardless of 2 vs. 4.
Re:Black, the uncompromising Python code formatter, is stable by Alex3917 on Saturday January 29, 2022 @08:50PM (#30132956)
> I used to think 2 space tabs made code harder to read than 4. Now I used 2 spaces and I’ve gotten used to it and can read it fine.

I might be in the minority, but it's an empirically testable proposition. Given the thousands of people taking Python quizzes for toptal, triplebyte, etc., it should be possible to see which version results in a lower time to answer or a higher accuracy rate.

Re:Black, the uncompromising Python code formatter, is stable by d12bb on Sunday January 30, 2022 @04:15AM (#30135255)
And that’s the reason everyone should use tabs. You like 2, I like 4, someone else one the team likes 3, 6, 8, whatever? No problem if tabs are used, everyone can set tabstop to their liking!
Re:Black, the uncompromising Python code formatter, is stable by heavenlyblue on Saturday January 29, 2022 @06:38PM (#30131783)
Just learn how to make them readable. I am yet to see a single developer who didn’t manage to learn that. Seriously, the worst types are the ones who think they are experienced enough to think what “is more readable”. It’s all a matter of habit.
Re:Black, the uncompromising Python code formatter, is stable by gmfawcett on Sunday January 30, 2022 @10:42AM (#30138087)
I disagree: your style is less readable than Black's. You've moved the data-structure syntax away from the data, and toward the function-call syntax, causing them to blend together.

This would be more readable -- put the space around the structure, not inside it:

    my_function_call( [1, 2, 3] )
Now we can clearly see the separation of the argument from the function call. But I wouldn't bother switching code formatters just to have this.