0xLuca
0xLuca

Reputation: 155

Is there a name for the difference of these two code styles?

When i see code from others, i mainly see two types of method-styling.

One looks like this, having many nested ifs:

void doSomething(Thing thing) {
  if (thing.hasOwner()) {
    Entity owner = thing.getOwner();
    if (owner instanceof Human) {
      Human humanOwner = (Human) owner;
      if (humanOwner.getAge() > 20) {
        //...
      }
    }
  }
}

And the other style, looks like this:

void doSomething(Thing thing) {
  if (!thing.hasOwner()) {
    return;
  }
  Entity owner = thing.getOwner();
  if (!(owner instanceof Human)) {
    return;
  }
  Human humanOwner = (Human) owner;
  if (humanOwner.getAge() <= 20) {
    return;
  }
  //...
}

My question is, are there names for these two code styles? And if, what are they called.

Upvotes: 6

Views: 243

Answers (4)

MC Emperor
MC Emperor

Reputation: 23057

The early-returns in the second example are known as guard clauses.

Prior to the actual thing the method is going to do, some preconditions are checked, and if they fail, the method immediately returns. It is a kind of fail-fast mechanism.

There's a lot of debate around those return statements. Some think that it's bad to have multiple return statements within a method. Others think that it avoids wrapping your code in a bunch of if statements, like in the first example.

My own humble option is in line with this post: minimize the number of returns, but use them if they enhance readability.

Related:

Upvotes: 7

Stephen C
Stephen C

Reputation: 719679

I don't know if there is a recognized name for the two styles, but in structured programming terms, they can be described as "single exit" versus "multiple exit" control structures. (This also includes continue and break statements in loop constructs.)

The classical structured programming paradigm advocated single exit over multiple exit, but most programmers these days are happy with either style, depending on the context. Even classically, relaxation of the "single exit" rule was acceptable when the resulting code was more readable.

(One needs to remember that structured programming was a viewed as the antidote to "spaghetti" programming, particularly in assembly language, where the sole control constructs were conditional and non-conditional branches.)

Upvotes: 3

Forketyfork
Forketyfork

Reputation: 7810

One could call it "multiple returns" and "single return". But I wouldn't call it a style, you may want to use both approaches, depending on readability in any particular case.

Single return is considered a better practice in general, since it allows you to write more readable code with the least surprise for the reader. In a complex method, it may be quite complicated to understand at which point the program will exit for any particular arguments, and what side effects may occur.

But if in any particular case you feel multiple returns improve readability of your code, there's nothing wrong with using them.

Upvotes: 2

Melron
Melron

Reputation: 579

i would say it's about readability. The 2nd style which i prefer, gives you the opportunity to send for example messages to the user/program for any check that should stop the program.

Upvotes: 2

Related Questions