DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • The Role of Functional Programming in Modern Software Development
  • Best Ways to Write Clean and Quality Python Code
  • A Guide to Constructor Chaining in Java
  • Magic of Aspects: How AOP Works in Spring

Trending

  • Building Resilient Go Apps: Mocking and Testing Database Error Responses
  • The Agile Paradox
  • The AWS Playbook for Building Future-Ready Data Systems
  • Understanding Time Series Databases
  1. DZone
  2. Coding
  3. Frameworks
  4. Why Many Return Statements Are a Bad Idea in OOP

Why Many Return Statements Are a Bad Idea in OOP

Multiple return statements in a method will cause your code not to be purely object-oriented. Here you'll find an example where you can use a clean OOP approach instead of using multiple returns.

By 
Yegor Bugayenko user avatar
Yegor Bugayenko
·
Sep. 15, 15 · Opinion
Likes (7)
Comment
Save
Tweet
Share
46.6K Views

Join the DZone community and get the full member experience.

Join For Free

This debate is very old, but I have something to say too. The question is whether a method may have multiple return statements or just one. The answer may surprise you: In a pure object-oriented world, a method must have a singlereturn statement and nothing else. Yes, just a return statement and that's it. No other operators or statements. Just return. All arguments in favor of multiple return statements go against the very idea of object-oriented programming.

This is a classical example:

public int max(int a, int b) {
  if (a > b) {
    return a;
  }
  return b;
}

The code above has two return statements, and it is shorter than this one with a single return:

public int max(int a, int b) {
  int m;
  if (a > b) {
    m = a;
  } else {
    m = b;
  }
  return m;
}

More verbose, less readable, and slower, right? Right.

This is the code in a pure object-oriented world:

public int max(int a, int b) {
  return new If(
    new GreaterThan(a, b),
    a, b
  );
}

What do you think now? There are no statements or operators. No if and no >. Instead, there are objects of class If and GreaterThan.

This is a pure and clean object-oriented approach.

However, Java doesn't have that. Java (and many other pseudo OOP languages) gives us operators like if, else, switch, for, while, etc. instead of giving built-in classes, which would do the same. Because of that, we continue to think in terms of procedures and keep talking about whether two return statements are better than one.

If your code is truly object-oriented, you won't be able to have more than one return. Moreover, you will have nothing except a return in each method. Actually, you will have only two operators in the entire software — new and return. That's it.

Until we're there, let's stick with just one return and at least try to look like pure OOP.

Object-oriented programming

Published at DZone with permission of Yegor Bugayenko. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Role of Functional Programming in Modern Software Development
  • Best Ways to Write Clean and Quality Python Code
  • A Guide to Constructor Chaining in Java
  • Magic of Aspects: How AOP Works in Spring

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: