Re: pointer syntax



"swansnow" <schultz@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:1132759180.698705.131050@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[...]
> * Use var with objects, only if I intend to change the reference
> (beware of orphaned memory!)

Best practice is to avoid the issue altogether. People are sometimes
tempted to return a newly allocated object from a function, for example
a ListDirectory() function that uses a TStrings object to store the
filenames in.

What's bad about that is that the object's creation is in a different
scope than the object's destruction. This is asking for trouble. The
general pattern is

Obj:=ClassRef.Create;
try
Obj.Work;
finally
Obj.Free; Obj:=nil;
end;

and you can follow it in the above case by reading "Obj.Work" as
"ListDirectory(Obj);". The object is passed into the function, and
it fills the list for you.

procedure ListDirectory(const Names: TStrings);
var SearchResult;
begin
Names.BeginUpdate;
try
Names.Clear;
SearchResult:=FindFirst();
while (SearchResult.Found) do begin
Names.Add(SearchResult.Name);
FindNext(SearchResult);
end;
finally
Names.EndUpdate;
end;
end;


> * If I have anything else, use var if I need changes to come back.
> Avoid explicit pointers like the plague. (Again, unless I have some
> special reason to need them, like strange API calls:) )

There is a useful trick that can be played with C-style explicit
pointers and not with var parameters: you can pass null as the pointer.
If you're not interested in the function's results but only in its
side effects, this frees you from having to allocate a buffer where
the result will be stored, only to be thrown away immediately.

In the above code, consider the effect of passing in nil as the
parameter. Of course, the receiving code has to check for this every
time. Equally of course, the above function has no side effects so no
functionality is left. That's not the point; it didn't compile anyway.

Groetjes,
Maarten Wiltink


.



Relevant Pages

  • Classes / Functions / Autonomy
    ... I do get a bit wordy at times. ... var $WhyUseVars; ... Of course, then I come up with the thought, that I might want to use the second function outside of the first function, but I've written the ... Im not sure what the best way is, I know I just want to avoid needless repetition, keep things simple, and avoid the situation where Im passing the same information in chained function calls. ...
    (comp.lang.php)
  • Re: Attach a class method to event handler
    ... How do I avoid the closure of my object reference? ... var target = document.getElementById; ...
    (comp.lang.javascript)
  • had Latent-Time disabled. It had a destination block set for
    ... its var ... Remailer operators vary in their administrative techniques. ... Some avoid ...
    (sci.crypt)
  • Re: Mathing a Value and printing out a column for that value
    ... >The above will print the whole record if no field matches "var". ... >There's various ways to avoid that if you want. ... Any pointers on how to avoid this please? ... > always quote your variables in shell, unless you have a specific reason not to, so ...
    (comp.lang.awk)
  • Re: Duplicate Key Alternative for Generic Dictionary
    ... To me it sounds like you're trying to avoid creating a Department ... var d1 = new Department; ... Developers for exciting positions in medical product ... I guess pretend I have 3 departments, and 3 employees in each one. ...
    (microsoft.public.dotnet.languages.csharp)