• Multiple Return Statements in Java
    20 replies, posted
I have heard that in general that multiple return statements in a method is bad. I understand why, but what if you are coding something very very simple. This is what my code is, and I have several methods that look very similar to it, but the basis is the same. [code] public boolean setAge(int a){ if(highLow(LOW_AGE,HIGH_AGE,a)){ age=a; return true; }else{ return false; } } [/code] How could this ever go wrong?? It will ONLY ever return true if it meets the conditions, otherwise, false. So do you think this is ok in general? When you know for sure that multiple values will not be returned?
That's fine.
Why would multiple return statements be bad? They're incredibly useful
They are extremely useful when you have for example 5 conditions where you want to exit the method and only one condition where you want to continue it. Instead of having to use an if and a new scope (or even worse, nested ifs) you can just use one if line per condition and return.
[QUOTE=bull3tmagn3t;35389671]I have heard that in general that multiple return statements in a method is bad. I understand why, but what if you are coding something very very simple.[/QUOTE] I dont understand why, and i would love an explanation as to why this is a bad thing Languages implement features that you are meant to use. Not using them because they are "bad" seems a little wasteful
multiple values won't ever be returned. when a return statement is hit the method is done and over with and will not continue (meaning you can have unreachable code) perhaps op means that branching / if statements are bad (for pipe-lining and all that)?
There's nothing wrong with it, but I would put it like this: [code] public boolean setAge(int a){ if(highLow(LOW_AGE,HIGH_AGE,a)){ age=a; return true; } return false; } [/code] It's just a personal preference, but without the else the return false looks more like a catch-all than it did before.
Multiple return statements are good. It prevents unnecessary nesting which improves readability.
Heck you could even do this if you really only want one return statement [code] public boolean setAge(int a){ boolean b = highLow(LOW_AGE,HIGH_AGE,a); if(b){ age=a; } return b; } [/code] It's functionally equivalent though; it's all preference.
[url]http://programmers.stackexchange.com/questions/118703/where-did-the-notion-of-one-return-only-come-from[/url] tl;dr With older languages like C it can lead to problems so it stuck around and the rule has been misused. With modern languages you don't need to worry about it.
[QUOTE=Ortzinator;35394733][url]http://programmers.stackexchange.com/questions/118703/where-did-the-notion-of-one-return-only-come-from[/url] tl;dr With older languages like C it can lead to problems so it stuck around and the rule has been misused. With modern languages you don't need to worry about it.[/QUOTE] What? The answers seem to indicate that the problem is with the programmer forgetting to release memory, which is your own damn fault. And it has nothing to do with the age of the language, but simply whether or not the language forces you to allocate memory manually, or if it does it automatically for you like with java. In either case, its an unnecessary rule With java, the "one return statement only" is clearly unnecessary.
As for your setAge function, it shouldn't return anything and you should be using exceptions instead.
[QUOTE=gparent;35425930]As for your setAge function, it shouldn't return anything and you should be using exceptions instead.[/QUOTE] Yes but this is highschool programming, we haven't learned exceptions yet. (Though I do know what they are and how to use them, but the assignment was clear, to return a boolean, which if i don't do i lose marks)
[QUOTE=bull3tmagn3t;35476650]Yes but this is highschool programming, we haven't learned exceptions yet. (Though I do know what they are and how to use them, but the assignment was clear, to return a boolean, which if i don't do i lose marks)[/QUOTE] Okay, no worries then.
[QUOTE=bull3tmagn3t;35389671] [code] public boolean setAge(int a){ if(highLow(LOW_AGE,HIGH_AGE,a)){ age=a; return true; }else{ return false; } } [/code] [/QUOTE] Multiple return statements, NO PROBLEM :D BUT in this case ... [code]public boolean setAge(int a){ return (highLow(LOW_AGE,HIGH_AGE,age=a); }[/code] Now how's that for code minimalism ^^
[QUOTE=Monster Bait;35504754]Multiple return statements, NO PROBLEM :D BUT in this case ... [code]public boolean setAge(int a){ return (highLow(LOW_AGE,HIGH_AGE,age=a); }[/code] Now how's that for code minimalism ^^[/QUOTE] Wouldn't that set age=a no matter if it's true or false?
[QUOTE=WTF Nuke;35505776]Wouldn't that set age=a no matter if it's true or false?[/QUOTE] Yes, i didn't notice that damn >.< Ignore my comment (unless you could possible figure out how to make it work)!
[QUOTE=Monster Bait;35504754]Multiple return statements, NO PROBLEM :D BUT in this case ... [code]public boolean setAge(int a){ return (highLow(LOW_AGE,HIGH_AGE,age=a); }[/code] Now how's that for code minimalism ^^[/QUOTE] I think you mean this: [code]public boolean setAge(int a){ return highLow(LOW_AGE,HIGH_AGE,a)? age=a,true: false; }[/code]
That would just make it awful though unfortunately.
[QUOTE=shill le 2nd;35507181]I think you mean this: [code]public boolean setAge(int a){ return highLow(LOW_AGE,HIGH_AGE,a)? age=a,true: false; }[/code][/QUOTE] That's what I was going for, using the ternary operator but i wanted to do it without having to write the true/false .. i didn't realize you could also include other assignments like 'age=a' i thought that in a ternary operator you could only define the output of the statement so ty!
[QUOTE=Monster Bait;35516676]That's what I was going for, using the ternary operator but i wanted to do it without having to write the true/false .. i didn't realize you could also include other assignments like 'age=a' i thought that in a ternary operator you could only define the output of the statement so ty![/QUOTE] Yeah, there might be a way to do it without explicitly giving "true" and "false", but I'll have to think about it. The only requirement of the ternary operator is that you return something on each side of the colon. What I did is use the comma operator, which returns its last argument. So the expression "age=a,true" means 'evaluate "age=a", then evaluate "true", then return the result of evaluating "true"'. So I guess I could do something like "(age=a)==a" instead of what I did, but that's even more messy.
Sorry, you need to Log In to post a reply to this thread.