[fpc-pascal] methods of an object to create others objects

spir denis.spir at gmail.com
Mon Jul 5 21:07:18 CEST 2010


On Mon, 5 Jul 2010 15:48:40 -0300
Marcos Douglas <md at delfire.net> wrote:

Questions of style: just a personal opinion.

> We can to use methods of an object to create others objects, or this
> is a bad programming practice?
> eg:
> var
>   obj1, obj2: TmyObject;
> begin
>   obj1 := tobj1.create; //ok
>   obj2 := obj1.createObj; // is this recommended?
>   try
>   //...
>   finally
>     obj1.free;
>     obj2.free;
>   end;
> end;

How else can one program using object orientation?

VAR
    t1, t2 : Text ;
BEGIN
    t1 := Text('abcdefghi') ;
    t2 := t1.section(3,7) ;
    t2.write ;       // 'cdefg', if I'm right ;-)
END

Or do you mean to copy a (referenced) object?

> Why I ask this:
> If not exists the variable obj2 in call obj1.createObj will happen a
> memory leak, right? Because there is not variable to release.

Do you mean using a function as a statement? (for its so-called "side effect")
*This*, in my opinion, is very bad programming practice. But it's just an opinion.
First, a function that has effects is already bad, except for recording data on the object (eg metadata, or memoization). Second, if you really want it to *do* something in addition to *making* its return value, you should make this "anomaly" obvious by writing the effect in an external proc (called by the func). So that, when you need the effect but not the value, just call the proc.


> Marcos Douglas

Denis
________________________________

vit esse estrany ☣

spir.wikidot.com



More information about the fpc-pascal mailing list