Re: Re: Why is 'else' demanded?

zhanghongliang <[email protected]> Thu, 13 Jun 2013 11:25:54 +0800
Newsgroups gmane.comp.programming.refactoring
Message-ID <[email protected]>
I've known what you mean. ' UNLESS it contains a return statement, as there=
turn now goes to another end.' =0D
The sentence tells me why the IDE I use doesn't support the refactor a seri=
es statements including return in =0D
a function.=0D
' Once the if statement has run, the situation might have changed.' I never=
 use try catch statements to do the job=0D
like you describe, so I don't know the ifs' use in the condition.=0D
Maybe 'it is a matter of style', the freedom of a programmer is important.=
=0D
=0D
Thank your answers.=0D
=0D
Best regards,=0D
ZhangHong-liang=0D
=0D
=0D
=0D
=E5=BC=A0=E7=BA=A2=E4=BA=AE   =0D
=0D
M:18640223030=0D
DL: 010=EF=BC=8D82870058=0D
=E4=B8=8A=E6=B5=B7=E9=9B=B7=E8=85=BE=E8=BD=AF=E4=BB=B6=E6=9C=89=E9=99=90=E5=
=85=AC=E5=8F=B8 (ShangHai Raxtone Limited Company)=0D
=E5=8C=97=E4=BA=AC=E5=B8=82=E6=B5=B7=E6=B7=80=E5=8C=BA=E4=B8=AD=E5=85=B3=E6=
=9D=91=E5=BD=A9=E5=92=8C=E5=9D=8A=E8=B7=AF11=E5=8F=B7=E5=8D=8E=E4=B8=80=E6=
=8E=A7=E8=82=A1=E5=A4=A7=E5=8E=A6201=E5=AE=A4 (Room 201, Buliding HuaYi con=
trol, No.11, CaiHeFang Road, Beijing , China)=0D
Fax:82870098=0D
=0D
=0D
From: Willem Bogaerts=0D
Date: 2013-06-10 17:41=0D
To: refactoring=0D
Subject: Re: [refactoring] Why is 'else' demanded?=0D
  =0D
=0D
> I have a problem. Why is there always else key word in a language?=0D
> Since I can use return to stop the process of a flow. I hate to see=0D
> else statement and I see I can never use else statement that only=0D
> increase the complexity of program.=0D
=0D
I would turn it the other way around: Why is there a return statement in=0D
most languages? "return" in those languages means "go to end" and is the=0D
one "go to" statement that keeps standing in the way of refactoring.=0D
=0D
For example, you can take any code out of a function that has the same=0D
structural level (like the inner part of a while loop) and extract it=0D
into a function or method, UNLESS it contains a return statement, as the=0D
return now goes to another end.=0D
=0D
In other words: refactoring starts with "structured programming" (the=0D
paradigm shift in the 1980s where "if - go to" statements were replaced=0D
by "while", "repeat" and "if-then-else" statements in a lot of=0D
languages). The return statement is not part of a structured program. (I=0D
know, if you use the return statement as the last statement in a=0D
function you have effectively worked around the "go to" part and you=0D
usually have no choice if you need the return statement to pass the=0D
return value.)=0D
=0D
> A if statement stands for a condition that should be unique, and it=0D
> should has no relation with anything else. So a language does not=0D
> need else and it should not appear in a program.=0D
=0D
But "if" is never unique, nor is that a demand. For example, my code=0D
sometimes feature the same if statement a few times, because the inner=0D
part of a previous if statement has "refreshed" the condition. For example:=
=0D
=0D
SomeObject =3D null;=0D
... try to get SomeObject from application cache ...=0D
if SomeObject is null:=0D
... read the object from the session cache ...=0D
endif;=0D
if SomeObject is null:=0D
... create a new one from database data...=0D
endif;=0D
=0D
The same thing is true for an "else" condition. Once the if statement=0D
has run, the situation might have changed. The use of an "else" keyword=0D
is than an elegant way to describe that the code should not re-evaluate=0D
the condition.=0D
=0D
Apart from that, it is a matter of style. For example, Python has the=0D
phrase "It is easier to ask forgiveness than permission" and does a lot=0D
in try/catch statements ("asking forgiveness") where others would have=0D
used if statements ("asking permission"). It depends on your code (and=0D
your taste) which of the forms is more descriptive.=0D
=0D
Best regards,=0D
=0D
Willem Bogaerts.=0D
=0D


[Non-text portions of this message have been removed]



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

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]=20
    [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/