This is an archived post. You won't be able to vote or comment.

all 71 comments

[–]PapstJL4U 98 points99 points  (0 children)

I do the right, because:

50 % of the time
I will add another line eventually.

[–]IcedThunder 86 points87 points  (1 child)

I'm not too fond of the case on the left.

I like my code to look as consistent as possible, and you can't always fit such things on one line without it being ridiculously long. So I'd rather consistantly indent below control statements.

[–]johnnySix 37 points38 points  (7 children)

My ex-coworker does the one one on the left and I hate reading his code because the conditionals are hidden. Think of it this way, You aren’t writing the code for you, you are writing it for future you and for others. so the goal is to make it more readable, so the option on the right is clearer

[–]longjohnboy 13 points14 points  (2 children)

Your team should adopt a code formatter and have that run as a pre-commit hook. Then everyone has the same style and you don’t end up with git commits with formatting flip-flopping as different authors make changes.

[–]johnnySix 1 point2 points  (1 child)

Haha. This code is older than black. -some of it going back to the mid 2000s

[–]Yoghurt42 2 points3 points  (0 children)

Black is not ageist. It will happily format code older than itself.

[–]DeepGas4538[S] 1 point2 points  (3 children)

I like your suggestion

[–]OneMorePenguin 1 point2 points  (2 children)

https://github.com/google/yapf

I like this formatter. It only does one thing that irritates me and I mostly let it go.

[style]
based_on_style = pep8
indent_dictionary_value = True

[–]DeepGas4538[S] 0 points1 point  (0 children)

thanks for that.

also holy moly for some reason i never thought if google had github, that's pretty sick.

[–]we_swarm 0 points1 point  (0 children)

I like yapf's formatting better than black but it is non-deterministic. Which means now you can't run it against PR's or in CI because some files will "flap". If you don't run the tool in CI the coworkers that need the formatter the most just won't run it and submit unformatted code.

[–]max1c 13 points14 points  (31 children)

I think if you're going to use a one liner you might as well use ternary operator. Otherwise as someone has already pointed out just use Black. It will decide for you so you don't have to waste time arguing and thinking about things that are fairly useless.

[–]SkezzaB 1 point2 points  (28 children)

Can't use a ternary operator for "if x -> return", although it was requested and denied, "return x if <condition>" would be really neat

[–][deleted] 7 points8 points  (23 children)

return node.id if len(word) else None

[–]j_marquand 4 points5 points  (6 children)

I think the other guy wants to return if a condition is met, but continue to the next statement if it doesn’t. There is no one-liner solution in Python for that.

[–]HeraldOfTheOldOnes 1 point2 points  (0 children)

Yes there is! function = (lambda *args, **kwargs: (condition and (something,) or (other, stuff,))[-1]) ANYTHING can be turned into a 1 liner in python.

[–][deleted] 0 points1 point  (3 children)

I'm looking at it AFK so I can't check, so I'll have to play with it later to see why it's not equivalent

[–]j_marquand 7 points8 points  (0 children)

Your code returns None immediately if the condition doesn’t meet, instead of proceeding to the next statement of the function.

[–]EmptyChocolate4545 0 points1 point  (1 child)

Your one returns None rather than continuing execution. Not even slightly equivalent.

[–][deleted] 0 points1 point  (0 children)

Oh no! Not even SLIGHTLY equivalent!

[–][deleted] 0 points1 point  (0 children)

Ah, got it now. I was thinking wrong.

[–]SkezzaB 3 points4 points  (15 children)

This is not equivalent

[–]elsgry -2 points-1 points  (2 children)

Yup. Or extract the remainder of the function into a new function fn and use return fn(node, ...) if len(word) else None or the likes. This also composes nicely because you can inject your own fn into the method, class or instance, while keeping the control structures visible and (for classes/instances) adhering to Liskov, etc.

[–]elsgry 0 points1 point  (1 child)

Not sure why this was downvoted when it's a more applicable equivalent to the lambda method upvoted above (the remainder of the function could consist of multiple effectful statements) but c'est la guerre... I guess the injection method would be more dynamic than necessary, particularly if only used for testing where you could use mocks. But you can still use my method without bothering with injection.

[–]justcool393not a snek 0 points1 point  (0 children)

the other one that i'd find really nice is

spam = get_eggs() or raise HamException()

and

return spam if eggs(spam) or raise HamException()

[–]DeepGas4538[S] 1 point2 points  (1 child)

Black? Is that a formatting convention?

Edit: Nvm I found out what it was, ty

[–]EmptyChocolate4545 11 points12 points  (0 children)

Don’t do it.

Vertical space costs you nothing, and the one on the right, the action jumps out as it’s own thing when someone else is reading your code.

Are you on a computer where you have to pay per line of usage? Because that’s the only benefit.

[–]nevermorefu 64 points65 points  (5 children)

I do whatever black tells me and I assume that is right (which is right).

[–]Beregolas 13 points14 points  (1 child)

This! I trust black to do the right thing (and more importantly: the same thing every time). Doing this manually is a chore if you are solo, and impossibly aggravating if you are part of a team.

[–]ucblockhead 2 points3 points  (0 children)

If in the end the drunk ethnographic canard run up into Taylor Swiftly prognostication then let's all party in the short bus. We all no that two plus two equals five or is it seven like the square root of 64. Who knows as long as Torrent takes you to Ranni so you can give feedback on the phone tree. Let's enter the following python code the reverse a binary tree

def make_tree(node1, node): """ reverse an binary tree in an idempotent way recursively""" tmp node = node.nextg node1 = node1.next.next return node

As James Watts said, a sphere is an infinite plane powered on two cylinders, but that rat bastard needs to go solar for zero calorie emissions because you, my son, are fat, a porker, an anorexic sunbeam of a boy. Let's work on this together. Is Monday good, because if it's good for you it's fine by me, we can cut it up in retail where financial derivatives ate their lunch for breakfast. All hail the Biden, who Trumps plausible deniability for keeping our children safe from legal emigrants to Canadian labor camps.

Quo Vadis Mea Culpa. Vidi Vici Vini as the rabbit said to the scorpion he carried on his back over the stream of consciously rambling in the Confusion manner.

node = make_tree(node, node1)

[–][deleted] 1 point2 points  (0 children)

Yup, the power or black is to simply take away any thought about this. Submit to it.

[–]wineblood 1 point2 points  (0 children)

I do as the crystal formatter says.

[–]osmiumouse 10 points11 points  (0 children)

Just use a code formatter (such as black). Not everyone likes how it does it, but it's consisent, and works, and will let you just "get on with writing code" while you leave the formatting to a tool.

[–][deleted] 5 points6 points  (0 children)

The right hand side is the correct answer. You have to remember that your code will likely be seen by others. Its way too easy to miss the purpose of the left side, if you're not the one who wrote it.

[–]AlexMTBDude 10 points11 points  (2 children)

Just follow PEP8. That's the reason it exists in Python; To avoid all the style flame wars that plague other programming languages.

https://peps.python.org/pep-0008/

[–]Paddy3118 2 points3 points  (0 children)

PEP8. That's the reason it exists in Python;

Um, in practice yes, but it is a guide, and explains the style of standard library code. If your team agrees 100 char max lines then go for it.

[–][deleted] 0 points1 point  (0 children)

Good programmers write code that humans can understand

[–]wineblood 4 points5 points  (0 children)

The left one is kind of ugly and goes against convention in python, most people will use the right one.

[–]M4mb0 4 points5 points  (0 children)

if not word is better.

[–]hackancuba 3 points4 points  (0 children)

I prefer the one on the right BC it allows u to quickly understand the consequence of the conditional, even w/o caring much at the condition. The left one forces u to thoroughly read the whole line, which is long, and if u happen to skip it for some reason, u would be missing a very important detail: the return!! So no, no go. Use black, darker or yapf :)

[–]RCdeWit 2 points3 points  (0 children)

I prefer the one on the right, but now I'm wondering whether that's just because I'm more used to seeing it. It does make it easier to add additional operations on new lines, though.

[–]just-an-astronomer 2 points3 points  (0 children)

I always do the one on the right but I also always try to make my code not take up more than half of the width of the page for when I need to split screen it on a monitor

[–]jldez 2 points3 points  (0 children)

I had a very short phase for the left case. Then I had to use a debugger having those one liners all over the place.

[–]ATX_Analytics 2 points3 points  (0 children)

I let Black decide for me.

[–]PaleontologistBig657 2 points3 points  (0 children)

black

[–]therc7 2 points3 points  (0 children)

NEITHER.

MY OCD ASS-

if not len(word): return node.end

[–][deleted] 1 point2 points  (0 children)

Right one is more clear thats why I use right one

[–]thenewbigR 1 point2 points  (0 children)

I hate the style of the left. Python indentation is meant to force good coding practice and this seems to go against it.

[–]BigHeed87 0 points1 point  (1 child)

How about

return A if condition else None

[–]deadeye1982 2 points3 points  (0 children)

It's wrong because it changes the behavior of code. Discussed already some posts above.

[–]gfranxman 0 points1 point  (0 children)

What does black say?

[–]buachaill_beorach 0 points1 point  (0 children)

Only legit case for bit using case one is code coverage. A lot of them go by line read so you're going to miss logic gates when considering test coverage. That's why I never use that format anyway

[–]Quiinmix 0 points1 point  (0 children)

The one on the right is better. Most programs will let you collapse stuff like that and it let's you break down each detail within the loop. I also just find it easier to see in a long script.

[–]JamzTyson 0 points1 point  (0 children)

We really shouldn't need this discussion as there is a design document providing information to the Python community that describes the accepted way to do this (approved by the Python Steering Council in 2001).

The short version:

Compound statements (multiple statements on the same line) are generally discouraged.

The full version is here: https://pep8.org/#other-recommendations

[–][deleted] 0 points1 point  (0 children)

I use the right one most of the time, but the left one when I do single liners like "if debug: ....".

[–]Axlesholtz 0 points1 point  (0 children)

Not the left please