 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
David Saracini Guest
|
Posted: Sat Jan 20, 2007 4:32 am Post subject: Methods in a class v. Global methods |
|
|
I've got a question that I think is kind of fundamental to Delphi (and probably other OO languages) and I don't know the answer to it. (and I'm not proud of admitting that - lol)
Are there any advantages to declaring global unit methods (as oppose to methods in a class) - if so, what are they? Please see the very simple (and un-compliled examples below - note: they probably will have errors in it)
Also - let's assume that TFoo is going to be created 1000s of times and stored in a TList.
{sample one - with global methods}
------------------------------------------------------
TFoo = class( TObject )
public
FFirstName : string;
FLastName : string;
end;
function GetFullName( aFoo : TFoo ) : string;
implementation
function GetFullName( aFoo : TFoo ) : string;
begin
Result := aFoo.FFirstName + ' ' + aFoo.FLastName;
end;
{sample two - with methods in class}
------------------------------------------------------
TFoo = class( TObject )
public
FFirstName : string;
FLastName : string;
function GetFullName: string; virtual;
end;
implementation
function TFoo.GetFullName: string;
begin
Result := FFirstName + ' ' + FLastName;
end;
To me, I would definitely prefer to code things like the second method. It will allow me to override functionality later if I need to - and in my opinion it's just cleaner. But that's just an opinion (and a coding style). I don't want to start a religious war here But, I do want to understand what the compiler is really doing here and understand when it might be adventageous to do it one way - or the other.
Final note: I do understand that since I declared "GetFullName" as virtual that it will require a lookup in in the VMT (and that obviously has a cost to it).
TIA and Best Regards,
David |
|
| Back to top |
|
 |
Harley Pebley Guest
|
Posted: Sat Jan 20, 2007 4:51 am Post subject: Re: Methods in a class v. Global methods |
|
|
| Quote: | Are there any advantages to declaring global unit methods (as oppose
to methods in a class) - if so, what are they? Please see the very
simple (and un-compliled examples below - note: they probably will
have errors in it)
|
Sure, but your simple example is too simple. ;-)
A case where you might want to use your example 1 style would be if you
have a set of classes for which you want a common method but cannot
change an exsiting class or introduce an ancestor at the appropriate
level. For example, you want to do something with TStrings. That's in
the VCL and you probably don't want to hack it, so to have a method
which works with a Memo's lines as well as a TStringList, you'd have a
method which isn't associated with a class and takes a TStrings
parameter. You could think of other examples; in my experience this
typically this happens with 3rd party libraries. If I need to introduce
functionallity in my own code in vastly different parts of an object
hierarchy, I tend to add an interface and might delegate the
implementation from the various places to a common class.
| Quote: | Also - let's assume that TFoo is going to be created 1000s of times
and stored in a TList.
...
To me, I would definitely prefer to code things like the second
method.
|
That would be the way I'd do it too. It clearly expresses intent and
gives the option to provide alternate results via inheritance. It's not
going to make any difference how many times the object's instantiated;
the instance size is the same in both examples.
| Quote: | But, I do want to understand what the compiler is really doing here
and understand when it might be adventageous to do it one way - or
the other.
|
AFAIU, other than the VMT lookup, they're pretty much the same.
HTH,
Harley Pebley
www.skylark-software.com |
|
| Back to top |
|
 |
David Saracini Guest
|
Posted: Sat Jan 20, 2007 5:11 am Post subject: Re: Methods in a class v. Global methods |
|
|
| Quote: | Sure, but your simple example is too simple.
|
Most examples are - :D
| Quote: |
That would be the way I'd do it too. It clearly expresses intent and
gives the option to provide alternate results via inheritance. It's not
going to make any difference how many times the object's instantiated;
the instance size is the same in both examples.
|
Really - they are both the same instance size? How is that possible? (this is what I meant in my original post by "fundamental question"). As I understand it, example two would include a pointer for the heap memory location of the code for the method. Correct? So wouldn't each instance be 4 bytes larger (assuming 32bit OS)?
Also, as I understand it, all instances of the TFoo would be pointing to the same Heap memory location. Correct? |
|
| Back to top |
|
 |
Michael Baytalsky Guest
|
Posted: Sat Jan 20, 2007 5:26 am Post subject: Re: Methods in a class v. Global methods |
|
|
| Quote: | Are there any advantages to declaring global unit methods (as oppose to
methods in a class) - if so, what are they? Please see the very simple (and
un-compliled examples below - note: they probably will have errors in it)
I'd suggest you to compile it and see ASM code generated (in CPU window on |
debug time). That will answer your question . Other then that, you are
correct, you will have cost associated with indirect calling if you
declare this method as virtual. However, if you do not, it should be
the same in most cases.
Regards,
Michael |
|
| Back to top |
|
 |
Harley Pebley Guest
|
Posted: Sat Jan 20, 2007 6:25 am Post subject: Re: Methods in a class v. Global methods |
|
|
| Quote: | That would be the way I'd do it too. It clearly expresses intent and
gives the option to provide alternate results via inheritance. It's
not going to make any difference how many times the object's
instantiated; the instance size is the same in both examples.
Really - they are both the same instance size? How is that possible?
(this is what I meant in my original post by "fundamental question").
As I understand it, example two would include a pointer for the heap
memory location of the code for the method. Correct? So wouldn't
each instance be 4 bytes larger (assuming 32bit OS)?
|
No, that method pointer is in the class, not the instance.
Try this:
====================
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
TTest1 = class
fField1: string;
fField2: string;
function Test1Method: string;
end;
TTest2 = class
fField1: string;
fField2: string;
end;
{ TTest1 }
function TTest1.Test1Method: string;
begin
result := fField1+fField2;
end;
begin
WriteLn('Test1 size: '+IntToStr(TTest1.InstanceSize));
WriteLn('Test2 size: '+IntToStr(TTest2.InstanceSize));
ReadLn;
end.
====================
| Quote: | Also, as I understand it, all instances of the TFoo would be pointing
to the same Heap memory location. Correct?
|
Yes.
Regards,
Harley Pebley
www.skylark-software.com |
|
| Back to top |
|
 |
Rudy Velthuis [TeamB] Guest
|
Posted: Sat Jan 20, 2007 6:28 am Post subject: Re: Methods in a class v. Global methods |
|
|
David Saracini wrote:
| Quote: |
I've got a question that I think is kind of fundamental to Delphi
(and probably other OO languages) and I don't know the answer to it.
(and I'm not proud of admitting that - lol)
Are there any advantages to declaring global unit methods (as oppose
to methods in a class) - if so, what are they?
|
Let me turn that around. In .NET, you use Math.Sin() and Math.Cos(), in
Delphi you use Sin() and Cos(). They do exactly the same, but I think
that calling Sin() and Cos() is more convenient.
Otherwise, there is no difference. So why make functions belong to a
class if there is no apparent need for it?
--
Rudy Velthuis [TeamB] http://rvelthuis.de/
"Half this game is ninety percent mental." -- Yogi Berra |
|
| Back to top |
|
 |
Rudy Velthuis [TeamB] Guest
|
Posted: Sat Jan 20, 2007 6:31 am Post subject: Re: Methods in a class v. Global methods |
|
|
Michael Baytalsky wrote:
| Quote: | Other then
that, you are correct, you will have cost associated with indirect
calling if you declare this method as virtual.
|
And if it is declared as static class method, there is no difference at
all, except in scope.
--
Rudy Velthuis [TeamB] http://rvelthuis.de/
"People demand freedom of speech to make up for the freedom of
thought which they avoid."
-- Soren Aabye Kierkegaard (1813-1855) |
|
| Back to top |
|
 |
Andreas Dorn Guest
|
Posted: Sat Jan 20, 2007 5:18 pm Post subject: Re: Methods in a class v. Global methods |
|
|
Rudy Velthuis [TeamB] wrote:
| Quote: | David Saracini wrote:
I've got a question that I think is kind of fundamental to Delphi
(and probably other OO languages) and I don't know the answer to it.
(and I'm not proud of admitting that - lol)
Are there any advantages to declaring global unit methods (as oppose
to methods in a class) - if so, what are they?
Let me turn that around. In .NET, you use Math.Sin() and Math.Cos(), in
Delphi you use Sin() and Cos(). They do exactly the same, but I think
that calling Sin() and Cos() is more convenient.
|
Lazy is the better word. When writing your own math libraries and
having a lot of different implementations for Sin and Cos the
existance of globals will be just irritating.
Same goes for IntToStr. A lot of people love it, but they probably never
thought about ever using UniCode.
| Quote: | Otherwise, there is no difference. So why make functions belong to a
class if there is no apparent need for it?
|
Because one day there'll be a need for it... |
|
| Back to top |
|
 |
Rudy Velthuis [TeamB] Guest
|
Posted: Sat Jan 20, 2007 5:28 pm Post subject: Re: Methods in a class v. Global methods |
|
|
Andreas Dorn wrote:
| Quote: | Lazy is the better word. When writing your own math libraries and
having a lot of different implementations for Sin and Cos the
existance of globals will be just irritating.
|
You can still qualify them.
Most people don't have different implementations of them either.
--
Rudy Velthuis [TeamB] http://rvelthuis.de/
"Silence is argument carried out by other means."
-- Ernesto "Che" Guevara (1928-1967) |
|
| Back to top |
|
 |
Andrea Raimondi Guest
|
Posted: Wed Jan 24, 2007 9:16 am Post subject: Re: Methods in a class v. Global methods |
|
|
David Saracini ha scritto:
| Quote: | {sample one - with global methods}
{sample two - with methods in class}
|
You missed one option:
(sample three - class methods)
------------------------------------------------------
TFoo = class( TObject )
public
FFirstName : string;
FLastName : string;
end;
TFooUtils = class
class function GetFullName( AFoo : TFoo ) : String;virtual;
end;
You can call it like this:
TFooUtils.GetFullName( AFoo );
This option - imho - giveth you the best of both worlds.
| Quote: | TIA and Best Regards,
|
welcome,
Andrew |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|