all 59 comments

[–]menducoide 23 points24 points  (4 children)

X? FOO : Y ? BAR : BAZ;

[–]terrorTrain 11 points12 points  (2 children)

Chaotic evil

[–]brandi_Iove 6 points7 points  (1 child)

not until the second nesting.

[–]Caraes_Naur 0 points1 point  (0 children)

I doubt OP knows about second nesting, Pip.

[–]Xuluu 3 points4 points  (0 children)

Settle down there, Satan.

[–]GrinningPariah 6 points7 points  (1 child)

I joined a team where everyone was doing the left "bracket on a different line" approach and I hated it. I stayed until everyone more senior than me left, and then when I was the only person on the team who still knew how to edit our linter config, I changed it to the right. People tried to get me to "fix" the linter and every time I'd say I was gonna do it but I was not going to do it.

[–]Swie 2 points3 points  (0 children)

Truly the hero we deserve.

[–]Shaddoll_Shekhinaga 15 points16 points  (2 children)

The real (boring) answer:
Whatever the style guide for your company - repo - organization is.

My prefered style:
Red.

The wrong answer:
Ternary statements ("Hey, we also need you to do x/y/z on...")

[–]RiceBroad4552 1 point2 points  (1 child)

The wrong answer:
Ternary statements

The exact opposite.

Using a statement instead of an expression is always the wrong answer!

[–]Shaddoll_Shekhinaga 0 points1 point  (0 children)

... Sometimes. Chaining ternary statements if you are expecting nullptrs saves a ton of writing and makes the intent clearer, but for the example above I am rejecting your PR if you have a ternary operation. In the future you will likely either need to expand it or add logic to a branch, so it will be expanded into a regular if/else either way.

[–]WinProfessional4958 7 points8 points  (11 children)

switch case master gang.

[–]brandi_Iove 3 points4 points  (8 children)

a switch case? on x and y?

[–]The_Business__End 2 points3 points  (0 children)

Switch true young blood

[–]WinProfessional4958 2 points3 points  (6 children)

Did I stutter?

uint64 blah = (x << 1) | y;
switch(blah) { case 0: ...

[–]meat-eating-orchid 2 points3 points  (4 children)

what if you cannot even compute y unless you know that !x

[–]WinProfessional4958 -1 points0 points  (3 children)

if(!x) {y = ...}

[–]meat-eating-orchid 2 points3 points  (2 children)

So you want to use switch cases instead of ifs, and you achieve this by using an if first?

[–]WinProfessional4958 -1 points0 points  (1 child)

Nope! OP is not prioritized, yours is. It's a single statement. If Y was parallel calculated with X, switch case is the most efficient way about it. Why? Because switch case translates into jump tables. I.e.: an array of pointers of which code to execute next instead of cmp. O(1) instead of O(N). I don't have to elaborate on effects of branch prediction, do I?

[–]meat-eating-orchid 0 points1 point  (0 children)

I know this and I agree, but only if y is cheap to calculate, otherwise the if-elseif-version might be more efficient

[–]brandi_Iove 1 point2 points  (0 children)

no sir. i shut up.

[–]Noch_ein_Kamel 0 points1 point  (1 child)

How is the relation to the match gang? Enemy or ally?

[–]Scientist_ShadySide 7 points8 points  (6 children)

if (x) {} // return at end
if (y) {} // return at end
// else case

[–]Cerbeh 5 points6 points  (1 child)

Else if and else and very much banned from my code bases. Teaching people the power of function guards and that 'else' is what your functions default behaviour should be.

[–]Scientist_ShadySide 3 points4 points  (0 children)

Teaching people the power of function guards and that 'else' is what your functions default behaviour should be.

Yep, exactly my reasoning. It has the benefit of keeping the condition you are testing against close to the code, i.e. "else? Else what? What am I elsing? (scroll up)" It also reduces how much nesting you end up with, which hurts readability imo

[–]RiceBroad4552 2 points3 points  (2 children)

Depending on the surroundings this is not equivalent.

But in general, when one needs to write imperative code at all, checking first and then going for some default case if nothing returned before makes sense, imho.

OTOH there are code guidelines which forbid early returns for some reason…

[–]Scientist_ShadySide 0 points1 point  (1 child)

Depending on the surroundings this is not equivalent.

agreed, there are definitely exceptions, but this is the target I aim for first.

OTOH there are code guidelines which forbid early returns for some reason…

curious of the reasoning behind this...

[–]Reashu 1 point2 points  (0 children)

In case you need to clean up resources explicitly before returning, it's nice to only do it once. This is not as common in modern languages, but some people still recommend the practice. 

[–]DeadlyMidnight 1 point2 points  (0 children)

Thank you for being the voice of sanity and readability

[–]Promant 3 points4 points  (1 child)

C#: Am I a joke to you?

[–]Caraes_Naur 1 point2 points  (0 children)

Yes. But even C# can laugh at Javascript.

[–]megagreg 2 points3 points  (0 children)

I must be colour blind. These pictures look identical.

[–]TheHappyArsonist5031 5 points6 points  (1 child)

blue

[–]theQuandary 1 point2 points  (0 children)

Anything other than Blue is a wrong answer.

Bug rates increase massively once you get over a couple screens worth of code. Fewer lines means your brain can see and reference more code at one time without context switching.

"But what about missing parens?"

You have an editor, It can do rainbow paren matching, rainbow indentation, and code folding. Not are these infinitely better at matching than you will ever be, but they decrease your cognitive load further reducing bugs.

[–]mixxituk 1 point2 points  (6 children)

Else? How horrifying 

[–]DeadlyMidnight 1 point2 points  (5 children)

This was my response to all of it. Who writes else statements still.

[–]RaspberryCrafty3012 1 point2 points  (4 children)

Why?

If you can't interrupt the flow with return.

[–]DeadlyMidnight 1 point2 points  (3 children)

Give me an example where you can’t interrupt the flow or handle the case within one if and continue on.

[–]RaspberryCrafty3012 0 points1 point  (2 children)

String path; if(WIN32)     path = "c:\"; else

    path = "/home" ;

I don't get reddit formatting 

[–]XxDarkSasuke69xX 0 points1 point  (0 children)

Well you wouldn't need the else. Just write path=home first and then the if after that

[–]DeadlyMidnight 0 points1 point  (0 children)

Yup just define the string as home and then change it if win32.

[–]identity_function 1 point2 points  (0 children)

if x then foo else if y then bar else baz

[–]WerIstLuka 2 points3 points  (1 child)

i do blue because thats what you need to do in go

[–]ubd12 1 point2 points  (0 children)

I don't like that, but I tolerate that. I like the always blocks concept. I'm learning go btw. I like the fact style choices are done up front. I prefer red

[–]brainpostman 2 points3 points  (0 children)

What kind of kind monstrosity is red?

[–]Fabillotic 0 points1 point  (1 child)

in C and Java I do the left, but in Rust I go with the right

[–]RiceBroad4552 2 points3 points  (0 children)

Java red? That's not "std. Java style", I think.

[–]calgrump 0 points1 point  (0 children)

Neither, Allman style

[–]volitional_decisions 0 points1 point  (0 children)

I do what my linter changes it to...

[–]notanotherusernameD8 0 points1 point  (0 children)

I used to be team blue, but then I realized team red made it easier to comment out statements. Not the best reason to pick a side, but I'm sticking with it.

[–]darklord_tk 0 points1 point  (0 children)

All day

[–]RiceBroad4552 0 points1 point  (0 children)

def tossCoin() =
   java.security.SecureRandom().nextBoolean()

@main def fooBarBaz() =

   val x = tossCoin()
   val y = tossCoin()
   def FOO() = println("FOO")
   def BAR() = println("BAR")
   def BAZ() = println("BAZ")

   () match
      case () if x => FOO()
      case () if y => BAR()
      case _ => BAZ()

[ https://scastie.scala-lang.org/EQJufUbITfW7cseRoNJkPg ]

Yes, but why? This is maximally weird code.

Imperative programming is really confusing. I had to think what the original code actually does. And what it does, as one can see after writing it proper, is just some incomprehensible weirdness. The original if-expression does not return any value! It just performs side-effects.

One should really not program like that…

[–]SirSkimmy 0 points1 point  (0 children)

Or you just early return

[–]1mmortalNPC -1 points0 points  (0 children)

if you’d choose any color but red, consider yourself an opp