Board index » delphi » Re: return command

Re: return command


2005-12-20 08:34:36 PM
delphi68
I have to agree with Don. If you extend his sample to:
function IsValid : boolean;
begin
result := false;
if not condition1 then
begin
ShowMessage('Blah Blah');
exit;
end;
if not condition2 then
begin
ShowMessage('Blah Blah');
exit;
end;
result := true;
end;
This is in no way hard to maintain and only this easy because of the exit
statements. Exit is NOT the next goto and I have to say that WITH is definitely
more evil... Regardless there are always a few places that even those are
ok. The only bad part about Exit (last I checked) is that it doesn't go
through try..finally code. For instance,
Object := TObject.Create;
try
if Something then Exit;
finally
FreeAndNil(Object);
end;
If Something is true then it will NOT go through the finally block. That
is very dangerous. Exits should only be used on simple, preferably single
branch execution and maybe simple loops, but that doesn't mean they are evil.
Anytime the code gets more complicated the flow should be carefully thought
out and not have hastily added Aborts and Exits...
My two cents anyway...
Ryan
Quote
>I disagree and use Exit a lot because the code is *easier* to
>maintain. Tiny example:
>
>function IsValid : boolean;
>begin
>result := false;
>if not condition1 then
>exit;
>if not condition2 then
>exit;
>result := true;
>end;
You should write it like this - if you wanted to keep working for me
;)

function IsValid : boolean;
begin
result := condition1 AND condition2;
end;
- Roddy

 
 

Re: return command

Ryan McGinty writes:
Quote
The only bad part about Exit (last I checked) is that it doesn't go
through try..finally code.
It goes. There would be no point to try/finally if it didn't.
--
Leonel
 

Re: return command

Hi,
In Delphi's functions there are Result variable (or function name) for
set return value to. In C/C++ there are 'return' command which
automatically exits function. In Delphi it would be:
// return Value;
Result:=Value;
Exit;
I would like to see this command in Delphi, it would speed up
development in come cases, especially when we have funcions
with nested for loops checking for some condition. Another example is
searching list for equal value i.e.
function IndexOf(Value: pointer): integer;
begin
Result:=-1;
for i:=0 to Count - 1 do
if FList[i] = Value then
return i;
// now:
// if FList[i] = Value then begin
// Result:=i;
// Exit;
// end;
end;
What do you think?
regards
Piotr
 

Re: return command

Huh, it does now. Well, I tested it at one point (I've been using Delphi
since ver 1 - I think I checked this around ver 3) and it didn't or maybe
I was just not paying very good attention to what I was doing ;) Thanks
for the info.
Ryan
Quote
>The only bad part about Exit (last I checked) is that it doesn't go
>through try..finally code.
>
It goes. There would be no point to try/finally if it didn't.

 

Re: return command

Piotr Szturmaj writes:
Quote
Hi,

In Delphi's functions there are Result variable (or function name) for
set return value to. In C/C++ there are 'return' command which
automatically exits function. In Delphi it would be:

// return Value;
Result:=Value;
Exit;
Exit, is like Goto, almost always a sign for bad coding practice. Definitely
a no-go. I use Exits, but only for very time critical functions and after
documenting that it is really faster that way. Other than that, Exit is
evil - imho. Even more than With.
Remeber: you write code once, change it 10 times and read it 100 times. You
do not want to slow down changing and reading it. Typing in 20 more
character to make the program flow more boring is a good investment in
99,9999% or all cases.
Ralf
 

Re: return command

marc hoffman writes:
Quote
Indeed. I would pick complicated loop code and unnecessary canExitLoop
variables over a clean and simple "exit", any day. Especially with
nested loops.
Come on. With "Exit" the "var" section would be so empty. :-)
--
Regards,
Andreas Hausladen
 

Re: return command

Ralf,
Quote
Exit, is like Goto, almost always a sign for bad coding practice. Definitely
a no-go. I use Exits, but only for very time critical functions and after
documenting that it is really faster that way. Other than that, Exit is
evil - imho. Even more than With.
Indeed. I would pick complicated loop code and unnecessary canExitLoop
variables over a clean and simple "exit", any day. Especially with
nested loops.
--
marc hoffman
Chief Architect, .NET
RemObjects Software
www.chromesville.com
and the fifty-two daughters of the revolution
turn the gold to chrome
 

Re: return command

Quote
[...] especially when we have funcions with nested for loops
checking for some condition.
That's especially when you don't want to see a "return" or an "exit" for
that matter. "Break" is already bad enough.
Exit already makes the flow hard to follow and understand if the whole
loops thing doesn't fit in less than a dozen lines, it is like goto,
convenient to type and use, but a pain to maintain.
Eric
 

Re: return command

Piotr Szturmaj writes:
Quote
I would like to see this command in Delphi, it would speed up
development in come cases
That can be easily be achieved with a live template like:
<?xml version="1.0" encoding="utf-8" ?>
<codetemplate xmlns="schemas.borland.com/Delphi/2005/codetemplates"
version="1.0.0">
<template name="return" invoke="manual">
<description>
C++ like return in Delphi
</description>
<author>
Andreas Hausladen
</author>
<point name="value">
<text>
False
</text>
<hint>
Returned value
</hint>
</point>
<code language="Delphi" context="methodbody" delimiter="|">
<![CDATA[begin // return |value|
|*|Result := |value|;
|*|Exit;
end;
|end|]]>
</code>
</template>
</codetemplate>
--
Regards,
Andreas Hausladen
 

Re: return command

TJC Support writes:
Quote
Does it go through the "finally..end" block if there is no exception, or
only if an exception is raised (which is what I'd expect)?
It always goes through the finally..end block, with and without an
exception.
--
Regards,
Andreas Hausladen
 

Re: return command

I disagree and use Exit a lot because the code is *easier* to maintain. Tiny
example:
function IsValid : boolean;
begin
result := false;
if not condition1 then
exit;
if not condition2 then
exit;
result := true;
end;
function IsValid : boolean;
begin
result := false;
if condition1 then begin
if condition2 then begin
result := true;
end;
end;
Add 10 more conditions and the second version is indented off the right side
of the screen and much more difficult to follow than the first.
"Ralf Mimoun" <XXXX@XXXXX.COM>writes
Quote

Exit, is like Goto, almost always a sign for bad coding practice.
Definitely a no-go. I use Exits, but only for very time critical functions
and after documenting that it is really faster that way. Other than that,
Exit is evil - imho. Even more than With.

Remeber: you write code once, change it 10 times and read it 100 times.
You do not want to slow down changing and reading it. Typing in 20 more
character to make the program flow more boring is a good investment in
99,9999% or all cases.
 

Re: return command

"Don Strenczewilk" <XXXX@XXXXX.COM>writes
Quote
I disagree and use Exit a lot because the code is *easier* to maintain.
Tiny example:

function IsValid : boolean;
begin
result := false;

if not condition1 then
exit;

if not condition2 then
exit;

result := true;
end;
You should write it like this - if you wanted to keep working for me ;)
function IsValid : boolean;
begin
result := condition1 AND condition2;
end;
- Roddy
 

Re: return command

TJC Support writes:
Quote
Does it go through the "finally..end" block if there is no exception,
or only if an exception is raised (which is what I'd expect)?
Why? Once a a try...finally block is entered, the finally block should
*always* execute.
--
Jon Robertson
Borland Certified Advanced Delphi 7 Developer
MedEvolve, Inc
www.medevolve.com
 

Re: return command

Quote
That's especially when you don't want to see a "return" or an "exit" for
that matter. "Break" is already bad enough.

Exit already makes the flow hard to follow and understand if the whole
loops thing doesn't fit in less than a dozen lines, it is like goto,
convenient to type and use, but a pain to maintain.
I thinks it is a coding style matter, Delphi users prefer
if...then...else..., when
C user choose return statement. it is all about programmer's problem abstract
vision, some things we would do better with Result:=Value some with return
Value.
regards
Piotr
 

Re: return command

I'll be more than happy to do that as soon as you start paying me. :)
Condition1 and condition2 are boolean in the tiny example but not in real
life. Let's see.. Here's a small chunk of real code:
...
if not (qLocalDELIVER_DT.IsNull or qLocalSHIP_DT.IsNull) then
if qLocalDELIVER_DT.asDateTime < qLocalSHIP_DT.asDateTime then begin
ShowMessage('The delivery date cannot be before the ship date' +^M^J +
'Please correct this before saving.');
exit;
end;
// 3/15/2005 dms- Warn if they try to ship or deliver on a holiday
if not(qLocalDELIVER_DT.IsNull) and
(dmmodel.IsHoliday(qLocalDELIVER_DT.Value))
and (Application.MessageBox( 'Delivery date is a holiday. Continue
anyways?',
'Holiday Selected', mb_YesNo +
mb_IconWarning) <>mrYes)
then
exit;
if not(qLocalSHIP_DT.IsNull) and (dmmodel.IsHoliday(qLocalSHIP_DT.Value))
and (Application.MessageBox( 'Ship date is a holiday. Continue anyways?',
'Holiday Selected', mb_YesNo +
mb_IconWarning) <>mrYes)
then
exit;
...
"Roddy Pratt" <roddy at spam fritter dot com>writes
Quote

You should write it like this - if you wanted to keep working for me ;)

function IsValid : boolean;
begin
result := condition1 AND condition2;
end;