Skip to main content
Software Engineering

Return to Question

Added appropriate tags
Link
added syntax-highlighting; slight touchups
Source Link
Deduplicator
  • 9.2k
  • 5
  • 33
  • 53

I often talk to programmers who say "Don't put multiple return statements in the same method."Don't put multiple return statements in the same method." When I ask them to tell me the reasons why, all I get is "The coding standard says so."The coding standard says so." or "It's confusing."It's confusing." When they show me solutions with a single return statement, the code looks uglier to me. For example:

if (condition)
 return 42;
else
 return 97;
if (condition)
 return 42;
else
 return 97;

"This is ugly, you have to use a local variable!"This is ugly, you have to use a local variable!"

int result;
if (condition)
 result = 42;
else
 result = 97;
return result;
int result;
if (condition)
 result = 42;
else
 result = 97;
return result;

How does this 50% code bloat make the program any easier to understand? Personally, I find it harder, because the state space has just increased by another variable that could easily have been prevented.

Of course, normally I would just write:

return (condition) ? 42 : 97;
return (condition) ? 42 : 97;

But many programmers eschew the conditional operator and prefer the long form.

Where did this notion of "one return only" come from? Is there a historical reason why this convention came about?

I often talk to programmers who say "Don't put multiple return statements in the same method." When I ask them to tell me the reasons why, all I get is "The coding standard says so." or "It's confusing." When they show me solutions with a single return statement, the code looks uglier to me. For example:

if (condition)
 return 42;
else
 return 97;

"This is ugly, you have to use a local variable!"

int result;
if (condition)
 result = 42;
else
 result = 97;
return result;

How does this 50% code bloat make the program any easier to understand? Personally, I find it harder, because the state space has just increased by another variable that could easily have been prevented.

Of course, normally I would just write:

return (condition) ? 42 : 97;

But many programmers eschew the conditional operator and prefer the long form.

Where did this notion of "one return only" come from? Is there a historical reason why this convention came about?

I often talk to programmers who say "Don't put multiple return statements in the same method." When I ask them to tell me the reasons why, all I get is "The coding standard says so." or "It's confusing." When they show me solutions with a single return statement, the code looks uglier to me. For example:

if (condition)
 return 42;
else
 return 97;

"This is ugly, you have to use a local variable!"

int result;
if (condition)
 result = 42;
else
 result = 97;
return result;

How does this 50% code bloat make the program any easier to understand? Personally, I find it harder, because the state space has just increased by another variable that could easily have been prevented.

Of course, normally I would just write:

return (condition) ? 42 : 97;

But many programmers eschew the conditional operator and prefer the long form.

Where did this notion of "one return only" come from? Is there a historical reason why this convention came about?

Minor cleanup to focus the question better.
Source Link
user22815
user22815

I often talk to Java programmers who say "Don't put multiple return statements in the same method." When I ask them to tell me the reasons why, all I get is "The coding standard says so." or "It's confusing." When they show me solutions with a single return statement, the code looks uglier to me. For example:

if (blablablacondition)
 return 42;
else
 return 97;

"This is ugly, you have to use a local variable!"

int result;
if (blablablacondition)
 result = 42;
else
 result = 97;
return result;

How does this 50% code bloat make the program any easier to understand? Personally, I find it harder, because the state space has just increased by another variable that could easily have been prevented.

Of course, normally I would just write:

return (blablablacondition) ? 42 : 97;

But many programmers eschew the conditional operator gets even less love among Java programmersand prefer the long form. "It's incomprehensible!"

Where did this notion of "one return only" come from, and? Is there a historical reason why do people adhere to it rigidlythis convention came about?

I often talk to Java programmers who say "Don't put multiple return statements in the same method." When I ask them to tell me the reasons why, all I get is "The coding standard says so." or "It's confusing." When they show me solutions with a single return statement, the code looks uglier to me. For example:

if (blablabla)
 return 42;
else
 return 97;

"This is ugly, you have to use a local variable!"

int result;
if (blablabla)
 result = 42;
else
 result = 97;
return result;

How does this 50% code bloat make the program any easier to understand? Personally, I find it harder, because the state space has just increased by another variable that could easily have been prevented.

Of course, normally I would just write:

return (blablabla) ? 42 : 97;

But the conditional operator gets even less love among Java programmers. "It's incomprehensible!"

Where did this notion of "one return only" come from, and why do people adhere to it rigidly?

I often talk to programmers who say "Don't put multiple return statements in the same method." When I ask them to tell me the reasons why, all I get is "The coding standard says so." or "It's confusing." When they show me solutions with a single return statement, the code looks uglier to me. For example:

if (condition)
 return 42;
else
 return 97;

"This is ugly, you have to use a local variable!"

int result;
if (condition)
 result = 42;
else
 result = 97;
return result;

How does this 50% code bloat make the program any easier to understand? Personally, I find it harder, because the state space has just increased by another variable that could easily have been prevented.

Of course, normally I would just write:

return (condition) ? 42 : 97;

But many programmers eschew the conditional operator and prefer the long form.

Where did this notion of "one return only" come from? Is there a historical reason why this convention came about?

Loading
Question Protected by Community Bot
edited tags
Link
Dynamic
  • 5.8k
  • 9
  • 47
  • 74
Loading
Post Made Community Wiki by Eugene - AmberPixels
edited tags
Link
Adam Lear
  • 32.1k
  • 8
  • 103
  • 126
Loading
edited tags
Link
user8
user8
Loading
Changed title to reflect the part of the question which isn't a duplicate.
Link
Steven Jeuris
  • 5.8k
  • 1
  • 32
  • 56
Loading
Removed sensasional language, and unecessary diversion to exception handling
Source Link
Eric Wilson
  • 12.1k
  • 9
  • 53
  • 78
Loading
Loading
Tweeted twitter.com/#!/StackProgrammer/status/134183679059693569
Source Link
fredoverflow
  • 7k
  • 9
  • 41
  • 46
Loading

AltStyle によって変換されたページ (->オリジナル) /