views:

303

answers:

7
function MyFunc: Boolean;
begin
    if eval then
    Result := True
    else
        Result := False;

    /* Note it's a fancy example, I know that in this case I can do: Result := Eval */
end;

OR

function MyFunc: Boolean;
begin
    Result := False;

    if eval then
        Result := True;

/* good by else statement */

end;
+1  A: 

It doesn't actually matter, since modern compilers knows how to "eat" it and optimize it, therefore you will receive almost same instructions ,maybe in different execution order. For my taste second way is more clear for reading.

Artem Barger
I think the second is much more readable too.
Gedean Dias
+3  A: 

Why not use.

function MyFunc: Boolean;
begin
    Result := eval;
/* good by if-else statement */
end;

The result is the same with either of the 3 variants. Performance wise there is basically no difference.

Only difference is in readability. If the function is really this simple why bother using an if statement

jitter
Because the "eval" could be much complicated, also he's mentioned this in his question.
Artem Barger
@jitter: that's right man, but look at the comment! It's a very litte example. Not all return types are booleans!
Gedean Dias
Seems I overread that. Sorry. I know that not all return types are boolean. What are you trying to say? The question itself isn't that clear so it's difficult to give a good answer. But if eval evaluates to a truthvalue then no if statement is needed. I dont't use Delphi is there something like if (1+1) ... allowed?
jitter
Not all result types are booleans, hence we have IfThen function ;-). Lame sample for string result: Result:=IfThen(eval,'right','not');
Ertugrul Tamer Kara
A: 

Is the real answer - that you simply set result to a value :)

Note you can use result as a normal variable within the function.

e.g.

function DoSomeStuff: Boolean;
Begin
  Result := (evaulate some conditions);
  if Result then
  begin
   //Do good stuff
  end;
end;
Despatcher
A: 

I use the 2nd way when the function has many:

 if (...) then
 begin
   [...]
   result := default_value;
   exit
 end;

check (or error) conditions. I don't want to repeat "result := default_value;" in each case.

Nick D
+7  A: 

This really depends on the method's complexity, you should always aim for readability, these examples for me are all fine

function MyFunc: Boolean;
begin
   Result := False;
   if (Something or SomethingElse) and Whatever then
     Result := True;
end;


function MyFunc: Boolean;
begin
  Result := (Something or SomethingElse) and Whatever;
end;


function MyFunc: Boolean;
begin
   Exit((Something or SomethingElse) and Whatever);
end;

function MyFunc: Boolean;
begin
  if (Something or SomethingElse) and Whatever then
     Result := True
  else
     Result := False;
end;

I, personaly, like to avoid else statments and write as few lines of code as possible, so I would go with example 2, but example 1 is fine too, options 3 and 4 isn't much readable IMO.

I think that if you give this 4 examples to a beginner, the first one is the easiest to understand.

Fabio Gomes
I personally like your example 1 best as it makes it more obvious to me that the default result is "false". I just wanted to note that I think that your Example 3 (Exit(result)) is new to Delphi 2009 just in case the OP has an earlier version and tries it.
MarkF
Thats right, example 3 is new to Delphi 2009.
Fabio Gomes
It's a common extension. FPC has it since 1998 or so.
Marco van de Voort
+1  A: 

I like to avoid unnecessary assignments, so I tend to use either

if eval then
begin
  // yada yada
  Result := True
end
else    
  Result := False;

or, when there's no surrounding code, this :

Result := eval;

One other thing to keep in mind though, is that branching in time-critical code can have a negative impact on performance. In some situations, updating values multiple times can be faster, if it can be combined with branch-prevention. Here's an example :

for i := 0 to Length(aArray) - 1 do
  if Assigned(aArray[i]) then
    Inc(AssignedCounter);

This code could run faster if written like this :

for i := 0 to Length(aArray) - 1 do
  Inc(AssignedCounter, Ord(Assigned(aArray[i])));
PatrickvL
Interesting comment, although I think in this case performance shouldn't be the deciding factor (and the compiler will probably generate the same code anyway)...so readability should go first
Smasher
@Smasher : Delphi generates different code for the above examples. The version without an "if" prevents branching, which is something that can help performance in some situations.And indeed: This applies to time-critical code only. Normal code should be 'optimized' for the human reader/editor ;-)
PatrickvL
A: 

you can also do MyFunc := true; which has the same meaning as Result := true;

Peter Turner
...and you would basically have the same two alternatives, so this doesn't answer the question at all.
Smasher
Yeah, but if this guy was writing a lexer or some sort of code highlighter, like Rob was suggesting, he'd want to know that. I can't really tell what this question was asking anyway.
Peter Turner