 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
. Guest
|
Posted: Sat Jun 11, 2005 5:51 am Post subject: simple coding style question |
|
|
If I have a method, is it better to write it so it has a single exit point
and is (marginally) more complicated or so it has more than one exit point
eg:
public int crap(int data){
int result;
if(data > 0){
result = data;
}
else{
result = -1;
}
return result;
}
or is this better:
public int crap(int data){
if(data > 0){
return data;
}
else{
return -1;
}
}
Or does it come down to personal prefference?
Thanks for your help
Regards
Michael
|
|
| Back to top |
|
 |
Bjorn Abelli Guest
|
Posted: Sat Jun 11, 2005 11:06 am Post subject: Re: simple coding style question |
|
|
"." <.@.com> wrote...
| Quote: | If I have a method, is it better to write it so it has a
single exit point and is (marginally) more complicated or
so it has more than one exit point eg:
|
[snipped]
| Quote: | Or does it come down to personal prefference?
|
If it's only in these minor methods, where the flow can be seen in one
glance, it really comes down to your own personal preference (or if the
department have a dierct coding standard). In these examples it can be
slightly more efficient to make use of more exit points, as you don't have
to make the extra statements for storing the results.
However, to build larger methods, there's the old but functioning concept of
modelling the flow with "Jackson Structured Programming" (another thing with
the acronym JSP, but not to be confused with JavaServer Pages).
If you or your department are using that, it surely would simplify things to
have just one singular exit-point, as this type of models doesn't even allow
multiple exit points.
// Bjorn A
|
|
| Back to top |
|
 |
Dale King Guest
|
Posted: Mon Jun 13, 2005 1:40 am Post subject: Re: simple coding style question |
|
|
.. wrote:
| Quote: | If I have a method, is it better to write it so it has a single exit point
and is (marginally) more complicated or so it has more than one exit point
|
The correct answer to that question is yes. It is better to write it so
that it has one exit point or so it has more than one exit point. ;-)
One cannot say that one is always better than the other. You choose the
one that is most readable and clearly. Having a single exit point is
often better, but not always (despite what the zealots will tell you).
| Quote: | public int crap(int data){
int result;
if(data > 0){
result = data;
}
else{
result = -1;
}
return result;
}
or is this better:
public int crap(int data){
if(data > 0){
return data;
}
else{
return -1;
}
}
|
Here are 2 other alternatives:
public int crap( int data )
{
int result = data;
if( data <= 0 )
{
result = -1;
}
return result;
}
I would favor this over your choices. It has one exit point and the if
only handles the exceptional case.
public int crap( int data )
{
if( data <= 0 )
{
return -1;
}
return data;
}
I think this one is a very poor choice. If you *are* going to have
multiple exit points then it is best to make it obvious by using control
structures as you did with the if-else. The example here is so simple
that it makes little difference, but in more real code the more clues
you can give to the reader the more readable the code will be.
| Quote: | Or does it come down to personal prefference?
|
NO! NO! NO! It does not come down to personal preference! It comes down
to making the code as readable as possible. That often requires looking
beyond your personal preference and trying to come up with rules that
say always do so-and-so.
--
Dale King
|
|
| Back to top |
|
 |
Dale King Guest
|
Posted: Mon Jun 13, 2005 1:40 am Post subject: Re: simple coding style question |
|
|
.. wrote:
| Quote: | If I have a method, is it better to write it so it has a single exit point
and is (marginally) more complicated or so it has more than one exit point
|
The correct answer to that question is yes. It is better to write it so
that it has one exit point or so it has more than one exit point. ;-)
One cannot say that one is always better than the other. You choose the
one that is most readable and clearly. Having a single exit point is
often better, but not always (despite what the zealots will tell you).
| Quote: | public int crap(int data){
int result;
if(data > 0){
result = data;
}
else{
result = -1;
}
return result;
}
or is this better:
public int crap(int data){
if(data > 0){
return data;
}
else{
return -1;
}
}
|
Here are 2 other alternatives:
public int crap( int data )
{
int result = data;
if( data <= 0 )
{
result = -1;
}
return result;
}
I would favor this over your choices. It has one exit point and the if
only handles the exceptional case.
public int crap( int data )
{
if( data <= 0 )
{
return -1;
}
return data;
}
I think this one is a very poor choice. If you *are* going to have
multiple exit points then it is best to make it obvious by using control
structures as you did with the if-else. The example here is so simple
that it makes little difference, but in more real code the more clues
you can give to the reader the more readable the code will be.
| Quote: | Or does it come down to personal prefference?
|
NO! NO! NO! It does not come down to personal preference! It comes down
to making the code as readable as possible. That often requires looking
beyond your personal preference and trying to come up with rules that
say always do so-and-so.
--
Dale King
|
|
| Back to top |
|
 |
Leon Guest
|
Posted: Mon Jun 13, 2005 8:13 am Post subject: Re: simple coding style question |
|
|
"." <.@.com> wrote
| Quote: | If I have a method, is it better to write it so it has a single exit point and
is (marginally) more complicated or so it has more than one exit point eg:
public int crap(int data){
int result;
if(data > 0){
result = data;
}
else{
result = -1;
}
return result;
}
or is this better:
public int crap(int data){
if(data > 0){
return data;
}
else{
return -1;
}
}
|
Neither is better:
public int crap(int data){
return (data > 0 ? data : -1);
}
Greetings, Leon.
|
|
| Back to top |
|
 |
Thomas G. Marshall Guest
|
Posted: Fri Jun 17, 2005 9:42 pm Post subject: Re: simple coding style question |
|
|
Dale King coughed up:
| Quote: | . wrote:
If I have a method, is it better to write it so it has a single exit
point and is (marginally) more complicated or so it has more than
one exit point
The correct answer to that question is yes. It is better to write it
so that it has one exit point or so it has more than one exit point.
One cannot say that one is always better than the other. You choose
the one that is most readable and clearly. Having a single exit point
is often better, but not always (despite what the zealots will tell
you).
|
Boy does *this* smell like another thread we've been subjected to
recently...
In any case, the multiple returns mechanism is often the clearest and most
maintainable. When that is the case, then use it. Otherwise, you'll
/often/ be building in status managing contrivances that can break over
time.
By the way, IIRC and FWIW, I think even the worst zealots allowed for
mid-procedure returns so long as they're part of the parameter checking.
--
Having a dog that is a purebred does not qualify it for breeding. Dogs
need to have several generations of clearances for various illnesses
before being bred. If you are breeding dogs without taking care as to
the genetic quality of the dog (again, being purebred is *not* enough),
you are what is known as a "backyard breeder" and are part of the
problem. Most of the congenital problems of present day dogs are
traceable directly to backyard breeding. Spay or neuter your pet
responsibly, and don't just think that you're somehow the exception and
can breed a dog without taking the care described.
|
|
| Back to top |
|
 |
Eric Sosman Guest
|
Posted: Fri Jun 17, 2005 10:10 pm Post subject: Re: simple coding style question |
|
|
Thomas G. Marshall wrote:
| Quote: | Dale King coughed up:
. wrote:
If I have a method, is it better to write it so it has a single exit
point and is (marginally) more complicated or so it has more than
one exit point
The correct answer to that question is yes. It is better to write it
so that it has one exit point or so it has more than one exit point.
One cannot say that one is always better than the other. You choose
the one that is most readable and clearly. Having a single exit point
is often better, but not always (despite what the zealots will tell
you).
Boy does *this* smell like another thread we've been subjected to
recently...
In any case, the multiple returns mechanism is often the clearest and most
maintainable. When that is the case, then use it. Otherwise, you'll
/often/ be building in status managing contrivances that can break over
time.
By the way, IIRC and FWIW, I think even the worst zealots allowed for
mid-procedure returns so long as they're part of the parameter checking.
|
Besides, "one and only one exit point" mixes poorly with
methods that exit by throwing an exception, especially if the
exception actually comes from a method further down the stack:
int method(BufferedReader rdr) throws IOException {
String line = rdr.readLine(); // exit point?!
...
return 42; // exit point
}
One *could* always do
int method(BufferedReader rdr) throws IOException {
IOException except = null;
try {
String line = rdr.readLine();
...
}
catch (IOException e) {
except = e;
}
if (except == null)
return 42;
else
throw except;
}
.... but that would be Vile Beyond Belief.
--
[email]Eric.Sosman (AT) sun (DOT) com[/email]
|
|
| Back to top |
|
 |
Patricia Shanahan Guest
|
Posted: Sat Jun 18, 2005 1:11 am Post subject: Re: simple coding style question |
|
|
Dale King wrote:
| Quote: | . wrote:
If I have a method, is it better to write it so it has a single exit
point and is (marginally) more complicated or so it has more than one
exit point
The correct answer to that question is yes. It is better to write it so
that it has one exit point or so it has more than one exit point. ;-)
One cannot say that one is always better than the other. You choose the
one that is most readable and clearly. Having a single exit point is
often better, but not always (despite what the zealots will tell you).
|
One of the strongest arguments I've seen for single exit, the difficulty
of inserting clean-up code in a multi-exit procedure, does not apply to
Java because of try-finally.
Patricia
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|