You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Sylvain Wallez <sy...@apache.org> on 2005/06/09 11:37:10 UTC

Eclipse 3.1's "Unnecessary else statement"

Hi all,

I noticed for a while that many commits are related to "making Eclipse 
happier" because of an additional 3.1 feature that flags "unnecessary 
else statement".

These changes are for constructs such as:
    if (condition) {
        return foo;
    } else {
        return bar;
    }

which are changed to:
    if (condition) {
        return foo;
    }
    return bar;

I find the second form less readable than the first one and would like 
to ask people to change their Eclipse compiler settings to ignore these 
"unnecessary else statement".

Notice that such constructs are actually a more readable way to write
    return condition ? foo : bar;
and that the reformating makes it even worse!

Where I find this construct appropriate though, is for checking 
preconditions before large bunches of code:
    if (!precond) {
        return null;
    }
    ... large section of code ...
    return result;

So please, update your settings and leave unchanged what doesn't need to 
be changed :-)

Sylvain

-- 
Sylvain Wallez                        Anyware Technologies
http://apache.org/~sylvain            http://anyware-tech.com
Apache Software Foundation Member     Research & Technology Director


Re: Eclipse 3.1's "Unnecessary else statement"

Posted by Carsten Ziegeler <cz...@apache.org>.
Sylvain Wallez wrote:

> 
> Really?
> 
>     Object result;
>     if (condition) {
>         result = foo;
>     } else {
>         result = bar;
>     }
>     return result;
> 
> or also
>     Object result = bar;
>     if (condition) {
>         result = foo;
>     }
>     return result;
> 
> Doesn't look better to me.
> 
No no, wait a second - the first one is the way a would write it and it
doesn't have an unnecessary else statement as it doesn't have a return
statement.

> Ok, I'll write "return cond ? foo : bar" from now on which is a single 
> return statement...
> 
Whatever. As others already stated, whoever writes the code decides how
it's written (apart from the few coding standards we agreed on and use).

Carsten
-- 
Carsten Ziegeler - Open Source Group, S&N AG
http://www.s-und-n.de
http://www.osoco.org/weblogs/rael/

Re: Eclipse 3.1's "Unnecessary else statement"

Posted by Sylvain Wallez <sy...@apache.org>.
Carsten Ziegeler wrote:

>Sylvain Wallez wrote:
>  
>
>>Hi all,
>>
>>I noticed for a while that many commits are related to "making Eclipse 
>>happier" because of an additional 3.1 feature that flags "unnecessary 
>>else statement".
>>
>>These changes are for constructs such as:
>>    if (condition) {
>>        return foo;
>>    } else {
>>        return bar;
>>    }
>>
>>which are changed to:
>>    if (condition) {
>>        return foo;
>>    }
>>    return bar;
>>
>>    
>>
>
>  
>
>>So please, update your settings and leave unchanged what doesn't need to 
>>be changed :-)
>>
>>    
>>
>Oh no, you're starting one of the famous code formatting threads...I'm
>just waiting for someone pointing out that the brackets should be in a
>new line...
>Seriously, I think a method should only have *one single* return
>statement, which makes imho code even more readable.
>  
>

Really?

    Object result;
    if (condition) {
        result = foo;
    } else {
        result = bar;
    }
    return result;

or also
    Object result = bar;
    if (condition) {
        result = foo;
    }
    return result;

Doesn't look better to me.

Ok, I'll write "return cond ? foo : bar" from now on which is a single 
return statement...

Sylvain

-- 
Sylvain Wallez                        Anyware Technologies
http://apache.org/~sylvain            http://anyware-tech.com
Apache Software Foundation Member     Research & Technology Director


Re: Eclipse 3.1's "Unnecessary else statement"

Posted by Unico Hommes <un...@apache.org>.
Carsten Ziegeler wrote:
> Sylvain Wallez wrote:
> 
>>Hi all,
>>
>>I noticed for a while that many commits are related to "making Eclipse 
>>happier" because of an additional 3.1 feature that flags "unnecessary 
>>else statement".
>>
>>These changes are for constructs such as:
>>    if (condition) {
>>        return foo;
>>    } else {
>>        return bar;
>>    }
>>
>>which are changed to:
>>    if (condition) {
>>        return foo;
>>    }
>>    return bar;
>>
> 
> 
>>So please, update your settings and leave unchanged what doesn't need to 
>>be changed :-)
>>
> 
> Oh no, you're starting one of the famous code formatting threads...I'm
> just waiting for someone pointing out that the brackets should be in a
> new line...

Just for play, here is my religion: I believe it is only control
statements that need to start on a new line, brackets are merely
punctuation:

if (condition) {
  return foo;
}
else {
  return bar;
}

:-P

> Seriously, I think a method should only have *one single* return
> statement, which makes imho code even more readable.

Seriously, you have a point. But I don't think it is worth the effort to
argue it or to have everyone abide. The "unnecessary else statement" fix
Sylvain points out is indeed silly as the proposed improvement by
Eclipse results in code that is actually less clear.

--
Unico


Re: Eclipse 3.1's "Unnecessary else statement"

Posted by Reinhard Poetz <re...@apache.org>.
Carsten Ziegeler wrote:
> Carsten Ziegeler wrote:
> 
> 
>>>So please, update your settings and leave unchanged what doesn't need to 
>>>be changed :-)
>>>
>>
>>Oh no, you're starting one of the famous code formatting threads...I'm
>>just waiting for someone pointing out that the brackets should be in a
>>new line...
>>Seriously, I think a method should only have *one single* return
>>statement, which makes imho code even more readable.
>>
> 
> I forgot to say, that I like the style without unnecessary else
> statements more :) - I don't care about settings of an ide telling me
> how I should format my code :)

Well, I like the style without unnecessary else statments more too and find it 
more readable (AbstractWidget):

-----------------------------------------------------------------------
// Test validators from the widget definition
if (!getDefinition().validate(this)) {
     // Failed
     return false;
} else {
     // Definition successful, test local validators
     if (this.validators == null) {
         // No local validators
         return true;
     } else {
         // Iterate on local validators
         Iterator iter = this.validators.iterator();
         while(iter.hasNext()) {
             WidgetValidator validator = (WidgetValidator)iter.next();
             if (!validator.validate(this)) {
                 return false;
             }
         }
         // All local iterators successful
         return true;
     }
}
-----------------------------------------------------------------------
if (!getDefinition().validate(this)) {
     // Failed
     return false;
}
// Definition successful, test local validators
if (this.validators == null) {
     // No local validators
     return true;
}

// Iterate on local validators
Iterator iter = this.validators.iterator();
while(iter.hasNext()) {
     WidgetValidator validator = (WidgetValidator)iter.next();
     if (!validator.validate(this)) {
         return false;
     }
}
// All local iterators successful
return true;
-----------------------------------------------------------------------

But I think we can agree that people who like the other form better only change 
it, if they touch existing code for some other reasons than for reformatting. 
For new code the person who writes it decides.

-- 
Reinhard Pötz           Independent Consultant, Trainer & (IT)-Coach 

{Software Engineering, Open Source, Web Applications, Apache Cocoon}

                                        web(log): http://www.poetz.cc
--------------------------------------------------------------------

Re: Eclipse 3.1's "Unnecessary else statement"

Posted by Carsten Ziegeler <cz...@apache.org>.
Carsten Ziegeler wrote:

> 
>>So please, update your settings and leave unchanged what doesn't need to 
>>be changed :-)
>>
> 
> Oh no, you're starting one of the famous code formatting threads...I'm
> just waiting for someone pointing out that the brackets should be in a
> new line...
> Seriously, I think a method should only have *one single* return
> statement, which makes imho code even more readable.
> 
I forgot to say, that I like the style without unnecessary else
statements more :) - I don't care about settings of an ide telling me
how I should format my code :)

Carsten

-- 
Carsten Ziegeler - Open Source Group, S&N AG
http://www.s-und-n.de
http://www.osoco.org/weblogs/rael/

Re: Eclipse 3.1's "Unnecessary else statement"

Posted by Carsten Ziegeler <cz...@apache.org>.
Sylvain Wallez wrote:
> Hi all,
> 
> I noticed for a while that many commits are related to "making Eclipse 
> happier" because of an additional 3.1 feature that flags "unnecessary 
> else statement".
> 
> These changes are for constructs such as:
>     if (condition) {
>         return foo;
>     } else {
>         return bar;
>     }
> 
> which are changed to:
>     if (condition) {
>         return foo;
>     }
>     return bar;
> 

> 
> So please, update your settings and leave unchanged what doesn't need to 
> be changed :-)
> 
Oh no, you're starting one of the famous code formatting threads...I'm
just waiting for someone pointing out that the brackets should be in a
new line...
Seriously, I think a method should only have *one single* return
statement, which makes imho code even more readable.

Carsten
-- 
Carsten Ziegeler - Open Source Group, S&N AG
http://www.s-und-n.de
http://www.osoco.org/weblogs/rael/