 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Eugene Goldberg Guest
|
Posted: Mon Dec 12, 2005 4:19 pm Post subject: calling class methods without instantiation of it |
|
|
I have recently discovered an interesting Object Pascal "feature" and it
looks like i am
missing some important basics..
There was a class declared, TSomeClass derived from TObject with a bunch of
utility methods...
Like GetApplicationVersionInfo function, some parsing routines etc...
Then there was a main form field declared like FSomeClass: TSomeClass...Then
in various spots
of applciation there were calls to these methods, like FSomeClass.Method1,
FSomeClass.Method2 etc...
I have been searching for FSomeClass := TSomeClass.Create; statement but was
unable to find any. But it still works...
Anyone can tell me how its possible? These methods are not decalred as
"class" methods...
Thanx,
Eugene.
|
|
| Back to top |
|
 |
Joanna Carter [TeamB] Guest
|
Posted: Mon Dec 12, 2005 4:48 pm Post subject: Re: calling class methods without instantiation of it |
|
|
"Eugene Goldberg" <egold (AT) mts-nn (DOT) ru> a écrit dans le message de news:
439da2d9$1 (AT) newsgroups (DOT) borland.com...
| Quote: | I have recently discovered an interesting Object Pascal "feature" and it
looks like i am
missing some important basics..
There was a class declared, TSomeClass derived from TObject with a bunch
of
utility methods...
Like GetApplicationVersionInfo function, some parsing routines etc...
Then there was a main form field declared like FSomeClass:
TSomeClass...Then
in various spots
of applciation there were calls to these methods, like FSomeClass.Method1,
FSomeClass.Method2 etc...
I have been searching for FSomeClass := TSomeClass.Create; statement but
was
unable to find any. But it still works...
Anyone can tell me how its possible? These methods are not decalred as
"class" methods...
|
If methods do not touch any instance variables, then those methods can be
called without a valid instance. If you are certain that this is the case,
then this is bad practice, such methods should really be marked as "class"
methods.
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer
|
|
| Back to top |
|
 |
Eugene Goldberg Guest
|
Posted: Mon Dec 12, 2005 4:52 pm Post subject: Re: calling class methods without instantiation of it |
|
|
thanx Joanna, thats the case...just a bunch of methods and no variables at
all...
"Joanna Carter [TeamB]" <joanna (AT) not (DOT) for.spam> wrote
|
|
| Back to top |
|
 |
Joanna Carter [TeamB] Guest
|
Posted: Mon Dec 12, 2005 5:00 pm Post subject: Re: calling class methods without instantiation of it |
|
|
"Eugene Goldberg" <egold (AT) mts-nn (DOT) ru> a écrit dans le message de news:
439daa91$1 (AT) newsgroups (DOT) borland.com...
| Quote: | thanx Joanna, thats the case...just a bunch of methods and no variables at
all...
|
EEEEuuugghh !!
Do you have the possibility of changing them to class methods ? It could
save someone's sanity in the future :-)
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer
|
|
| Back to top |
|
 |
Eugene Goldberg Guest
|
Posted: Mon Dec 12, 2005 5:54 pm Post subject: Re: calling class methods without instantiation of it |
|
|
is it some UK - specific spelling of short form of "Eugene" ? ))
| Quote: | Do you have the possibility of changing them to class methods ? It could
save someone's sanity in the future
|
I surely will.
Thanx again.
|
|
| Back to top |
|
 |
Rudy Velthuis [TeamB] Guest
|
Posted: Mon Dec 12, 2005 9:42 pm Post subject: Re: calling class methods without instantiation of it |
|
|
At 17:19:47, 12.12.2005, Eugene Goldberg wrote:
| Quote: | FSomeClass.Method1, FSomeClass.Method2 etc...
I have been searching for FSomeClass := TSomeClass.Create; statement
but was unable to find any.
|
That is why they are *class* methods. They don't require an instance. In
fact, you could just as well call them as TSomeClass.Method1. The Self
that gets passed is not that of an instance, but of the class itself
(unless the class methods are static, in that case they are like ordinary
non-method functions, but with a different scope, i.e. no Self is passed
at all).
Class methods can't actually access instance fields or methods.
--
Rudy.Velthuis {TeamB} http://velthuis.homepage.t-online.de/
"Politics is the art of looking for trouble, finding it everywhere,
diagnosing it incorrectly, and applying the wrong remedies."
-- Groucho Marx
|
|
| Back to top |
|
 |
Hrvoje Brozovic Guest
|
Posted: Tue Dec 13, 2005 10:15 pm Post subject: Re: calling class methods without instantiation of it |
|
|
"Eugene Goldberg" <egold (AT) mts-nn (DOT) ru> wrote
| Quote: | I have recently discovered an interesting Object Pascal "feature" and it
looks like i am
missing some important basics..
There was a class declared, TSomeClass derived from TObject with a bunch
of
utility methods...
Like GetApplicationVersionInfo function, some parsing routines etc...
Then there was a main form field declared like FSomeClass:
TSomeClass...Then
in various spots
of applciation there were calls to these methods, like FSomeClass.Method1,
FSomeClass.Method2 etc...
I have been searching for FSomeClass := TSomeClass.Create; statement but
was
unable to find any. But it still works...
Anyone can tell me how its possible? These methods are not decalred as
"class" methods...
|
Did you ever heard advice to be carefull not to Free object instance twice,
or to set it to nil after freeing? Or how usefull is FreeAndNil?.
If you did, you should follow direct line of reasoning that leads you
to conclusion that valid instance is not needed to method code to be called.
Take a look at TObject.Free. It's only purpose is to handle Self = nil case.
Otherwise, Destroy would be called for nil instance, hence AV.
Your FSomeClass.Method2 is exactly some thing.
|
|
| Back to top |
|
 |
Jens Gruschel Guest
|
Posted: Wed Dec 14, 2005 6:52 pm Post subject: Re: calling class methods without instantiation of it |
|
|
| Quote: | There was a class declared, TSomeClass derived from TObject with a bunch of
utility methods...
Like GetApplicationVersionInfo function, some parsing routines etc...
|
Converting them into class methods is a good idea. But IMO class methods
often are a sign for a bad design (there are exceptions). Often it's
procedural programming hidden behind OOP constructs (the Functional
Decomposition Antipattern). Instead of a GetApplicationVersionInfo
method you could also have an ApplicationVersionInfo class, which allows
you to enhance it later more easily (like caching the values after they
have been fetched the first time, although I admit that in this case it
probably doesn't make too much sense). You can also pass the values to
some other method easily and benefit from OOP in other ways, too.
Jens
--
Jens Gruschel
http://www.pegtop.net
|
|
| Back to top |
|
 |
Guest
|
Posted: Wed Dec 14, 2005 7:13 pm Post subject: Re: calling class methods without instantiation of it |
|
|
"Jens Gruschel" <nospam (AT) thisurldoesnotexist (DOT) com> wrote in message
| Quote: | But IMO class methods often are a sign for a bad design (there are
exceptions). Often it's procedural programming hidden behind OOP constructs
|
I agree. I tend to use them to keep things organized until I can figure out
how it *shoud* be. When I'm doing these days is revising other people's
code, so class methods are actually an improvement... kind of like a
waystation until a good design can actually get implemented. Plus, we
release frequently, so I need to make sure any changes I make will still
work -- I don't have the time to do it all in one fell swoop.
That said, if I were to design something from scratch, I would look
suspiciously at any class that had class methods and think carefully about
whether they were called for. I *do* think it's possible to overdesign. You
don't always need full-blown objects, but it's good to have the modularity
present so you can evolve real objects when you need to.
(I lean towads the incremental development, frequent release,
don't-do-everything-up-front-because-it-will-change-eventually-anyways,
style of programming. )
-Corinna
|
|
| Back to top |
|
 |
Jens Gruschel Guest
|
Posted: Wed Dec 14, 2005 7:50 pm Post subject: Re: calling class methods without instantiation of it |
|
|
| Quote: | I *do* think it's possible to overdesign.
|
Right (you are talking about the Poltergeists antipattern, just to show
off again *g*). But since many programmers tend to underdesign software
(sometimes because of lack of experience, and sometimes because of lack
of time) I rarely worry about it.
Jens
--
Jens Gruschel
http://www.pegtop.net
|
|
| Back to top |
|
 |
Diego Barros Guest
|
Posted: Thu Dec 15, 2005 9:59 am Post subject: Re: calling class methods without instantiation of it |
|
|
On 2005-12-15 06:13:52 +1100, <nutmegkat (AT) gmail (DOT) com> said:
| Quote: |
"Jens Gruschel" <nospam (AT) thisurldoesnotexist (DOT) com> wrote in message
But IMO class methods often are a sign for a bad design (there are
exceptions). Often it's procedural programming hidden behind OOP
constructs
That said, if I were to design something from scratch, I would look
suspiciously at any class that had class methods and think carefully
about whether they were called for. I *do* think it's possible to
overdesign. You don't always need full-blown objects, but it's good to
have the modularity present so you can evolve real objects when you
need to.
|
You'll be looking at the .NET framework suspiciously then, with its
proliferation of static methods. Of course, you won't be able to get
your methods outside a class in C#.
|
|
| Back to top |
|
 |
Guest
|
Posted: Thu Dec 15, 2005 3:22 pm Post subject: Re: calling class methods without instantiation of it |
|
|
"Diego Barros" <diego(at)heyboyo(dot)com> wrote in message
| Quote: | You'll be looking at the .NET framework suspiciously then, with its
proliferation of static methods. Of course, you won't be able to get
your methods outside a class in C#.
|
I haven't done anything with .NET, so I can't really comment. However, I
have worked with Java, where everything is a class. But if you look at
classes as containers, then it makes sense to have things like Math.random,
Math.cos, etc. I wouldn't want to have to create a math object just because
I wanted a random number. I think it makes sense for self-contained methods,
which are related to each other in some way, to be put into the same class,
as static methods. But this is more "library"-type code. I wouldn't think
it'd be a good idea for general application design.
I'm no expert, but it seems to me that static methods do break the OO
paradigm a bit, and so should be used with care. [I think all good systems
provide a way for the guiding philosophy to be broken - trust the user,
don't control them, just provide guidelines, support, and suggestions. This
is one reason I like working in Linux instead of Windows...]
-Corinna
|
|
| Back to top |
|
 |
Jens Gruschel Guest
|
Posted: Thu Dec 15, 2005 6:54 pm Post subject: Re: calling class methods without instantiation of it |
|
|
| Quote: | I wouldn't want to have to create a math object just because
I wanted a random number.
|
Well, for Math.cos and similar functions that's alright. But random
functions rely on some seed, which can perfectly be done with a class.
AFAIK in .net you need to instanciate a random generator object if you
need random numbers, and IMO that's a good approach. This way you can
have several generators which work independently (which can be important
if you need random numbers at two different places with different
frequence).
Jens
--
Jens Gruschel
http://www.pegtop.net
|
|
| 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
|
|