How expensive is object instantication?

S

sam.s.kong

Hi,

I'm doing an ASP project in VBScript.
In VBScript, there's no namespace and no static method (class method).
Thus, what I'm trying to do is to make a clas, define instance methods
in the class, create a global object and call the instance methods.
It will simulate a namespace.
My concern is that what's the overhead.

-----------Example----------

[library.asp]

class MyClass
function F1
....
end function

function F2
....
end function
end class

set G = new MyClass 'This will be always created even if it's not used
at all.

[some_page.asp]
<!--#include file="library.asp"-->
G.F1
G.F2

-------------------------

What do you think?
Is it good or bad?
And why so?

If you have any other suggestions about namespace, share with me
please.

Thanks.

Sam
 
P

Patrice

If you do nothing in the initialize event, it is likely small compared with
DB access etc... Also this is the kind of question that is easily solved by
testing...

Another option would be to use a prefix before your the sub name (i.e.
something like G_F1 or glbF1)
 
S

sam.s.kong

Hi Justin,

Justin said:
You could do it this way instead. It avoids creating the object unless
it is needed, and it guarantees that the class will be instantiated by
the time it is used.

G.F1

Function G()
If IsEmpty([G::Inst]) Then Set [G::Inst] = New MyClass
Set G = [G::Inst]
End Function

Class MyClass
Function F1() : End Function
End Class

Dim [G::Inst]

Excellent!
That way, I can delay the instantication until it's first used.
So, basically you think that wrapping functions in a class is a good
workaround to simulate namespaces?

By the way, I've never seen a notation like [G::Inst].
Is it a valid syntax?

Thanks.

Sam
 
A

Anthony Jones

Justin Piper said:
Hi Justin,

Justin said:
You could do it this way instead. It avoids creating the object unless
it is needed, and it guarantees that the class will be instantiated by
the time it is used.

G.F1

Function G()
If IsEmpty([G::Inst]) Then Set [G::Inst] = New MyClass
Set G = [G::Inst]
End Function

Class MyClass
Function F1() : End Function
End Class

Dim [G::Inst]

Excellent!
That way, I can delay the instantication until it's first used.
So, basically you think that wrapping functions in a class is a good
workaround to simulate namespaces?

The only other way to simulate namespaces I can think of is to adopt a
naming convention that includes the namespace name:

Function [G::F1]() : End Function

Function Main()
Dim F1: Set F1 = GetRef("G::F1")

F1()
End Function

That might be useful if you wanted to break the G namespace into
multiple files.
By the way, I've never seen a notation like [G::Inst].
Is it a valid syntax?

It's a quoted identifier. They're not very useful in general, so I tend
to use them to indicate that one identifier has some relationship to
another (e.g, I probably would have named the class [G::Impl]).

If you had that much complexity that a namespace needed to be split into
seperate files you wouldn't be building your class hiearchy in VBScript.

 
A

Anthony Jones

Hi,

I'm doing an ASP project in VBScript.
In VBScript, there's no namespace and no static method (class method).
Thus, what I'm trying to do is to make a clas, define instance methods
in the class, create a global object and call the instance methods.
It will simulate a namespace.
My concern is that what's the overhead.

-----------Example----------

[library.asp]

class MyClass
function F1
....
end function

function F2
....
end function
end class

set G = new MyClass 'This will be always created even if it's not used
at all.

[some_page.asp]
<!--#include file="library.asp"-->
G.F1
G.F2

-------------------------

What do you think?
Is it good or bad?
And why so?

It's a good idea. I use this approach a lot avoid function name collision
between multiple include files and/or functions in the page.

The only thing you need to be careful of then is that the class names that
you use will not collide but since there are far fewer of them that's fairly
easy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top