Which indentation style do you all prefer for expanding brackets?
Posted by sasson10@reddit | Python | View on Reddit | 56 comments
I just got curious as to what the more popular style of indentation is when expanding brackets into multiple lines.
This applies to everything, but I'll use a list as an example, the base list will be l = [item1, item2, item3]
- Going down a line and 1 indent level forward from the very first time item
l = [
item1,
item2,
item3
]
- Keeping the first item on the definition line, then adding spaces so everything lines up
l = [item1,
item2,
item3]
These are the main 2 I've seen in my time, if you have any others I'd be interested to see them as well
Balzac_Jones@reddit
This'll probably get me no end of hate, but I like a mix of the two:
My brain has always disliked the closing parentheses/bracket outdented on the line below.
commy2@reddit
The point of the trailing comma was that you get a smaller diff when adding items to the collection later. Putting the closing bracket on the same line as the last item defeats that purpose.
Balzac_Jones@reddit
Yeah, I actually didn't mean to include the trailing comma in my post. Conflicting reflexes...
Beanesidhe@reddit
(1) but I am not going to waste energy on fighting anyone or anything using (2) - or other variants - about formatting preferences.
Specialist_Solid523@reddit
You forgot to include:
karius85@reddit
First. The second is terrible.
differentiable_@reddit
Idk, just let ruff or black run on pre-commit.
JayCallaha@reddit
This is the way
CIS_Professor@reddit
PEP 8 suggests this (my preference):
or this:
ElHeim@reddit
Those are options concerning the position of the closing delimiter.
But if you check a few examples above you'll find that alignment with the opening delimiter is ok (as far as PEP 8 is concerned):
knightress_oxhide@reddit
auto format everything before a new commit
sasson10@reddit (OP)
I didn't even know auto formatting was a thing until making this post lol
TheMcSebi@reddit
First one, but I usually type the comma after the last item as well
Noobfire2@reddit
Don't ever come up with your own formatting rules. Every project in existance should just use
rufffor formatting and if absolutely necessary, adjust rules slightly to your preference.It will format to the first variant, btw.
JamzTyson@reddit
That's way too dogmatic and absolute for my taste.
Farlo1@reddit
We can haggle about which specific formatter/style, but when would you even NOT want automatic and consistent formatting? Even in a single-person project it frees your mind to not worry about it and think about the actual code.
ProsodySpeaks@reddit
Only formats to first one if doesn't fit on one line or has trailing comma.
Well mine does maybe it's a configuration thing
Schmittfried@reddit
FIFY
inspectorG4dget@reddit
My preference is this:
This way, adding new elements doesn't include the line with the closing bracket as a changed line in the fit diff.
I also use the same philosophy for function/method arguments, so if I'm calling a.b(1, 2).c(3, 4), aligning to the open brace/bracket/paren (rather than the standard 4-space indent) indicates which function call/scope I'm in.
This is highly opinionated and too many devs disagree with me. But for myself, I made a custome flake8 linter to catch this: https://pypi.org/project/flake8-multiline-equals/
quantinuum@reddit
Don’t choose formatting style because (manual) formatting isn’t something you should be doing. Get ruff or black in.
GameCounter@reddit
Use ruff or black.
1 but include the trailing comma. It makes adding a new item at the end of the list a one-line diff instead of two.
i_like_tuis@reddit
Use ruff and worry about more important things.
RevolutionaryRip2135@reddit
Formatting is an important thing… it helps you present meaning… having auto format on / ruff on commit is way to hell imho.
daredevil82@reddit
spoken like a true fundamentalist believer that MY WAY IS THE ONE TRUE WAY
JamzTyson@reddit
When contributing to a project: Whichever style the project uses.
When using a code formatter (such as black): Whichever the formatter gives me.
For my own projects when not using an auto-formatter: I prefer the second.
gahbageked@reddit
I just use black or some other form of code formatter that follows popular language conventions. I don't want to think about how to format code, I want to use that time to think about more important things.
In a professional setting, this is even more important. If you're working on a codebase with a team, it's much easier to use a formatting tool than waste time debating on personal stylistic preferences of code formatting. This also works to rein in the psychopaths who don't format or arrange their code at all (they exist).
TheBB@reddit
The first one, which is also the one black or ruff will use.
marr75@reddit
Black or ruff will add a trailing comma (for good reason)
ottawadeveloper@reddit
I like (1) for all iterables because it's easy to add or remove items.
I use (2) for function parameters because it feels so weird otherwise.
big_data_mike@reddit
The first one. Easier to comment items out of the list for when I rerun the code
assumptionkrebs1990@reddit
If a list (its iterator) can not be written in one line (which I prefer if ever possible) I use style 2, I don't see the appeal to use two lines just for the brackets.
PRADA_G616@reddit
Ya see that's a plus plus though! Typo That's why it's always good to spend valuable time reading formats esp on discord. I don't give a f*** obviously, as long as I can reformat the malicious spyware into matter most for the 5th time. Thread is MINT!
zDibs@reddit
Same. Either a single line or style 2.
Just find it easier to read the second variant over the first. Never understood why people love the first variant. 🤷♂️
HardlyAnyGravitas@reddit
Same here. I think the first is just copying the style from other, less 'readable' languages.
I think people forget what coding languages are for. They have one purpose and one purpose only - to make it easier for humans to read and maintain the machine code that processors actually process.
I suppose with the advent of AI, we could end up with codebases written entirely in machine code. That will really be the end of human coders that people are constantly predicting...
sausix@reddit
Readability especially when you have multiple levels of indentation. It's not worth to just care for less lines of code.
Alkanen@reddit
Black. That is all.
stevenjd@reddit
Black. Just say no.
eavanvalkenburg@reddit
The first one with a comma at the end of item3 is what I would always use (unless it fits on one line), the reason is that that gives you much cleaner diffs when committing, while the second one will be two lines changed, this is also the reasoning why tools like Ruff use that style.
0kSwede@reddit
And commenting. I can comment individual items from that list without affecting the construction of that list.
ProsodySpeaks@reddit
Not to mention add or remove any item (including first and last) with one command
lunatuna215@reddit
Yup
ProsodySpeaks@reddit
With ruff format the trailing comma makes it do the first example (even if would fit within line length) or without trailing comma it'll leave it on one line if fits in line length.
I quite like having the ability to decide case by case just by adding or removing the comma
aplarsen@reddit
This is one of the things that makes me happy about Python's forgiveness on trailing commas. Clean diffs.
usrlibshare@reddit
Simple answer: I don''t give a damn.
Whenever I save my file, the auto-formatter runs, and takes care of this. I have more important things that occupy my mindspace.
gofmtstarted this for me. Discussions about this or that formatting are pointless. It's literally bikeshedding.Schmittfried@reddit
It’s not bikeshedding, readability is important. Formatters like gofmt are a great step forward because they outsource that discussion. But it’s good that some people have them.
Rainboltpoe@reddit
Plenty of things are important and also completely automated, and thus not worth spending time on.
RevolutionaryRip2135@reddit
I am not a psychopat so no. 1 as it is readable and two newlines cost next to nothing - use ctrl f12 to navigate structure of file in idea
l_dang@reddit
This
aminoy77@reddit
Style 1 every time. The closing bracket on its own line means you can add/remove items and the diff only shows the actual change, not a closing bracket moving around.
Style 2 breaks the moment you rename the variable — suddenly all the alignment is wrong and you're fixing whitespace instead of code.
DoubleAway6573@reddit
the one that reduce lines in a diff for quicker commits reviews.
RockOnTheWater@reddit
The first one but with trailing commas
shinitakunai@reddit
Two, I don't like having unneeded extra lines in the code. Been using it 16 years (so yeah fighting lot of people and linters 🥲)
billsil@reddit
Depends on the length. For long lists, I’ll put ~80 characters on the line. For absurdly long lists/atrings, I’ll just put it on one line. I’m clearly not going to read it, so I don’t want it to take up 1000 lines.
I always put a comma after the last item though. It avoids bugs when you add a new row.
mireqB@reddit
aloobhujiyaay@reddit
Honestly depends on team style, but for solo work I go with style 1
gfranxman@reddit
I dunno, I can’t remember the last time I wrote or formatted code manually.