A common condition that all programs should do is to check if variables are assigned or not.
Take the below statements:
(1)
if Assigned(Ptr) then begin // do something end;
(2)
if Ptr <> nil then begin // do something end;
What is the difference between Assigned(Ptr)
and Ptr <> nil
?
if ($var === null){ } // This checks if the variable, by type, IS null.
To check for null variables, you can use a strict equality operator ( === ) to compare the variable with null . This is demonstrated below, where the boolean expression evaluates to true for only for null and evaluates to false for other falsy values.
It means null is equal to undefined but not identical. When we define a variable to undefined then we are trying to convey that the variable does not exist . When we define a variable to null then we are trying to convey that the variable is empty.
Use "==" to check a variable's value. A "==" is used to check that the two values on either side are equal. If you set a variable to null with "=" then checking that the variable is equal to null would return true.
It's usually the same... except when you check a function...
function mfi: TObject; begin Result := nil; end; procedure TForm1.btn1Click(Sender: TObject); type TMyFunction = function: TObject of object; var f: TMyFunction; begin f := mfi; if Assigned(f) then begin ShowMessage('yes'); // TRUE end else begin ShowMessage('no'); end; if f <> nil then begin ShowMessage('yes'); end else begin ShowMessage('no'); // FALSE end; end;
With the second syntax, it will check the result of the function, not the function itself...
As far as performance, there is no difference. I personally prefer the second form as I find that humans can parse the meaning quicker.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With