Gedean Dias
Gedean Dias

Reputation: 1103

How do you set the Result value?

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;

Upvotes: 3

Views: 555

Answers (7)

Peter Turner
Peter Turner

Reputation: 11435

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

Upvotes: -1

PatrickvL
PatrickvL

Reputation: 4164

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])));

Upvotes: 1

Fabio Gomes
Fabio Gomes

Reputation: 6022

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.

Upvotes: 8

Nick Dandoulakis
Nick Dandoulakis

Reputation: 43110

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.

Upvotes: 0

Despatcher
Despatcher

Reputation: 1725

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; 

Upvotes: 0

jitter
jitter

Reputation: 54605

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

Upvotes: 3

Artem Barger
Artem Barger

Reputation: 41222

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.

Upvotes: 1

Related Questions