I have seen in many examples the creation of a variable with the same type as the Result, and the assignment of a function to it at the end, instead of using the result variable first.
For example, in the code inside System.JSON
class function TJSONObject.ParseJSONValue(
const Data: TArray<Byte>;
const Offset: Integer;
const ALength: Integer;
Options: TJSONParseOptions
): TJSONValue;
var
Parent: TJSONArray;
Answer: TJSONValue;
Br: TJSONByteReader;
begin
Parent := TJSONArray.Create;
Answer := nil; { Why not just use Result directly here? }
Br := TJSONByteReader.Create(
Data,
Offset,
ALength,
TJSONParseOption.IsUTF8 in Options
);
try
ConsumeWhitespaces(Br);
if
(ParseValue(Br, Parent, TJSONParseOption.UseBool in Options) = ALength)
and
(Parent.Count = 1)
then
Answer := Parent.Pop; { Why not just use Result directly here? }
Result := Answer;
finally
Parent.Free;
Br.Free;
end;
end;
Why create an Answer variable instead of just using Result?
Is this how the programmer decided to do this or is there a reason for this?
source
share