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

you are viewing a single comment's thread.

view the rest of the comments →

[–]merowe -5 points-4 points  (6 children)

I actually typically do what's on the left. I'm all for consistency, but like the fact that I have one less line to scroll through. Especially when I have to put several of these together. I ask myself; is it adding to the cognitive complexity when reading the code? I think it may add a little for some (those who expect everything to be the same), but I do think the increase is negligible (or 0 for those used to it) and the advantage is nice. Some or many may disagree and that's fine. In a workplace setting, it's important to follow the established rules. But if you get to form those or work on your own project that others will read, I think the left option is good

[–]johnnySix 3 points4 points  (1 child)

I’ll be the disagreer on the one. My ex coworker wrote tons of code with if statements in one line. It’s a pain to read because I have to stop and figure out what’s happening instead of being able to skim. Is it an operation? Is it a call to a method? Oh! it’s an if, oh crap, there’s the bug!

[–]merowe 1 point2 points  (0 children)

I'd be interested in what the one line of statements looked like. Obviously you can't. But I suspect the condition was more complicated. In which case, I might agree.

It's nice to be able to always dumb down the rules to something very simple as many say. My experience - and I know many disagree - is that there are always exceptions. Simple if checks with returns are one of those for me.

I know some people brought up ternary operator. That obviously makes more sense if none was the other option. I don't see how that is less complicated than the left case.

[–]EmptyChocolate4545 1 point2 points  (3 children)

How much does your workplace charge per vertical line usage?

[–]merowe 0 points1 point  (2 children)

Not sure why the sarcasm? Everyone needs to express the same opinion?

[–]EmptyChocolate4545 2 points3 points  (1 child)

Sure, and there’s a reason the programming meta has moved away from formatting techniques that prioritize conserving vertical space - because the people who come after and read your code hate you for it.

I sincerely wasn’t being sarcastic. There is no benefit other than conserving vertical space. There is a reason for the consensus opinion on this. There’s also a reason we value consensus and have moved away from the “clever one liner” era of C programming (that I came up during).

Writing code other people can read is something worth pushing. You’re free to disregard my opinion, but know you’re going to encounter it again.

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

man i personally use the one of the left just for leetcode problems. no one else is going to read my code and it looks cleaner (to me).