• Ever wanted an RSS feed of all your favorite gaming news sites? Go check out our new Gaming Headlines feed! Read more about it here.

A or B

  • A, it's all about minimalism baby

    Votes: 288 41.3%
  • B, it's gotta be the explicit logic

    Votes: 409 58.7%

  • Total voters
    697

Stopdoor

Member
Oct 25, 2017
5,778
Toronto
A
Code:
function doThing() {
    if (condition) {
        return 1;
    }
    return 2;
}

B
Code:
function doThing() {
    if (condition) {
        return 1;
    } else {
        return 2;
    }
}

Seen a lot of the former practice at my workplace lately and tbh it's totally bothering me, I'd put human readability above most things. Are you all about saving characters? Is the former actually somehow "safer" in your opinion?

(Edit: I should've made the example more dirty to make using a ternary not the easy answer, but yeah, just assume that's not plausible here)
 
Last edited:

Transistor

The Walnut King
Administrator
Oct 25, 2017
37,119
Washington, D.C.
A will return 2 no matter what and then only return 1 if the statement is true.
B is the proper form if you are trying to do an either/or thing.

EDIT: Ignore me. I haven't had my coffee yet.
 

Deleted member 16908

Oct 27, 2017
9,377
Code:
function doThing() {
    if (condition) {
        return 1;
    }
    else return 2;
}

Wouldn't this work?
 

Deleted member 16908

Oct 27, 2017
9,377
A will return 2 no matter what and then only return 1 if the statement is true.
B is the proper form if you are trying to do an either/or thing.

I'm pretty sure both functions work exactly the same. The only way A is returning 2 and not 1 is if the condition isn't true, so it's still an either/or situation.
 
OP
OP
Stopdoor

Stopdoor

Member
Oct 25, 2017
5,778
Toronto
Code:
function doThing() {
    if (condition) {
        return 1;
    }
    else return 2;
}

Wouldn't this work?

If your syntax allowed it, it's essentially the exact same as option 1 but then I'd question why you didn't do

Code:
function doThing() {
    if (condition) return 1;
    else return 2;
}

or even

Code:
function doThing() {
    return condition ? 1 : 2;
}
 

Deleted member 16908

Oct 27, 2017
9,377
Not necessarily. A will only return 1 if the condition is true. And then it will return 2 no matter what.

B will return 1 if it is true and 2 if it is false.

Maybe in the language you use it works that way, but in most languages "return" closes the function. You can't return 1 and then 2. As soon as you return once, that's it. Function over.
 

SweetNicole

The Old Guard
Member
Oct 24, 2017
6,542
A is the correct, clean code way to handle it. It simplifies the logic visually and is less lines of code. That being said, ternary operators are simpler even more so.

After the if with a return statement, there is no need for the else part as the code below the if will only run when the condition stated is not fulfilled.
 

Jazar

Member
Oct 25, 2017
1,474
South Florida
Funny I was just writing this logic yesterday. I used the else logic but Resharper tool wanted to simplify it to the first. The end of the day IMO it's not a huge deal either way so long as the logic is consise and clear.
 

Transistor

The Walnut King
Administrator
Oct 25, 2017
37,119
Washington, D.C.
Pretty sure these would work functionally exactly the same unless something about the logic was later modified.
I'm pretty sure both functions work exactly the same. The only way A is returning 2 and not 1 is if the condition isn't true, so it's still an either/or situation.
How are they any different? They could both be reduced into a ternary operator.
Maybe in the language you use it works that way, but in most languages "return" closes the function. You can't return 1 and then 2. If you return at all, that's it. Function over.
Ha! Y'all are 100% right. I haven't had my coffee yet and my brain substituted "print" where "return" was written.

I need my coffee now.

Regardless, I prefer the readability of B
 

grmlin

Member
Oct 25, 2017
10,283
Germany
I'm all for automatically applied code styles that do that kind of stuff for me, or at least highlight errors. There are only very few rules/code styles I can't deal with.

BUT, different styles from different developers in a team are a no go imo
 

GYODX

Member
Oct 27, 2017
7,233
Not necessarily. A will only return 1 if the condition is true. And then it will return 2 no matter what.

B will return 1 if it is true and 2 if it is false.

EDIT: ignore me. I haven't had my coffee yet
Functions only return once, though? At least in the languages I'm familiar with.
 

Deleted member 16908

Oct 27, 2017
9,377
If your syntax allowed it, it's essentially the exact same as option 1 but then I'd question why you didn't do

Code:
function doThing() {
    if (condition) return 1;
    else return 2;
}

or even

Code:
function doThing() {
    return condition ? 1 : 2;
}

Those are both good options too.
 
OP
OP
Stopdoor

Stopdoor

Member
Oct 25, 2017
5,778
Toronto
A is the correct, clean code way to handle it. It simplifies the logic visually and is less lines of code. That being said, ternary operators are simpler even more so.

After the if with a return statement, there is no need for the else part as the code below the if will only run when the condition stated is not fulfilled.

My brain can process the word "else" faster than anything else, though. I'm just reading out a list of exclusive cases and don't have to worry about somehow one of those cases doesn't accidentally not return and go to bottom 🤔

I should've made the example a tiny bit more dirty to make "ternary" not possible, tbh, I'm more focused on if you had to do logic branches.
 

rpm

Into the Woods
The Fallen
Oct 25, 2017
12,348
Parts Unknown
I'd do a ternary operator over either if possible, but if I had to do it either of those two ways, I'd go with B in this specific instance, if you're doing just an if else, I want to see the else. If instead of just "return 2" it was multiple lines of code, I'd go with A, because my brain sees that as "getting any simple cases out of the way before the real work of the method begins"
The real crime here is not putting opening braces on a new line.
This is the real debate right here
 

BeeksElectric

Member
Oct 25, 2017
109
B because readability is key. If you're the only one who will ever read your code, you can use shortcuts like A, but if you're on a team where someone else will eventually have to work on your code, you have to make sure it's easily comprehensible.
 

Basquiat

alt account
Banned
Apr 2, 2020
369
So type A is called an early exit condition I guess. I'd say the readability you gain from it is only worthwhile if you have functions with multiple nested conditions. Those functions can become painful to read as they end up being longer horizontally than vertically.
 

Lumination

Member
Oct 26, 2017
12,458
Newbies out of school will love the elegance of shorter code. People who have worked a few years will appreciate the explicitness of longer form code.
 

dragonbane

Member
Oct 26, 2017
4,583
Germany
If I can I would do:

return condition ? 1 : 2;

But otherwise it's A for me (with proper new line braces :P) unless the function is really long, then I prefer B for readability and consistency.
 

chezzymann

Banned
Oct 25, 2017
4,042
I really don't like stuff like jQuery and some of the new ES6 syntax like ? :. I want things to be obvious and readable even if it means a few extra lines of code.
 

nano

Member
Oct 26, 2017
413
Berlin
Would be A in our team. Depending on the length of the condition we would even simplify it more:
Code:
int DoThing()
{
    return condition ? 1 : 2;
}

We don't have an universal rule for that, if the condition is a longer expression, we tend to favor readability:

Code:
int DoThing()
{
    var condition = foobar || barfoo || foobaz;
    return condition ? 1 : 2;
}

But the else is just noise if it can be converted into a simple return like here. Depends more on the length of the variables/objects we use. We have expressive variable names fairly often and would use a conventional if:

Code:
int DoThing()
{
     if(isThisObjectEqualToSomeValue)
     {
          return someConstantThatWeHaveDefinedAbove;
     }

     return someOtherDefaultValue;
}
 

mute

▲ Legend ▲
Member
Oct 25, 2017
25,062
A if I'm doing stuff for me

B if I'm doing stuff professionally with other people, and I'll fuss if I catch anyone trying to pull an A at work too.
 

Chakoo

Member
Oct 27, 2017
2,838
Toronto, Canada
It depends on what your intended design is.

If you are trying to make an early exit (like checking if something is is invalid) then A all the way. B gets in the way for this design specially if you work somewhere that has a line character limit restriction (I worked at a place that was a strict no longer than 100 char per line)

If you are trying to handle a fork in the road situation where you need to do X or Y then B is cleaner and easier for a user to read.

Yet if the case is to be just a simple check function (ie isValid()) then this all the ***king way.
Code:
function isValidThing() {
    return condition ? 1 : 2;
}

So again, It really comes down to what you are trying to design/build for that given function then "it must always be A/B".
 

ZeroDotFlow

Member
Oct 27, 2017
928
A every day. It's not about elegance, it's just more readable. As you deal with larger and larger files or classes, the additional lines of code can quickly make things into a blurry mess as you try to suss out what the code is actually doing. It also makes it clear that this is the final return statement, serving as a bookend to the function. Ternaries are better than both though.
 

digit_zero

Member
Oct 27, 2017
2,360
The readability argument doesn't really hold with me just because writing code in the form of A is so prevalent - I'd expect/hope my coworker immediately understands what is happening there.

That could also be me defending myself for writing it like A every single time.
 

Saganator

Member
Oct 26, 2017
6,999
B. To avoid possibility of confusion. Like when I first looked at A, I mistakenly thought return 2 would happen regardless of the condition, but then I remembered the function would end if the condition was true and 1 is returned.

edit: However if the function was larger I'd probably go with A
 

dragonbane

Member
Oct 26, 2017
4,583
Germany
It depends on what your intended design is.

If you are trying to make an early exit (like checking if something is is invalid) then A all the way. B gets in the way for this design specially if you work somewhere that has a line character limit restriction (I worked at a place that was a strict no longer than 100 char per line)

If you are trying to handle a fork in the road situation where you need to do X or Y then B is cleaner and easier for a user to read.

Yet if the case is to be just a simple check function (ie isValid()) then this all the ***king way.
Code:
function isValidThing() {
    return condition ? 1 : 2;
}

So again, It really comes down to what you are trying to design/build for that given function then "it must always be A/B".
Yeah I find myself use A, B or C depending on the situation.