Re: Why is 'else' demanded?

Willem Bogaerts <[email protected]> Mon, 10 Jun 2013 11:41:50 +0200
Newsgroups gmane.comp.programming.refactoring
Message-ID <[email protected]>
> I have a problem. Why is there always else key word in a language?
> Since I can use return to stop the process of a flow. I hate to see
> else statement and I see I can never use else statement that only
> increase the complexity of program.

I would turn it the other way around: Why is there a return statement in
most languages? "return" in those languages means "go to end" and is the
one "go to" statement that keeps standing in the way of refactoring.

For example, you can take any code out of a function that has the same
structural level (like the inner part of a while loop) and extract it
into a function or method, UNLESS it contains a return statement, as the
return now goes to another end.

In other words: refactoring starts with "structured programming" (the
paradigm shift in the 1980s where "if - go to" statements were replaced
by "while", "repeat" and "if-then-else" statements in a lot of
languages). The return statement is not part of a structured program. (I
know, if you use the return statement as the last statement in a
function you have effectively worked around the "go to" part and you
usually have no choice if you need the return statement to pass the
return value.)

> A if statement stands for a condition that should be unique, and it
> should has no relation with anything else. So a language does not
> need else and it should not appear in a program.

But "if" is never unique, nor is that a demand. For example, my code
sometimes feature the same if statement a few times, because the inner
part of a previous if statement has "refreshed" the condition. For example:

SomeObject = null;
... try to get SomeObject from application cache ...
if SomeObject is null:
   ... read the object from the session cache ...
endif;
if SomeObject is null:
   ... create a new one from database data...
endif;

The same thing is true for an "else" condition. Once the if statement
has run, the situation might have changed. The use of an "else" keyword
is than an elegant way to describe that the code should not re-evaluate
the condition.

Apart from that, it is a matter of style. For example, Python has the
phrase "It is easier to ask forgiveness than permission" and does a lot
in try/catch statements ("asking forgiveness") where others would have
used if statements ("asking permission"). It depends on your code (and
your taste) which of the forms is more descriptive.

Best regards,

Willem Bogaerts.


------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/refactoring/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/refactoring/join
    (Yahoo! ID required)

<*> To change settings via email:
    [email protected] 
    [email protected]

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/