Article

From:
To:
Ralf Stocker
Subject:
Re: Delphi Project X Cross GUI
Newsgroup:
embarcadero.public.delphi.non-technical

Re: Delphi Project X Cross GUI

Brad White wrote:

> 
> I'm open to being persuaded, but the only example
> I've seen was Allen's.  While he did fine job demonstrating
> some very cool things that you could do with generics
> and an anonymous procedure, it made the code much
> less readable, IMHO.
> 

+1. Entirely.

> Do you have an example where it would increase redability
> or quality?
> 
> Thanks,
> Brad.

In a perfect world it would be something like:

function GetUserName: string; begin Result:='';
using temp:=TStringList.Create do begin temp.Add('Please be so kind'); temp.Add('in order to enter'); temp.Add('your beloved name'); Result:=InputBox(temp.Text, ''); end;
if Result='' then //some bogus processing begin ShowMessage('You did not enter a name'); Result:='guest'; //whatever end; end;
There are some very interesting things to note however:
1. The above construction resembles (from *user* POV) to 'with' aliasing which is (AFAIS from QC and from here) the community's preferred solution for the 'Infamous "with" resolution'. IOW something like:
//for ex. some very big name here with tbl:=MainCustomerDataModule.ProblematicOrderDataSource.Dataset do begin tbl.SQL:='SELECT * FROM ORDERS WHERE STATUS=8'; //whatever tbl.Open; end;
- or -
with temp:=TStringList.Create do begin temp.Add('blah!'); //do something ShowMessage(temp.text); temp.Free; //<---! end;
As you see the main difference between 'with' aliasing and 'using' is that 'with' doesn't manage the lifetime of the aliased object.
In the 'using' case this gives the (at least theoretical) the possibility for some checks for the compiler such as:
var nastyRef: TStringList; //global
procedure Foo; begin    using temp:=TStringList.Create do    begin temp.Add('blah!'); //do something nastyRef:=temp; //Compiler warning like: 'nastyRef is outside' ShowMessage(temp.text);    end;    //temp destroyed here. So nastyRef is dangling. end;
And this cannot be checked at the compile time neither in the case of 'with' aliasing neither in the classical case of today which is try...finally block.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2. Another thing which perhaps you (as experienced Delphi developer) cannot see at the first glance is that
   using temp:=TStringList.Create do    begin temp.Add('blah!'); //do something //snip
gives a clear indication that the compiler will take care of the lifetime of temp and you should only concentrate on your coding (human) problem to solve.
Ok, perhaps if you'll see the code...
   temp:=TStringList.Create;    try      ... //snip
....you will automatically search for a temp.Free (or similar) in a 'finally' block. But this can be *your* habit and also can be a little bit tedious if the finally block is outside of screen. And also, the temp.free in the 'finally' block, being more manual is prone to error (you have to accomplish more steps to build a try/finally block). Using 'using' is much safer.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3. Also, a nested try/finally is (imho) much more less readable. Compare the following equivalent snippets:
MyComp := TComponent.Create(Self); try    MyComp.Name:='FooComponent';    MyComp.Tag:=1; //and many other things...    myLabel := TLabel.Create(Self);    try      myLabel.Parent:=Self;      myLabel.Caption:=MyComp.Name;      myLabel.Color:=clMagenta;      //etc. etc.      ShowModal;    finally      myLabel.Free;    end; finally    MyComp.Free; end;
---8<------8<------8<--- vs ---8<------8<------8<---

using MyComp := TComponent.Create(Self) do begin    MyComp.Name:='FooComponent';    MyComp.Tag:=1; //and many other things...    using myLabel := TLabel.Create(Self) do    begin      myLabel.Parent:=Self;      myLabel.Caption:=MyComp.Name;      myLabel.Color:=clMagenta;      //etc. etc.      ShowModal;    end; end;
....and I didn't put a 3rd level of nesting, neither a try..exception block there... :-)
Did I convinced you? :-)
--
m. Th.
On the Wings of the Wind... http://wings-of-wind.com/
FYI: Click here to see how many newsgroups are indexed
 
 
Originally created by
Tamarack Associates
Mon, 25 Nov 2024 11:33:31 UTC
Copyright © 2009-2024
HREF Tools Corp.