New series of blog articles around the Pascal/Delphi language, brought by Marcos Douglas B. Santos for tmssoftware.com :
New series of blog articles around the Pascal/Delphi language, brought by Marcos Douglas B. Santos for tmssoftware.com :
Check the first article about the Exit() statement
https://www.tmssoftware.com/site/blog.asp?post=432
https://www.tmssoftware.com/site/blog.asp?post=432
Check the first article about the Exit() statement
https://www.tmssoftware.com/site/blog.asp?post=432
https://www.tmssoftware.com/site/blog.asp?post=432
Good article. I recently had a potential little bug that was related to a combination of a local variable being assigned to Result, and using Exit without parameters. It goes to show how incredibly important it is to read your compiler hints and understand them (and fix them). It also shows how important it is to understand the scope of try/finally.
ReplyDeleteNote that the original code was quite a bit more complex.
function TSomeClass.Something:Integer;
var
res: integer;
begin
res := 0;
GetResources;
try
if Condition
then begin
if AnotherCondition
then begin
res := 1; // HINT: Value assigned to res is never used
EXIT;
end
else begin
res := MoreConditionalCode;
end;
end
else res := -1;
finally
ReleaseResources;
end;
Result := (res > 0) and YetMoreConditionds;
end;
Lars Fosdal may not exists this hint if you just use Result directly instead "res" variable.
ReplyDeleteMarcos Douglas B. Santos - "Note that the original code was quite a bit more complex."
ReplyDeleteLars Fosdal I got it.
ReplyDelete