News for hackers. Stuff that matters.
Re:Black, the uncompromising Python code formatter, is stable by captainmuon on Saturday January 29, 2022 @04:34PM (#30130755)
Personally, I don't understand the appeal of opinionated code formatters. If you don't want to discuss "taste" questions, then just don't, it doesn't matter what option you pick. If it is an important question, then you should debate it.

I think you can convey meaning with subtle code style differences. Empty lines to delineate blocks. Single quotes if the string is a keyword, double quotes if it is for the user. Spaces around operators to make an expression clearer. I spend a minute or two before I commit to make the code tidy (linter and then manual tweaking) and would expect that from everybody on my team - it takes often less time than rebasing and picking good commit names, for example.

But even though it annoys me slightly when I encounter Black (or god forbid, Go) used in a project, I know a lot of people like it a lot, and it is good to have the choice. So congrats to the release! :-)

Re:Black, the uncompromising Python code formatter, is stable by mumblemumble on Saturday January 29, 2022 @04:41PM (#30130819)
I think the general consensus of many people, myself included, is that it doesn't matter what the specifics are, as long as they're being applied consistently. Beyond that, it's a question of optimizing the cost/benefit ratio.

The problem with most things that have to be applied manually is that they're also applied inconsistently. It's like Hungarian notation. Over time, it becomes pure noise: you can't rely on it, especially when reading someone else's code, so it's safer to just ignore it. At which point, the convention's cost/benefit is approaching infinity, because its denominator nearly zero. So I'm not sure I care if the absolute cost is small. It's still an almost complete waste of effort.

The only place I've ever seen manual conventions applied consistently enough to be useful in a sustainable way is when someone's carved out their own private silo of code that is mostly only touched by them.

Re:Black, the uncompromising Python code formatter, is stable by perrygeo on Saturday January 29, 2022 @06:02PM (#30131504)
> convey meaning with subtle code style differences

Why? This is exactly why we need autoformatters :-) if you're on a team, you should be conveying meaning with variable names, comments, and structure. If it doesn't show up in the syntax tree, it doesn't have meaning. Consistency always wins because it elevates everyone's ability to read the code.

There's more to it than the social aspect though: putting effort into manually moving around characters in a text editor is not the best use of mental bandwidth. My experience (and that of many I know) is that formatters help you code faster with less bugs by letting you focus on the higher-level structure of the problem NOT on the minutiae of where each punctuation mark should live. If your code is syntactically correct, the autoformatter will make it look "right" and you don't have to think about consistent formatting at all. If it's not syntactically correct, you set your editor to autoformat on save and you get immediate feedback. So it's not just about consistency, it's also a productivity aid.

I understand having strong opinions about the "right" code style. Personally, I dislike the double quotes in black. So I type single quotes and let black do its thing on save. What I type does not need to be what I commit (so long as they both compile to the same AST)! I don't want to waste time debating formatting details in a PR, and I certainly don't want to interrupt my coding flow to manually perform the labor of shuffling ascii chars around when there is fully automated solution. I can't think of a single scenario where my personal style preference would outweigh the significant advantages I've seen from autoformatters.

Re:Black, the uncompromising Python code formatter, is stable by tayo42 on Saturday January 29, 2022 @07:54PM (#30132433)
> If it doesn't show up in the syntax tree, it doesn't have meaning.

Why did you use paragraphs to write your comment then? People read the code thats written.

Re:Black, the uncompromising Python code formatter, is stable by solarmist on Saturday January 29, 2022 @09:23PM (#30133218)
Auto formatters support paragraphs, but they don't support I'm going to give this paragraph 2 blank lines because it's particularly important/complicated/blah.
Re:Black, the uncompromising Python code formatter, is stable by mumblemumble on Saturday January 29, 2022 @11:14PM (#30133954)
Typographic standards typically don't, either. Fortunately, there's more than one way to convey emphasis.

Also, single functions that are long enough to contain something analogous to paragraphs are arguably not Pythonic in the first place.

Re:Black, the uncompromising Python code formatter, is stable by solarmist on Saturday January 29, 2022 @11:22PM (#30134003)
For paragraph I just mean a series of 1 or more lines separated by a blank line vs no blank lines.
Re:Black, the uncompromising Python code formatter, is stable by Mawr on Sunday January 30, 2022 @03:49AM (#30135143)
Are there two blank lines between these parts of the code for a reason or did the person who wrote this code make a mistake?

If that part of the code is special for whatever reason, add a comment saying why.

Re:Black, the uncompromising Python code formatter, is stable by tayo42 on Saturday January 29, 2022 @09:48PM (#30133375)
Oh interesting thanks! I couldn't find it in the docs before
Re:Black, the uncompromising Python code formatter, is stable by solarmist on Saturday January 29, 2022 @10:54PM (#30133823)
Yeah, I don't think it's mentioned because it feels like supporting spaces between words, I’d guess. Just so fundamental a functionality it's assumed.
Re:Black, the uncompromising Python code formatter, is stable by Qub3d on Sunday January 30, 2022 @02:15AM (#30134795)
I think the argument is not that structure should be dispensed, or that it doesn't matter, rather that a given style convention is fungible, and we should probably all just pick something good enough and get on with it, because the code will execute regardless of whether we use " or ' around our strings.

Spend some time considering the language standards, but then ship it for god's sake.

See also from Peter Norvig[0]:

* Get involved in a language standardization effort. It could be the ANSI C++ committee, or it could be deciding if your local coding style will have 2 or 4 space indentation levels. Either way, you learn about what other people like in a language, how deeply they feel so, and perhaps even a little about why they feel so.

* Have the good sense to get off the language standardization effort as quickly as possible.

[0]:https://www.norvig.com/21-days.html

Re:Black, the uncompromising Python code formatter, is stable by tayo42 on Sunday January 30, 2022 @11:12AM (#30138399)
I actually had this formatter thing happen at work. We have code that generates more python code. Now the quotes are changed and we need escapes because a rule it being applied. Escapes are harder to read and look messier.
Re:Black, the uncompromising Python code formatter, is stable by mbeex on Sunday January 30, 2022 @07:47AM (#30136697)
> So I type single quotes and let black do its thing on save.

It is the one small grip, I have with black. C&P for example some string-indexed dict element as argument into a f-string sometimes produces duplicate double quotes if it gets into black's hands beforehand. But in the end, black outweights all these minor things. This particular issue also is probably better considered as a tooling task for editors, IDEs etc.

Re:Black, the uncompromising Python code formatter, is stable by monsieurbanana on Saturday January 29, 2022 @04:42PM (#30130830)
> If you don't want to discuss "taste" questions, then just don't, it doesn't matter what option you pick.

Exactly, it doesn't matter what option you pick, as long as everybody picks the same option. Enter opinionated code formatters, because otherwise how do you do it?

Re:Black, the uncompromising Python code formatter, is stable by colechristensen on Saturday January 29, 2022 @05:19PM (#30131158)
> otherwise how do you do it?

Only reject obviously quite messy code, and only rarely allow formatting rewrites. Learn that anybody’s strong preferences for a specific style or discussion of such aren’t demonstrably beneficial for the bottom line of the business or customers.

Re:Black, the uncompromising Python code formatter, is stable by darkerside on Saturday January 29, 2022 @06:15PM (#30131601)
This is a recipe for people spending time cleaning up files, back and forth, ad infinitum. Or, just letting the stubborn person win.
Re:Black, the uncompromising Python code formatter, is stable by colechristensen on Saturday January 29, 2022 @10:37PM (#30133701)
My point is you let people have their different styles, reject changes on lines where code isn't changed, and coach the style zealots to calm down. Changing a couple of bytes in a file? Don't change the style. Writing something new? Figure out what's best. Rewriting a big chunk? Make reasonable formatting choices.

Some programmers are zealots who have a hard time working with others, the solution isn't always to force the matter or acquiesce to the squeakiest wheel. A strong organization should be able to enable people do have their style and disable people from forcing their opinions on everybody or wasting everybody's time with low-value debate.

Re:Black, the uncompromising Python code formatter, is stable by crehn on Sunday January 30, 2022 @02:17AM (#30134805)
> Changing a couple of bytes in a file? Don't change the style. Writing something new? Figure out what's best. Rewriting a big chunk? Make reasonable formatting choices.

This is exactly the type of arguably useless effort we don't want to spend any time on. Having to think about what is "reasonable", "allowed", or "best" is non-negligible cognitive burden for both the writer and reviewer/reader.

Re:Black, the uncompromising Python code formatter, is stable by The_Colonel on Sunday January 30, 2022 @07:01AM (#30136321)
When I use black or other formatter, I have to exert cognitive effort to anticipate how the formatter will format the code so that I can avoid the inevitable stupid formatting decisions.

I often then tweak the code in a way that is not necessarily better, it's merely rendered better by black.

It just feels like I'm fighting against the tool half of the time.

Re:Black, the uncompromising Python code formatter, is stable by dragonwriter on Saturday January 29, 2022 @11:23PM (#30134006)
> My point is you let people have their different styles, reject changes on lines where code isn't changed, and coach the style zealots to calm down

Code with style changes, especially within a file, is harder to follow than almost any reasonable consistent style. And adopting a common formatter with common settings is a lot less work than “reject changes on lines where code isn't changed, and coach the style zealots to calm down” even if it was neutral in readability.

Re:Black, the uncompromising Python code formatter, is stable by paiute on Saturday January 29, 2022 @11:20PM (#30133986)
I think it's a tell when people get so fussy about style, if you can't get past non functional differences how are you going do on differences of functional differences. "my code is spaghetti and performs poorly, but at least I used a formatter!"
Re:Black, the uncompromising Python code formatter, is stable by onphonenow on Saturday January 29, 2022 @05:25PM (#30131200)
The reason I like black is the most opinonated folks have WEIRD opinions.

You see that here.

Things like a = [ 1, 2, 3 ] when 90% of the world is a = [1, 2, 3]

These folks will spend hours / days try to force weird approaches.

Sometimes they are obviously wrong. But it's annoying to have to fight over it. Something like black saves you the hassle and the WEIRD local coding rules someone comes up with if you are on a project with them.

Re:Black, the uncompromising Python code formatter, is stable by lolc on Saturday January 29, 2022 @08:33PM (#30132798)
I once came in on a project where one dev used "func( arg )" and the other used "func (arg)". They would reformat each other's lines when they changed something, so the diff had a lot of whitespace changes. The two were not on speaking terms any longer, so I was tasked with cleaning up the mess.

First step was to run an autoformatter. I used stock presets so neither of the two would think the other was favoured in the cleanup.

Re:Black, the uncompromising Python code formatter, is stable by paiute on Saturday January 29, 2022 @11:23PM (#30134005)
If I worked there I would always do func( arg) or func(arg ) just because.
Re:Black, the uncompromising Python code formatter, is stable by onphonenow on Monday January 31, 2022 @12:37AM (#30144946)
Haha. Brilliant :) The extra spaces look so weird to me, but yeah, folks have strong opinions.
Re:Black, the uncompromising Python code formatter, is stable by hnarayanan on Sunday January 30, 2022 @03:07AM (#30135018)
Chaotic evil
Re:Black, the uncompromising Python code formatter, is stable by patrick451 on Saturday January 29, 2022 @10:55PM (#30133826)
I just don't see why you would care if this extra space is in there or not. If they want to stick it in there, be my guest. I can read either just fine, even if, gasp, it's not 100% consistent through the whole codebase.
Re:Black, the uncompromising Python code formatter, is stable by onphonenow on Monday January 31, 2022 @12:36AM (#30144923)
Will you change to match their code style? Great.

However, if another person ALSO has a strong opinion about this - nightmare.

Re:Black, the uncompromising Python code formatter, is stable by darkerside on Saturday January 29, 2022 @06:16PM (#30131611)
Terrific point I never thought of. There's definitely a power law, where 80% of the arguing time is spent on the 20% dumbest shit.
Re:Black, the uncompromising Python code formatter, is stable by IgorPartola on Saturday January 29, 2022 @06:29PM (#30131723)
I think I figured out why: it’s easy for everyone to have an opinion about simple things. Complex things that are worth arguing about take more knowledge/experience and at that point fewer folks are qualified to argue about them.
Re:Black, the uncompromising Python code formatter, is stable by darkerside on Saturday January 29, 2022 @07:25PM (#30132172)
Re:Black, the uncompromising Python code formatter, is stable by Closi on Saturday January 29, 2022 @04:54PM (#30130932)
> Personally, I don't understand the appeal of opinionated code formatters. If you don't want to discuss "taste" questions, then just don't, it doesn't matter what option you pick.

I think the appeal is that it avoids the discussions entirely (i.e. if it doesn't matter what option you pick, why even have the option? Particularly if these options will mean you divert away from the standard python style guide, which has been trying to standardize these choices for years so that codebases have similar rules.)

i.e. if your org has inconsistent python formatting between different independently-operating teams, it's easier to enforce PEP 8 than start a load of pointless arguments about what good looks like.

Re:Black, the uncompromising Python code formatter, is stable by ferdowsi on Saturday January 29, 2022 @04:40PM (#30130801)
> If you don't want to discuss "taste" questions, then just don't, it doesn't matter what option you pick. If it is an important question, then you should debate it.

I'm personally glad to see Python development come to the same conclusion enforced by Go's opinionated tooling.

That is: style questions are almost always unimportant to producing value, but teams still waste excessive time on them because of the engineering tendency to bikeshed triviata.

Re:Black, the uncompromising Python code formatter, is stable by Shacklz on Saturday January 29, 2022 @05:06PM (#30131036)
> I think you can convey meaning with subtle code style differences.

Yes that's very true, but, and now comes the big but - if you're working in a project with dozens and dozens of other devs, you will necessarily see all kinds of weird formatting. And this is where an opinionated formatter shines. It doesn't matter how many people work on your project, what the churn-rate is or if the guy that just started yesterday already read the code-formatting guidelines or not - the formatter will just take care of it and remove all hassles surrounding that topic. And this only at the price of opinionated formatting which I may or may not like - a price that I likely have to pay either way, because there's no way that everybody shares the same opinion about formatting in the first place.

I recommend to give opinionated formatters a try, if you haven't already. I know plenty of folks who were initially against it but eventually came around it because it made life a lot easier: No more discussions about formatting on PRs, no more tabs vs space wars, everything's already settled and automatically taken care of.

Re:Black, the uncompromising Python code formatter, is stable by dragonwriter on Saturday January 29, 2022 @05:18PM (#30131151)
> Personally, I don't understand the appeal of opinionated code formatters.

Opinionated code formatters mean that opinionated coders with different preferences on a team don't produce (or at least produce less) noise in diffs.

Re:Black, the uncompromising Python code formatter, is stable by erik_seaberg on Saturday January 29, 2022 @05:52PM (#30131428)
I only reformat code I am rewriting, for that reason. I've never seen a tool that was very good at bending its own rules to minimize these accidental changes (e.g., let a function call go to column 83 to keep it on one line).
Re:Black, the uncompromising Python code formatter, is stable by darkerside on Saturday January 29, 2022 @06:14PM (#30131591)
Sounds like you actually are very opinionated about your coding style, so it's both totally understandable and yet ironic that you'd begrudge that of others. I've tried hard to internalize that opinions are like assholes, everybody has one and they all stink. My own included.
Re:Black, the uncompromising Python code formatter, is stable by qbasic_forever on Saturday January 29, 2022 @06:28PM (#30131713)
> Empty lines to delineate blocks.

Use comments to describe those blocks and their meaning, don't just assume everyone is thinking exactly the same as you are about the code organization (hint, they probably aren't). Where you see a bunch of logically separated blocks of operations I might be new to the codebase and just see a bunch of unnecessary whitespace. Describe what's being organized and why in comments.

Re:Black, the uncompromising Python code formatter, is stable by tayo42 on Saturday January 29, 2022 @07:45PM (#30132356)
does it not allow you do something like this to set up variables?

  def foo(a,b,c):
    var x = 40
    var y = None
  
    bar()
    
    return x * b
I think the blocks of code make it more readable, some variables are set up, some work is done, then it returns something
Re:Black, the uncompromising Python code formatter, is stable by throwaway894345 on Saturday January 29, 2022 @05:04PM (#30131011)
I can stop discussing taste issues, but it doesn’t prevent others from debating what the proper style for my PR ought to be, meanwhile I can’t merge my PR.
Re:Black, the uncompromising Python code formatter, is stable by rightbyte on Sunday January 30, 2022 @02:03AM (#30134742)
Ye I don't understand it either. Why would you want some automated Whitespace Hitler telling you what to do. You can't even select between tabs and spaces for indentation.