BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

calling class methods without instantiation of it

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design
View previous topic :: View next topic  
Author Message
Eugene Goldberg
Guest





PostPosted: Mon Dec 12, 2005 4:19 pm    Post subject: calling class methods without instantiation of it Reply with 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...

Thanx,
Eugene.


Back to top
Joanna Carter [TeamB]
Guest





PostPosted: Mon Dec 12, 2005 4:48 pm    Post subject: Re: calling class methods without instantiation of it Reply with quote



"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





PostPosted: Mon Dec 12, 2005 4:52 pm    Post subject: Re: calling class methods without instantiation of it Reply with quote



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





PostPosted: Mon Dec 12, 2005 5:00 pm    Post subject: Re: calling class methods without instantiation of it Reply with quote

"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





PostPosted: Mon Dec 12, 2005 5:54 pm    Post subject: Re: calling class methods without instantiation of it Reply with quote

Quote:
EEEEuuugghh !!

is it some UK - specific spelling of short form of "Eugene" ? Smile))

Quote:
Do you have the possibility of changing them to class methods ? It could
save someone's sanity in the future Smile

I surely will.

Thanx again.



Back to top
Rudy Velthuis [TeamB]
Guest





PostPosted: Mon Dec 12, 2005 9:42 pm    Post subject: Re: calling class methods without instantiation of it Reply with quote

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





PostPosted: Tue Dec 13, 2005 10:15 pm    Post subject: Re: calling class methods without instantiation of it Reply with quote

"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





PostPosted: Wed Dec 14, 2005 6:52 pm    Post subject: Re: calling class methods without instantiation of it Reply with quote

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






PostPosted: Wed Dec 14, 2005 7:13 pm    Post subject: Re: calling class methods without instantiation of it Reply with quote


"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. Smile )

-Corinna



Back to top
Jens Gruschel
Guest





PostPosted: Wed Dec 14, 2005 7:50 pm    Post subject: Re: calling class methods without instantiation of it Reply with quote

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





PostPosted: Thu Dec 15, 2005 9:59 am    Post subject: Re: calling class methods without instantiation of it Reply with quote

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. Smile Of course, you won't be able to get
your methods outside a class in C#.


Back to top
Guest






PostPosted: Thu Dec 15, 2005 3:22 pm    Post subject: Re: calling class methods without instantiation of it Reply with quote


"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. Smile 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





PostPosted: Thu Dec 15, 2005 6:54 pm    Post subject: Re: calling class methods without instantiation of it Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.