Multiple Returns from a Single Method

Doorway - image licensed from Photodune
Doorway - image licensed from Photodune

It’s funny just about the only thing anyone really objected to from my recent post on Minimal Coding Style was multiple return statements.

Let’s start by looking back to where this idea stems from. As best I can tell objections to multiple returns stem from Dijkstra’s 1968 paper “Go To Statement Considered Harmful”. From David Tribble who has written a Retrospective on the letter, from the introduction:

This paper was written at a time when the accepted way of programming was to code iterative loops, if-thens, and other control structures by hand using goto statements. Most programming languages of the time did not support the basic control flow statements that we take for granted today, or only provided very limited forms of them. Dijkstra did not mean that all uses of goto were bad, but rather that superior control structures should exist that, when used properly, would eliminate most of the uses of goto popular at the time. Dijkstra still allowed for the use of goto for more complicated programming control structures.

Here is what I believe about methods:

  • Short, Short, Short – at most one screen long – anything more requires the reader to scroll up and down to understand the code.
  • Do only one thing – for the ultimate anti example of this: Munger (MacOS 7/8/9) the swiss army knife of memory allocation that did different things depending on the combination of parameters. Note the linked article doesn’t describe a fraction of what Munger did. Be afraid.
  • Have descriptive (but not verbose) name.
  • Be simple and easy for the maintainer to read – this implies reducing the complexity of the control structures.

Some reasons I dislike the single exit argument:

  • If there are cases that aren’t applicable (invalid method arguments, …) I like to exit the method early to avoid additional indentation.
  • Without early exits we have to keep track of whether each additional branch was intended to execute.
  • Without early exits the ‘result’ might accidentally get changed, meaning the wrong value is returned.
  • If more code is added later it might accidentally get run even though its author intended the method to be finished.
  • If you need to clean up use a try/finally block since even early returns pass through finally blocks.
  • If multiple return statements make a method hard to read then the method is probably too large. In addition most IDE’s will allow you to highlight the control statements in any colour you need to make them visible.

More than a few other people have written on this in recent years: Bruce Eckel, Java Think (Taylor Gauthier), Java Basics, Peter Ritchie.

Do the person reading your code in the future a favour. Use early return statements to minimize the complexity in your code.

Image via: https://photodune.net/

Mark Levison

Mark Levison

Mark Levison has been helping Scrum teams and organizations with Agile, Scrum and Kanban style approaches since 2001. From certified scrum master training to custom Agile courses, he has helped well over 8,000 individuals, earning him respect and top rated reviews as one of the pioneers within the industry, as well as a raft of certifications from the ScrumAlliance. Mark has been a speaker at various Agile Conferences for more than 20 years, and is a published Scrum author with eBooks as well as articles on InfoQ.com, ScrumAlliance.org and AgileAlliance.org.

Get Certified

Explore what Scrum is and how to make it work for you in our Scrum Certification training. Hands-on learning will guide you to improve teamwork, deliver quick feedback, and achieve better products and results.

About this course

Focuses on the role of the team and the ScrumMaster. Get the skills and practical experience necessary to improve teamwork, take the exam, and advance your career with a certification that is in high demand today. Often the best fit for anyone new to Scrum.

Learning and Benefits

Relatable Scenarios

Learn on-the-job applications of key Scrum concepts, skills, principles, along with practical solutions that you can apply the next day for difficult, real-life situations.

Respected Certification

Everything you need to earn your Scrum Alliance® ScrumMaster certification, including exam fee and membership, and so much more.

Practical Exercises

With focus on the challenges that real teams face, and tools to dig deeper. You don’t need more boring Scrum theory. You need something you can sink your teeth into to see immediate results.

Jargon-Free Learning

This workshop is not just for software development or people with a computer science degree. We’ve helped many non-software teams with Scrum.

Career Advancement

Use Scrum knowledge to standout at work, get paid more, and impress your customer, all without burning out.

Ongoing Support

Our active Scrum community forum is a safe place to ask questions. Long after you earn the Certified Scrum Master certification, you will have access to the forum, course materials, and additional valuable resources.