• 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.
  • We have made minor adjustments to how the search bar works on ResetEra. You can read about the changes 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

PorcoLighto

Member
Oct 25, 2017
766
A is more readable for me and how I do it usually too.
When I look at an If, I first look at the result of the block. If that block terminates with a return then I do not care what comes after. If it does not, then I add else.
 

nullref

Member
Oct 27, 2017
3,073
Kind of depends on what the function is actually doing, and how much more there is to the code than the simple example, but I'd generally do A. I'm all for being more verbose when it's actually meaningful, but I'd argue B is not inherently more readable in general, and that if the logical branches are not of equivalent "weight"—e.g. guard clause for rarer condition vs. main "happy path" logic—A is better to get the early return checks out of the way and then eliminate the extra conditional block scope for the main logic. In real code, I think excessive conditional block nesting tends to be a bigger readability problem.

(Of course, if the function is actually as few lines as the example, I'd just use a ternary instead.)

The real crime here is not putting opening braces on a new line.

The real real crime is not just doing whatever is most conventional for your project or language and moving on to things that actually matter. :)
 
Last edited:

Jonathan Lanza

"I've made a Gigantic mistake"
Member
Feb 8, 2019
6,889
B

I look at A and I feel like the return 2 SHOULD happen. I know that's not the case but that's how my brain automatically interprets anything that comes after an if statement.

B looks more like a binary choice.
 

swsp

Member
Oct 27, 2017
556
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?
A is preferred by clang-tidy and as such is all you'll see in our codebase. We can't merge code that conforms to B.

edit: it originates as an LLVM coding guideline
 

Poppy

Member
Oct 25, 2017
18,294
richmond, va
i feel way more comfortable doing it the second way because basically, i dont program a ton and i still have a pretty rigid style derived from using java in college courses, so i think i tend to do things in like a textbook style
 

Dache

Member
Oct 25, 2017
1,135
UK
Bit of a sidetrack, but in C#, wouldn't case B fail because there's no default Return? Like, I'd have to do this if I wanted to add the explicit else case becuse without the second Return False, it wouldn't compile:

C#:
bool MyFunc()
{
    if (situation)
    {
        return true;
    }
    else
    {
        return false;
    }
  
    return false;
}

Otherwise, I'd be forced to use A, right? I realise B is clearer to human readability.

Edit: Might this be a compiler quirk if it wouldn't otherwise do this for others?
 
Nov 5, 2017
180
I prefer B, but then I don't really do OOP, I program microcontrollers mostly in C. But this is a discussion where you won't reach a consensus.
 

ascii42

Member
Oct 25, 2017
5,804
Bit of a sidetrack, but in C#, wouldn't case B fail because there's no default Return? Like, I'd have to do this if I wanted to add the explicit else case becuse without the second Return False, it wouldn't compile:

C#:
bool MyFunc()
{
    if (situation)
    {
        return true;
    }
    else
    {
        return false;
    }
 
    return false;
}

Otherwise, I'd be forced to use A, right? I realise B is clearer to human readability.
No, C# would handle B fine.
 

steejee

Member
Oct 28, 2017
8,746
Would vary a lot by the context. Meaning how much is happening in that "return 1" vs "return 2" - if one was more complex Id do A. Also depends what type of data.

The real crime here is not putting opening braces on a new line.

Depends on language. Go uses same line opening bracket and it's not optional.
 

Peebs

Alt-Account
Banned
Dec 16, 2020
119
B,

Then you run it through a minifier that automatically converts it to A when it's deployed so it shrinks the footprint.
 
Oct 27, 2017
1,148
Finland
The argument B is more readable is weird. It's good to explicit and verbose at times, but that hardly matters in this case. Also as pointed out, most linters by default would accept B as the correct solution.

Both are as readable but one has extra clutter in it (but a negligible amount).

I'm an A guy.

But a ternary-like thing would be another option

Code:
function doThing() {
    return (condition ? x = 1 : 2);
}
Which language are you thinking? Because at least in JS that would be overly complex (simply condition ? 1 : 2 would work) and as a side effect, you'd have a global variable x.
 

Failburger

Banned
Dec 3, 2018
2,455
Whatever my tech lead says, they're ultimately the owner of code quality at my job.

But I prefer B due to readability; much easier to 'skim'.
 
Oct 27, 2017
3,683
In general, I'd take B any day.

In this case it's not a big deal as both are fairly simple to understand, but in general I prefer as much explicit code as possible, even if that bulks up the line count as it's much easier for handover purposes and digestibility.
 

Timmm

Member
Oct 28, 2017
2,896
Manchester, UK
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.

This basically

I prefer there to be as few ways "out" of a function as possible, so would probably use a ternary, or make a boolean variable and assign the value using the if statement and then return the variable at the end

Basically it depends on how complicated the logic in the function is, for one as simple as the examples it doesn't matter a whole deal
 

Carn

Member
Oct 27, 2017
11,993
The Netherlands
B is more intuitive. A looks like it will return 2 even if the condition is true.

if there are only two allowed returns you can also just set one as a default.

Code:
var = 1;
function doThing() {
    if (condition) var = 2;
  }

Or if you have functions that need multiple paramaters you might want to guard against undefined paramaters (so you can use a terniary to just default them if you want).
 

grmlin

Member
Oct 25, 2017
10,365
Germany
The argument B is more readable is weird. It's good to explicit and verbose at times, but that hardly matters in this case. Also as pointed out, most linters by default would accept B as the correct solution.

Both are as readable but one has extra clutter in it (but a negligible amount).


Which language are you thinking? Because at least in JS that would be overly complex (simply condition ? 1 : 2 would work) and as a side effect, you'd have a global variable x.
- readability is much more important than shorter code
- minifiers (js) or compilers care for the build/deployed version
- code style should always be checked automatically

and if it's Javascript, the answer would be
JavaScript:
const doThing = () => condition ? 1 : 2;
anyway. But as OP said, this is just a very simple snippet and it all changes with a different context.
 

Tony B

Member
Dec 29, 2018
670
So in your example, I'd argue it's a wash, go for legibility (i.e. B). Where I might differ is the point at which you have multiple paths to return the same(ish) thing (for example multiple ways of validating an input prior to processing it leading to multiple error codes).

So for example:

Code:
function doThing(input) {
  if input != NULL {
      return ERROR1;
  }
  else if (valid(input)) {
      return ERROR2;
  }
  else if (someGlobalState == READY) {
      return ERROR3;
  }
  else {
    // do thing to input
    return result;
  }
}

I'd argue in this instance we've started to obfuscate where the major processing will be performed.

Code:
function doThing(input) {
  if input != NULL {
      return ERROR1;
  }
  if (valid(input)) {
      return ERROR2;
  }
  if (someGlobalState == READY) {
      return ERROR3;
  }

  // do thing to input
  return result;

}

I've seen alot of code that nests the processing within an unseemly number of if statements where a flatter structure and multiple return points would be more readable (assuming your language allows this that is). But of course ultimately, alot of this is down to what you'll find legible. Assuming you're running through some sort of intelligent compiler/interpreter, it'll normally produce similar executable code anyway.
 

Mathiassen

The Fallen
Oct 31, 2017
257
A!
Early return is good practice. Helps lifetimes and readability.

B is actually bad code.
 

julia crawford

Took the red AND the blue pills
Member
Oct 27, 2017
35,646
Honestly no one will ever need to be fast enough that they can't take their time reading the code in a slightly slower way. The only real benefit here is having linters to maintain consistency across a team. At that point you can justify going ham on every single choice, but unless you're doing that... it's useless and a waste of time to consider legibility to this level of scrutiny.
 
Oct 27, 2017
3,683
So in your example, I'd argue it's a wash, go for legibility (i.e. B). Where I might differ is the point at which you have multiple paths to return the same(ish) thing (for example multiple ways of validating an input prior to processing it leading to multiple error codes).

So for example:

Code:
function doThing(input) {
  if input != NULL {
      return ERROR1;
  }
  else if (valid(input)) {
      return ERROR2;
  }
  else if (someGlobalState == READY) {
      return ERROR3;
  }
  else {
    // do thing to input
    return result;
  }
}

I'd argue in this instance we've started to obfuscate where the major processing will be performed.

Code:
function doThing(input) {
  if input != NULL {
      return ERROR1;
  }
  if (valid(input)) {
      return ERROR2;
  }
  if (someGlobalState == READY) {
      return ERROR3;
  }

  // do thing to input
  return result;

}

I've seen alot of code that nests the processing within an unseemly number of if statements where a flatter structure and multiple return points would be more readable (assuming your language allows this that is). But of course ultimately, alot of this is down to what you'll find legible. Assuming you're running through some sort of intelligent compiler/interpreter, it'll normally produce similar executable code anyway.
Obviously the context would matter, but I'd argue the former is more readable, primarily because it's making it explicit that each of the conditions are intended to be mutually exclusive, whereas simply using a number of if statements may not necessarily mean they would all be exclusive.
 

turbobrick

Member
Oct 25, 2017
13,166
Phoenix, AZ
I voted A assuming that's the only thing the function does.

In reality though I doubt it would be, and if I wrote a function it would only have one return statement.
 

nullref

Member
Oct 27, 2017
3,073

That's generally worse than either A or B because you've introduced mutable state, and now you have to track its transitions to read the function instead of just follow the control flow.

A lead of mine once told me they preferred functions with a single entry and exit point (i.e., only one return statement), and that's become completely ingrained in my coding style ever since.

I think you're often going to produce more convoluted code in trying to hold to the "single return" than you would otherwise. That rule makes more sense in languages with e.g. explicit memory allocation, where you also have to ensure you free resources before you return—so it was less about the pure readability of the single return, than about making it easier to be correct in that regard. Less so in garbage-collected languages where you rarely have to worry about that kind of thing.
 
Last edited:

Mivey

Member
Oct 25, 2017
17,924
I think B is nicer, but I'd lie if I said I have never written A, especially if the condition is something that's added later.
 

the-pi-guy

Member
Oct 29, 2017
6,301
I think either are fine.

As long as there isn't any compiler goofiness where it can somehow return two things.
 

ZeroDotFlow

Member
Oct 27, 2017
928
So in your example, I'd argue it's a wash, go for legibility (i.e. B). Where I might differ is the point at which you have multiple paths to return the same(ish) thing (for example multiple ways of validating an input prior to processing it leading to multiple error codes).

So for example:

Code:
function doThing(input) {
  if input != NULL {
      return ERROR1;
  }
  else if (valid(input)) {
      return ERROR2;
  }
  else if (someGlobalState == READY) {
      return ERROR3;
  }
  else {
    // do thing to input
    return result;
  }
}

I'd argue in this instance we've started to obfuscate where the major processing will be performed.

Code:
function doThing(input) {
  if input != NULL {
      return ERROR1;
  }
  if (valid(input)) {
      return ERROR2;
  }
  if (someGlobalState == READY) {
      return ERROR3;
  }

  // do thing to input
  return result;

}

I've seen alot of code that nests the processing within an unseemly number of if statements where a flatter structure and multiple return points would be more readable (assuming your language allows this that is). But of course ultimately, alot of this is down to what you'll find legible. Assuming you're running through some sort of intelligent compiler/interpreter, it'll normally produce similar executable code anyway.
Those are functionally two very different code blocks even if the output is the same in this scenario. The first one actually obfuscates the logic more because the blocks are all connected in a single logical chain. Only one of those statements will ever be executed. In the second block, you're guaranteed that each check will occur sequentially and in order if for some reason one of the statements was not actually a return statement. Or if multiple of the logic branches were true (like A < 2, followed by A < 3).

If you were to reduce a set of if statements into an if-else statement, the odds are fairly likely you'd bring in a bug or two.
 

BobsReset

Chicken Chaser
Member
Oct 26, 2017
912
I prefer readability over simplicity. In the OP we can all clearly see what's going on but I am sure we have all come across code where the return might be another function call (which in turn returns something else). Having an explicit 'else return null' would probably make life easier for the next sucker to come along and try and figure out what's going on.
 

Mathiassen

The Fallen
Oct 31, 2017
257
I'd argue readbility is not if something is explicit or implicit but if the flow of the program can be reasoned about by the reader.
 

voidecker

Member
Oct 27, 2017
130
I'm team A, though mostly because the ESLint config I worked with on one of my bigger projects at my last job would throw warnings with B (i.e. It would want a return statement that's outside of a conditional)
 

cubicle47b

Member
Aug 9, 2019
730
My preference is using a ternary there as long as the if statement isn't super long. I've seen linters not accept B.