Assigned record checking lazarus / 62-General ---------------------------------------------------------------------- Blaazen · 2020-10-13 10:54 Why there's checking if record (TRect) is assigned (it is unit types.pp, there's more cases like this)? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function InflateRect(var Rect: TRect; dx: Integer; dy: Integer): Boolean;begin  if Assigned(@Rect) then  begin    with Rect do    begin      dec(Left, dx);      dec(Top, dy);      inc(Right, dx);      inc(Bottom, dy);    end;    Result := True;  end  else    Result := False;end;I know it is passed by reference, but still it is a record. In Delphi it is procedure, not function. ---------------------------------------------------------------------- jamie · 2020-10-13 12:28 that is incase it gets past from the pointer source and it's empty. but you are correct, that is kind of over kill... I don't recall seeing that code, where did you find that cause that is inefficient ? EDIT:  oh, I see, its in types...  bad code indeed... Edit2:  actually come to think about it, its actually most likely faster code but really, the assignment test should not be there and it could be inlined too. ---------------------------------------------------------------------- Blaazen · 2020-10-13 12:54 I also didn't see the assignment test elsewhere. If someone wants to play with pointers then he/she must be responsible - example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var p: PWord;begin  p:=nil;  Form1.KeyDown(p^, []);end;Here the first parameter is also passed by reference and there's no checking, no compiler complaints, it simply crashes. ---------------------------------------------------------------------- lucamar · 2020-10-13 15:13 Most probably someone used boiler-plate code without fully realizing the difference between a passed record and, say, an object. Happens all the time (at least to me ): you keep so much state in your head that some details go missing and you find yourself doing something by rote instead of thinking it more carefully. As in, say, inserting unneeded try..finally blocks and such like. ---------------------------------------------------------------------- PascalDragon · 2020-10-14 10:45 Quote from: Blaazen on October 13, 2020, 12:54:00 pmI know it is passed by reference, but still it is a record. In Delphi it is procedure, not function. I'd say that the code was simply copied from Types.OffsetRect and adjusted. I take it that no one checked whether Delphi implemented InflateRect the same. By the way: Delphi also returns False if you pass a Nil parameter to OffsetRect instead of raising an exception: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  SysUtils, Types; begin  Writeln(BoolToStr(OffsetRect(PRect(Nil)^, 0, 0), True));end. ---------------------------------------------------------------------- jamie · 2020-10-14 11:33 its a windows function in Delphi via the api  it returns false if it fails but you need to use the getlastError to know why it failed. I guess it sets the lasterror code.  I guess there could be a couple of reasons whey it would fail, nil rect or rect size is already overlapped ---------------------------------------------------------------------- PascalDragon · 2020-10-15 08:53 Quote from: jamie on October 14, 2020, 01:33:13 pmits a windows function in Delphi via the api Since it's a function declared in the Types unit it might forward to the Windows API function on Windows however on non-Windows it will be implemented directly. (Would be interesting to test whether they thought of the Rect parameter being Nil when they implemented that cross platform function )