Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Unnecessary Else Clause.
Unnecessary Else Clause. [message #159625] Thu, 20 May 2004 09:11 Go to next message
Vince O'Sullivan is currently offline Vince O'SullivanFriend
Messages: 25
Registered: July 2009
Junior Member
A statement such as the following:

if (condition)
return 1;
else
return 2;

can throw up the warning (if user preferences include the option):
"Statement unnecessarily nested within else clause. The corresponding
then clause doe not complete normally."

Whilst the first statement is true can someone point me to a source of
information that would confirm the second part - about the abnormal
completion?

Thanks,
Vince.
Re: Unnecessary Else Clause. [message #159729 is a reply to message #159625] Fri, 21 May 2004 00:59 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: skoehler-remove-for-no-spam.upb.de

Vince wrote:
> A statement such as the following:
>
> if (condition)
> return 1;
> else
> return 2;
>
> can throw up the warning (if user preferences include the option):
> "Statement unnecessarily nested within else clause. The corresponding
> then clause doe not complete normally."

It does just mean, that your code is equivalent to

if (condition)
return 1;

return 2;

> Whilst the first statement is true can someone point me to a source of
> information that would confirm the second part - about the abnormal
> completion?

Well, the code "return 1;" doesn't complete (normally), since the VM
will never reach any code behind the return-statement.
Re: Unnecessary Else Clause. [message #159968 is a reply to message #159625] Fri, 21 May 2004 21:05 Go to previous messageGo to next message
Michael Greer is currently offline Michael GreerFriend
Messages: 6
Registered: July 2009
Junior Member
Vince wrote:
> A statement such as the following:
>
> if (condition)
> return 1;
> else
> return 2;
>
Probably not a good idea anyway: it is much clearer to have only one
return, at the end, and change the return value with if/else statements
along the way.
Re: Unnecessary Else Clause. [message #159975 is a reply to message #159729] Fri, 21 May 2004 21:39 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: smesh.openrules.com

Search for "Unnecessary else statement" in
http://download2.eclipse.org/downloads/drops/S-3.0M9-2004052 11200/eclipse-news-all-M9.html
- it's explained well enough.

--
Sam Mesh - http://openrules.com
Re: Unnecessary Else Clause. [message #160117 is a reply to message #159968] Sun, 23 May 2004 04:25 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: akiezun.cuthis.mit.edu.andthis

> Vince wrote:
> > A statement such as the following:
> >
> > if (condition)
> > return 1;
> > else
> > return 2;
> >
> Probably not a good idea anyway: it is much clearer to have only one
> return, at the end, and change the return value with if/else statements
> along the way.

you sure ?
that'd be really ugly and not clean at at all - the 2 returns in the code
above is _much_ better, imo
a.
Re: Unnecessary Else Clause. [message #160700 is a reply to message #160117] Tue, 25 May 2004 19:28 Go to previous messageGo to next message
Philippe Mulet is currently offline Philippe MuletFriend
Messages: 229
Registered: July 2009
Senior Member
There is no better style I think. Sometimes we write code which become more
and more complex, and in the end we don't realize that we nested code too
much. This warning is just here to provide a way to signal these. It is up
to you to figure how you want to handle these. In particular, this is why I
did tolerate the else-if scenario, which I believe is a standard pattern.

"adam kiezun" <akiezun@cuthis.mit.edu.andthis> wrote in message
news:c8p8hi$t2b$1@eclipse.org...
> > Vince wrote:
> > > A statement such as the following:
> > >
> > > if (condition)
> > > return 1;
> > > else
> > > return 2;
> > >
> > Probably not a good idea anyway: it is much clearer to have only one
> > return, at the end, and change the return value with if/else statements
> > along the way.
>
> you sure ?
> that'd be really ugly and not clean at at all - the 2 returns in the code
> above is _much_ better, imo
> a.
>
>
Re: Unnecessary Else Clause. [message #160844 is a reply to message #159975] Wed, 26 May 2004 08:30 Go to previous messageGo to next message
Vince O'Sullivan is currently offline Vince O'SullivanFriend
Messages: 25
Registered: July 2009
Junior Member
I see the reasoning and don't have a problem with it however I'm still not
sure what is meant by the explanation that the then clause never
"completes normally". What exactly is the abnormality that is being
implied? To me it just looks like a style issue.

Example of then clause not completing "normally":
if (condition)
return 1;
else
return 2;

Example of then clause completing "normally":
if (condition)
return 1;

return 2;


Sam Mesh wrote:
> Search for "Unnecessary else statement" in
>
http://download2.eclipse.org/downloads/drops/S-3.0M9-2004052 11200/eclipse-news-all-M9.html
> - it's explained well enough.
Re: Unnecessary Else Clause. [message #160939 is a reply to message #160844] Wed, 26 May 2004 12:53 Go to previous messageGo to next message
Philippe Mulet is currently offline Philippe MuletFriend
Messages: 229
Registered: July 2009
Senior Member
The wording is inspired from the JLSpec 2nd edition section 14.1.

"Vince" <vjosullivan@hotmail.com> wrote in message
news:c91kid$bqv$1@eclipse.org...
> I see the reasoning and don't have a problem with it however I'm still not
> sure what is meant by the explanation that the then clause never
> "completes normally". What exactly is the abnormality that is being
> implied? To me it just looks like a style issue.
>
> Example of then clause not completing "normally":
> if (condition)
> return 1;
> else
> return 2;
>
> Example of then clause completing "normally":
> if (condition)
> return 1;
>
> return 2;
>
>
> Sam Mesh wrote:
> > Search for "Unnecessary else statement" in
> >
>
http://download2.eclipse.org/downloads/drops/S-3.0M9-2004052 11200/eclipse-news-all-M9.html
> > - it's explained well enough.
>
>
Thanks [message #161143 is a reply to message #160939] Thu, 27 May 2004 09:10 Go to previous message
Vince O'Sullivan is currently offline Vince O'SullivanFriend
Messages: 25
Registered: July 2009
Junior Member
Philippe Mulet wrote:
> The wording is inspired from the JLSpec 2nd edition section 14.1.

I see, thanks.
Vince.
Previous Topic:How to edit ant build file with a name other than "build.xml"
Next Topic:Could I change keyboard shortcut for "Content Assist"?
Goto Forum:
  


Current Time: Sat Jul 27 16:36:25 GMT 2024

Powered by FUDForum. Page generated in 0.03200 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top