Would I be right to suspect that this pattern is characteristic of C programmers?

Would I be right to suspect that this pattern is characteristic of C programmers?

function Foo:TWhatever;
var aReturnValue:TWhatever;
begin
    aReturnValue := ...
    ...
    Result := aReturnValue;
    Exit;
end;

I'm seeing it a lot and it just seems weird. Especially when in the middle of the method I see:
          if(...) then begin
              Result := aReturnValue;
              Exit;
          end;

Seems like someone who is used to the C-style "return X" both setting the return value and exiting the method.

Comments

  1. Bruce McGee Yes, that is a good practice. It's readable code, and it does not increase the complexity of the code, because the decision points are not nested.

    Additionally, the decision points are by nature independent of each other, which is also good.

    This pattern has the added advantage of reducing ELSE blocks, which is even better.

    ReplyDelete
  2. Corrie Engelbrecht I think Lars Fosdal thought that you were one of those "Single Entry, Single Exit" at all costs fanatics, and was taking a shot at that.

    ReplyDelete
  3. Anthony Frazier I think the tone of Corrie Engelbrecht 's posts made it clear that it was a religious topic for him. I was close to closing comments on the post at the point Lars posted. You've persuaded me.

    ReplyDelete

Post a Comment