When should you use "return"?

I have experience in multiple languages. Some of these are “return-strict” like python and javascript forces you to use return. Other languages like elixir don’t have return statement and then we have languages like ruby where “return” is an optional statement.

My teacher says that you should always use return. Although I have heard other people saying return should be used if you want to do an earlier return. At the same time, ruby has an open syntax(in my opinion). What is the answer to this question and/or is there one?

Like semi-colons in Ruby, it would be strange to see them everywhere. Use return for early returns, when you have some kind of a condition where the rest of the work would not have to be done.

def hello(name, leaving: false)
   return 'See you later' if leaving
   'Hello #{name}!'
end

All Ruby methods return the result of the last statement evaluated, implicitly, and so return is not necessary.

When in teams where the rule is to always use return, making things explicit, then do as they do.

A couple of questions:

  • What was the language when your teacher said that?
  • Did your teacher give a reason?
  1. The teacher only teaches ruby.
  2. He said that other languages which are “return-strict” forces you to use return and therefore should you use return in ruby because there other languages forces you. I think he isn’t aware of languages there return isn’t a thing.

It’s definitely possible that this is the reason your teacher gave that advice. I don’t think it’s necessarily bad advice, but I also think that this is a stylistic choice, and more importantly, it’s very good to understand that there are many many languages with (a form of) implicit returns.

A lot of modern languages have some form of implicit return, usually when they allow shorthand definitions, just like JavaScript. JavaScript’s arrow function declaration has a block-less variant with an implicit return:

function explicit() {
  return 42;
}

const stillExplicit = () => {
  return 42;
}

const implicit = () => 42

It’s extremely likely that there are styleguides (repo, organisation, company, or standards level) that prohibit various combinations of the variants above, but they’re all functionally the same, and to me equally readable.

Nim is a very interesting one, as Nim allows implicit returns via results , and via “the last statement in a procedure or expression” (the latter just like Ruby):

proc explicit(): string =
  return "Typical explicit return"
 
proc implicitResult(): string =
  result = "Implicit return through result"
 
proc implicitLastStatement(): string =
  "Implicit return since it's last statement"

Returning a value from an expression, block, procedure, function, or method exists in a lot of different shapes and form, and not all syntax is (as you already imply) consistent across languages.

In my personal experience the following are true for Ruby:

  • In general, people omit return if it’s the last statement in a function
  • In general, early (explicit) return are acceptable and encouraged
  • In general, return inside blocks for enumeration are to be avoided or commentated because they don’t do what you may expect them to do.

Remember that for Ruby:

  • return terminates the method or lambda.
  • next terminates the block, proc, or lambda.
  • break terminates the method that invoked the proc or lambda, or yielded the block.

For the same argument, then, using semicolons there could be justified. And if that is the style guide imposed there, I would do it. But there is value in the implicit, once understood.

But, I would, as @SleeplessByte indicates, treat it as a style guide recommendation for that code base. You can strictly use them, but you will encounter a lot of code (even in the core library) that will use the implicit return.