Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:54:55+00:00 2026-06-12T23:54:55+00:00

Possible Duplicate: Should a function have only one return statement? My teacher took points

  • 0

Possible Duplicate:
Should a function have only one return statement?

My teacher took points off on some java code I wrote (I’m still getting an A, this is just the first non 100% grade I’ve gotten in the java course) I’m not going to argue with the teacher on the point, but I would like to get some advice from actual programmers. Here is the offending piece of code:

private char byte2suit(byte val) {
    switch(val) {
        case 0: return 's';
        case 1: return 'c';
        case 2: return 'h';
        case 3: return 'd';
        }
    //fallback value
    return 'h';
    }

In my opinion this is much clearer that initalizing a return value, assigning in the case (and adding break; every line) then returning the value. Of course, in code other people see, my opinion isn’t all powerful, so I would like to know what you guys think about multiple return statements in java (or C/C++ for that matter), if you do use multiple return statements do you use it more than the “if (this) return a; else return b;” statement? If you don’t use multiple return statements, can you give a really convincing reason ( unreadable code is not a reason if the above code IS readable and is the maximum extent of my multiple return coding practice )

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:54:56+00:00Added an answer on June 12, 2026 at 11:54 pm

    Some academics believe that there should only be one return statement at the end of the function. They believe that multiple return statement make the code impure in some way.

    However most language designers and professonal programmers disagree with this purist view. Multiple return statements can be confusing if your method is long and complicated (but if this is the case, you have bigger problems then multiple return statements). But often multiple return statements can make the code more readable.

    Your code is fine, apart from one change I’d make: use default.

    switch(val) {
        case 0: return 's';
        case 1: return 'c';
        case 2: return 'h';
        case 3: return 'd';
        default: return 'h';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Should a function have only one return statement? Hello, gcc 4.4.4 c89
Possible Duplicate: Should a function have only one return statement? This is what I
Possible Duplicate: Why should a function have only one exit-point? As a CS student
Possible Duplicate: Java: Always override equals? should I override equals function for any class
Possible Duplicate: How many lines of code should a function/procedure/method have? I would like
Possible Duplicate: F# equivalent of LINQ Single This function should return the first element
Possible Duplicate: How many lines of code should a function/procedure/method have? Out team has
Possible Duplicate: Should C++ eliminate header files? In languages like C# and Java there
Possible Duplicate: Tasks should show up only if the user has been assigned it
Possible Duplicate: Why should the interface for a Java class be prefered? When should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.