• 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

GlacialTruffle

One Winged Slayer
Member
Oct 25, 2017
571
Voted A
I probably use both interchangeably.
A for catching errors
Code:
if (var == null)
    return null;

return var + 7;

And B for something more descriptive
Code:
if (var > 7)
     return var;
else
     return var + 7;
 
Last edited:

StrangeADT

Prophet of Truth
Member
Oct 25, 2017
2,054
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.
Nah this is a stylistic choice not related to seniority. It's all over the place IME from code reviewing stuff for people of all experience levels. Been programming for a decade. A (or ternary) all the way for me. It's plenty explicit.
 

wtl420

Member
Feb 16, 2019
35
Don't agree with the poll options at all. Early returns are explicit and great and make the code much easier to read when dealing with more and more ways it can exit
 

prophetvx

Member
Nov 28, 2017
5,325
Nah this is a stylistic choice not related to seniority. It's all over the place IME from code reviewing stuff for people of all experience levels. Been programming for a decade. A (or ternary) all the way for me. It's plenty explicit.
Totally. I've been in the industry for almost 20 years and agree.

None of them are difficult to maintain, if a more complex example was provided then there would be a clearer right or wrong for maintenance purposes.
 

b3llydrum

Member
Feb 21, 2018
4,147
A just feels gross - I'd rather use B.

Well, I'd rather use a ternary but that's not an option here!
 

Tomasoares

Member
Oct 28, 2017
4,510
Yeah, it's nothing like a conditional chain like "if ((conditionA || conditionB) && !conditionC etc" or whatever.

I'm not a super experienced programmer but I'm all for readibility, and both answers do it fine.
 

Fafalada

Member
Oct 27, 2017
3,065
Totally agree with the first point. Second one: Without these discussions, there wouldn't be much progress in programming languages and the guidelines within companies.
Well it wouldn't eliminate them - it would narrow away similar discussions from people writting code to people designing languages, tools and IDEs. Cognitive load of formatting discussions shouldn't sit with the people writting production code, it's just a giant time-waste that we had the means to side-step decades ago.
 

Chakoo

Member
Oct 27, 2017
2,838
Toronto, Canada
doThing enterprise edition:


Code:
DataStructures.Numbers.IntegerContainer doThing() {
 
    DataStructures.Numbers.IntegerContainer returnObject = ObjectFactoryFactory.getInstance()->getNumberContainerFactory()->getInstance(DataTypes.NumberTypes.TypeInteger)->getNewContainer();

    if ( EqualityCheckFactory.getInstance()->isEqual( Booleans.Value_True, condition ) ) {
        returnObject->setValue( Integers.Value_1 );
    } else {
        returnObject->setValue( Integers.Value_2 );
    }
 
    return returnObject;
}
Oh god..... Reason #123209973 of why I hate working on android projects. I also think android teams hated me because I always wrote Java code like C/C++ and would end up replacing 4 files and thousands of lines of code with just a few hundred in a single file that is still much easier to read than what came before.
 

just_myles

Member
Oct 25, 2017
6,451
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)

B. It's not that verbose honestly so I'm not sure why it would be omitted, and it's easily readable. At a glance I would assume the return 2 would happen regardless of the condition.
 

brainchild

Independent Developer
Verified
Nov 25, 2017
9,478
Code:
answer = conditional_operator_syntactically_allowed ? ternary_operator : poll_option_a;


 
Last edited:
Oct 26, 2017
2,430
For a simple example like this I'd use a ternary.

For more complex things I tend to use early returns mostly for error handling and more descriptive if else structures for the main logic of the function.

Of course the real answer is to use whatever standard was set for the project.
 

BigWeather

Member
Nov 4, 2017
1,426
I present C.

Code:
function doThing() {
    var x = 2;
    if (condition) {
        x = 1;
    }
    return x;
}
This is what we recommend in my department (well, assuming there's a bit more complexity, this we'd just return condition ? 1 : 2;). We prefer one point of return as it makes debugging easier (one place for an "exiting this function" breakpoint / log statement). Additionally setting the return variable at the top clearly establishes the default return value (or, if there is none the compiler will flag that the return variable isn't set if any path fails to set it). Finally, setting the return into a variable provides a way to have your IDE find all points at which it is set and to what, something a neither A or B allows.
 

BigWeather

Member
Nov 4, 2017
1,426
Another advantage to a single return is if you have to go back and make the function async it's easier to wrap it in a Promise and change the single return to a resolve.
 

GangWarily

Member
Oct 25, 2017
901
I mostly write JS code but I think I have a linter rule to warn unnecessary else conditions if there's an early return like in B.

So I vote A.
 

Shyotl

Member
Oct 25, 2017
1,272
Probably B. I always prefer utilizing syntax that helps communicate intent. Realistically, I'd use a ternary here tho
doThing enterprise edition:


Code:
DataStructures.Numbers.IntegerContainer doThing() {
 
    DataStructures.Numbers.IntegerContainer returnObject = ObjectFactoryFactory.getInstance()->getNumberContainerFactory()->getInstance(DataTypes.NumberTypes.TypeInteger)->getNewContainer();

    if ( EqualityCheckFactory.getInstance()->isEqual( Booleans.Value_True, condition ) ) {
        returnObject->setValue( Integers.Value_1 );
    } else {
        returnObject->setValue( Integers.Value_2 );
    }
 
    return returnObject;
}

This is why I stay in the native layer at work. Hate java with a passion. Horizontal sprawl. Singleton abuse. Just shit.
 

LuckyHit

Member
Oct 27, 2017
123
I prefer B, personally.
Also, serious question, are there programmers that format their code like this?
Code:
function doThing()
{
    if (condition)
    {
        return 1;
    }
    else
    {
        return 2;
    }
}
 

trashbandit

Member
Dec 19, 2019
3,909
I prefer B, personally.
Also, serious question, are there programmers that format their code like this?
Code:
function doThing()
{
    if (condition)
    {
        return 1;
    }
    else
    {
        return 2;
    }
}
Recently I've switched over to this style because it's easier to parse the "shape" of the code for me. It's easier to get a handle on the scope/nesting, if you happen to be unlucky enough to deal with deep nesting.

I would go with B in the absence of a ternary, though I would make the if else statements one line, so:
Code:
function doThing()
{
     if (condition)  return 1;
     else  return 2;
}

and if I'm feeling spicy
Code:
function doThing()
{
     if (condition)  return 1;
     else            return 2;
}
 
Oct 25, 2017
6,454
it depends on the situation, but i prefer A. i tend to read it as "heading toward the expected termination of the function ('2' in this case), unless this thing interrupts it".

it's subtle, but in the right cases when i'm reading a lot of code quickly, even little things that that can help me parse intent.
 
Nov 2, 2017
2,239
I prefer B, personally.
Also, serious question, are there programmers that format their code like this?
Code:
function doThing()
{
    if (condition)
    {
        return 1;
    }
    else
    {
        return 2;
    }
}

Also a B, and this is my preference for the styling. I've been in javascript land for a while, I get it, I'm doing the other because there's good reasons, but this is the way I prefer. I like being able to look at indent levels like that and see clear line demarcations for the start and end of the block's body, plus the thing driving the block on the same indent level. This just parses more cleanly to me.

This is maybe overkill on something like this, but I feel it really stands out when you get something like :

Code:
function doThing()
{
    if ((conditionA == 'A' && conditionB == 'Y') ||
        (conditionA == 'B' && conditionB == 'N'))
    {
        return true;
    }
}

I'm definitely not saying it's the one true way, because obviously you can refactor to avoid that scenario, but it's just what I prefer personally.
 

Weltall Zero

Game Developer
Banned
Oct 26, 2017
19,343
Madrid
*Arguably* being a strong keyword there.

In general, I don't think most people would agree with you that having returns other than in the last line makes code paths harder to follow. Personally especially returns makes it easier for me to follow - when you see a return statement, you immediately know that the path ends there.

Sure, but the problem is precisely when you don't see a return statement. The point of "single entry, single exit" is to make the whole control flow of a method and all of its possible branches immediately obvious at a glance. You only need to look at the brackets / indentation to know where the control can possibly flow into. Arbitrary exit points force you to scan each prior line for return statements before you can make even basic claims like "this line will always execute".

I mean, rivers of ink have been written about this very topic; it's probably pointless of us to retread this years-old discussion. I too was skeptical of this principle when a co-worker first adviced it to me years ago, but I gave it a try and it was a game changer.

Breaks I don't really like anyway since nowadays I mostly use JavaScript and functional loops are generally more preferable to imperative loops.

Also the entire philosophy of functional programming (which is very trendy at the moment) kind of goes against your idea. Immutability is a huge part of functional programming and your example after the ternary breaks that idea.

Sure, but the OP didn't specify this was functional programming.

The thinking goes that once you present a variable to be modified like that, you now have to track its changes across the whole method (or potentially multiple methods if you pass it as a reference parameter) which makes it harder to read.

The thinking is clear, as long as you understand that t's a give and take. For me, control flow is far more important to perceive at a glance than the value of any one specific variable. And if at any point it's the value that is critical, having one exit point also makes it that much easier to debug the return value, by plopping one single log line, or a single breakpoint, right before the final return.

In the end, like so many goddamn things in programming, everyone is bound to have different priorities and very strong opinions about it. All I can offer is my own experience, which is that I've been coding using multiple returns for over ten years, then using single returns for almost ten more years, and I can't go back to the former. I suggest giving it at least a try before dismissing it.
 

Weltall Zero

Game Developer
Banned
Oct 26, 2017
19,343
Madrid
I prefer B, personally.
Also, serious question, are there programmers that format their code like this?
Code:
function doThing()
{
    if (condition)
    {
        return 1;
    }
    else
    {
        return 2;
    }
}

This is pretty much C# standard (perhaps with the exception of having else in their own line, not sure what's C#'s standard on that). I went from C++ to Java to C# over the years, so I have picked up a mixture of what I think looks best and cleanest, which mostly means Java standards... even when I write C#. :)
 

Rodelero

Member
Oct 27, 2017
11,508
Effectively B. I'm a C++ programmer for the most part and for whatever reason I despise braces on the same line as statements, so I'd opt for the following, or use a ternary operator if the condition is simple enough.

I honestly think part of the reason I like this way is just symettry, which I do think helps readability.

C++:
int DoThing()
{
    if (condition)
        return 1;
    else
        return 2;
}
 
Last edited:

HybridEidolon

Member
Sep 27, 2020
337
It really depends on the procedure or function and what is being evaluated in each control flow branch.

A would be suitable for describing preconditions in recursive algorithms or in input checking, where the branch is a short-circuit precondition to continued recursion or execution of a procedure.

B would be suitable for boolean evaluation, especially if both branches result in more function evaluation downstream. However, if it is a mathematically pure function, I would prefer to express the branch as something like this in JavaScript:

JavaScript:
const booleanFoo = (a, b) => evalA(a) || evalB(b);

This is semantically different in JavaScript than using named functions because named functions are hoisted during evaluation, but functional purity is important in some UI frameworks.
 

LuckyHit

Member
Oct 27, 2017
123
What's wrong with this?
I don't know? Nothing as far as I can tell. I just never see anyone using this format, even though one of my college professors insisted we write code like this for their class. I actually kind of like it, as I can immediately tell where a function begins and ends by just looking down for the aligned bracket, but I'm not a professional programmer by any means. I also imagine that it could get annoying if there are multiple nested if-else statements...
 

Weltall Zero

Game Developer
Banned
Oct 26, 2017
19,343
Madrid
In fact I usually do something a bit different:
Code:
int doThing(){
    result = 2;
    if condition{
        //Other code
        result = 1;
    }
    return result;
}

I know it's now widely accepted but I still don't like to exit functions mid-way through return statements, or loops with breaks/exits (I only do it if there's a notable performance gain).

This man knows what's up. :)

The last company I worked at (...2 months, cuz yeah) banned use of the ternary operator. 🤣

Jesus. What's the penalty for lambda expressions, a firing squad? :D
 

ChrisR

Member
Oct 26, 2017
6,794
I don't know? Nothing as far as I can tell. I just never see anyone using this format, even though one of my college professors insisted we write code like this for their class. I actually kind of like it, as I can immediately tell where a function begins and ends by just looking down for the aligned bracket, but I'm not a professional programmer by any means. I also imagine that it could get annoying if there are multiple nested if-else statements...
Ah okay, I thought you were posting it as a "what the fuck is this" kinda statement :D

As for multiple nested if/else statements, that's more of a code smell for me that there might be some refactoring that needs to happen than to be annoyed at how the code looks with braces placement.
 

LuckyHit

Member
Oct 27, 2017
123
Ah okay, I thought you were posting it as a "what the fuck is this" kinda statement :D

As for multiple nested if/else statements, that's more of a code smell for me that there might be some refactoring that needs to happen than to be annoyed at how the code looks with braces placement.

Ohhhh, okay I see it now. Yeah, that's my bad! I meant it as a totally sincere question.
Also, thanks for the answers everyone.
 

BigWeather

Member
Nov 4, 2017
1,426
This man knows what's up. :)



Jesus. What's the penalty for lambda expressions, a firing squad? :D
To be fair, when lambda expressions were first added to C# they were about 10x slower than doing non-lambda. I had a college new hire that insisted on using them regardless of the performance penalty (because readability). I told him it was fine in UI code where you are likely input-bound anyhow but don't do it in computationally intensive code where the performance hit could matter. He refused. It was the only time I've had to really whip out my "No, actually, I'm technical lead and in this case performance REALLY matters so change that to a for." Luckily they fixed that performance issue with the next version. Sadly I haven't done C# since 2015 when I switched jobs. Now I do TypeScript/JavaScript (about 90/10 split). Love TypeScript.
 

yyr

Member
Nov 14, 2017
3,461
White Plains, NY
I'd choose B, simply because it's easier for me to read. I always prefer more information rather than less, regardless of what I'm doing.

I wouldn't knock anyone choosing A, though. Feels like it comes down to personal preference rather than one answer being "more correct" than the other.
 

Radnom

Member
Oct 25, 2017
1,014
I definitely prefer A in the example because I always want to see a 'return x;' at the bottom of the function at the lowest indent. For me whatever that value is is the 'default' or error value and helps me get a better idea of the function.

I know the else will always get hit, and the compiler will let me know if there's something wrong, and that it's totally just personal preference so I can work with either, but I feel funny when every return value is in an else statement or condition.

Code:
bool IsBanana()
{
    if (type == "banana")
    {
        return true;
    }
    return false;
}

bool GetFruitType()
{
    if (type == "orange")
    {
        return 1;
    }
    else if (type == "banana")
    {
        return 2;
    }
    return -1; //error: not a fruit
}

edit: it's not about minimalism though, because I also like to expand brackets onto new lines
 

Micael

Member
Oct 28, 2017
1,362
Real answer for me is neither, from my experience this "if condition then return x" code is usually sanity check related, like if the language has null values it is to check if the given variable is null and so on, and honestly if else are terrible language constructs for that. They are ok for one or two checks, but if the code involves extra ones things start looking ugly real fast, and the real intent of the software soon starts getting muddled.

This also tends to be part of a larger issue of handling domain expected errors as exceptions, which tends to see functions that might fail not really show that in the return type, and instead relying on function names to display that (say your TrySomeThing).

Like take the example of the GetFruitType Radnom posted above.

Code:
bool GetFruitType()
{
    if (type == "orange")
    {
        return 1;
    }
    else if (type == "banana")
    {
        return 2;
    }
    return -1; //error: not a fruit
}

If you are the function caller you have no idea what constitutes an error, that the function can give an error and so on, because it is returning an int regardless, but if the function returns a result type then the user knew that function could fail, and would have to account that when calling it.

Something like F# computation expressions (for the purposes of this lets call that the result monad), it is just a lot cleaner for this sort of error handling code, since it maintains the code nice and linear while doing all the same functionality.

like say there is a function that takes some value that can be null, and then has to divide that value by some other value (obviously that value cannot be 0), if one did it with the if else, like one saw above we might get a function that looked like this:

Code:
someFunction (somePossibleBaseNullValue, valueToDivide) =
    if somePossibleBaseNullValue = null then
       return  -1
    if valueToDivide = 0 then
       return -1

    return  somePossibleBaseNullValue / valueToDivide

and to make it more explicit maybe instead of returning -1 you do a raise exception, however in languages with the result monad, that function becomes something like this:

Code:
someFunction (somePossibleBaseNullValue, valueToDivide ) =
    result {
        let! checkedBaseValue = somePossibleNullValue |> Result.NotNull "Value was null"

        let! checkedValueToDivide =  (valueToDivide = 0) |> Result.isFalse "Cannot divide by 0"

        return checkedBaseValue / checkedValueToDivide
    }

The above function looks extremely similar to what a function without any error checking might look like, and ofc the advantages get bigger and bigger the more complex and the more error checking and so on there needs to be.
Adding to that the above function now returns a type of Result<int,string>, which means it can have either return an int or a string with the error, so the user has to check that, he can't just assume the function was a success when it might not have been, essentially turning a potential runtime error into an error that needs to be solved at compile time.


With all that being said, voted A, feel like it is pretty explicit, but can understand why B would be chosen in such a simple example, in the real world though I would argue most things are more complex so A makes more sense, since it is saying "if this fails the check return now", and then you end up with the main part of the function code without it looking nested, especially since realistically you could have and if inside that if, that just tends to read poorly.
 
Last edited:

GS_Dan

Member
Oct 30, 2017
1,969
A for me. If there's an else, I automatically assume that there's a need for it: that the if wouldn't return and there's likely to be code after the if/else which is yet to be reached.
 

Saro

Member
Oct 25, 2017
144
I've used both but I lean towards A because I've been doing alot more work in JS. A ternary operator would be the pefered choice if I was working on a team tho (looks at React).
 

Nozem

Member
Oct 27, 2017
396
A. Early returns are great.

B becomes a mess when dealing with 3 or more options.
 

Horp

Member
Nov 16, 2017
3,707
A balance is best. Sometimes overly verbose gets harder to read, if nothing else sometimes because of its cheer size. Bigger blocks means less overview. Making readable code is an art form of its own, and there are no simple ABC rules to follow that will lead to the perfectly readable code.
Especaially jsx/tsx code. Making that readable is really hard.
 

Rendering...

Member
Oct 30, 2017
19,089
I'm a noob, but readability is often the way to go IMO (but not past the point of stupid verbosity). Unless you're doing a minor project for yourself and no one else will ever see your code.
 
Last edited:

Scheris

Member
Oct 27, 2017
1,379
B when there's multiple if/else if/else statements, although short-hand statements like PHP's for if/else (using ?) I'll use for easier readability (and less typing) if it's a simple check.
 

hateradio

Member
Oct 28, 2017
8,733
welcome, nowhere
Code:
bool IsBanana()
{
    if (type == "banana")
    {
        return true;
    }
    return false;
}

edit: it's not about minimalism though, because I also like to expand brackets onto new lines

Totally on a different tangent. This example makes me squirm.

Code:
bool isBanana(String type) {
    return type == "banana"; // just return if it's banana or not, no need for if/else
}



A is the way to go, there's no need for the else.
Pretty much.
 

chrominance

Sky Van Gogh
Member
Oct 25, 2017
13,572
I voted B because I would rather your code be easily understood by other programmers than elegant or concise. But in this specific case, A and B basically mean the same thing. Assuming there is no other logic in the function, it should be trivial for most people to figure out they do the same thing. In that case I'd take either, and resort to whatever the house style is as the tiebreaker.
 

TP-DK

Member
Oct 28, 2017
2,042
Denmark
Code:
fun doThing() {
    return if (condition) {
        1
    } else {
        2
    }

Code:
fun doThing() =
     if (condition) {
         1
     } else {
         2
     }

Of course these are only for really simple cases.