Dot syntax performance question

  • Thread starter Thread starter Steve Peterson
  • Start date Start date
S

Steve Peterson

Hello

In my projects I like to use the "Me." (Me"dot") keyword to refer to any
members or methods (ex: Me.MyVariable = "Bob" or calling a sub by Me.MySub)
in the particular class I'm working with. I also like to use the "MyBase"
keyword to refer to any members or methods in an inherited class in the same
way, if I'm working with that. For me, at least, using these keywords really
helps me clarify my code.

My question is really twofold:

1) Is there any sort of performance hit in using these keywords?
2) Is this "Good" programming practice? (In 99% of code samples I see this
isn't done)

Thanks
Steve
 
Steve Peterson said:
Hello

In my projects I like to use the "Me." (Me"dot") keyword to refer to
any members or methods (ex: Me.MyVariable = "Bob" or calling a sub by
Me.MySub) in the particular class I'm working with. I also like to
use the "MyBase" keyword to refer to any members or methods in an
inherited class in the same way, if I'm working with that. For me, at
least, using these keywords really helps me clarify my code.

My question is really twofold:

1) Is there any sort of performance hit in using these keywords?

No. If you don't use "Me.", it is automatically used - if the usual name
resolution mechanism (from narrowest to widest scope) finds the same member.

In VB6 there was a difference, not in VB.NET anymore.
2) Is this "Good" programming practice? (In 99% of code samples I see
this isn't done)

I also use "Me." to be able to use intellisense.
 
Steve,
1) Is there any sort of performance hit in using these keywords?
As Armin pointed out, there is no performance difference, you could use
ILDASM.EXE to look at the IL code for you routine and you will see they are
the same.
2) Is this "Good" programming practice? (In 99% of code samples I see this
isn't done)
It is "Good" programming practice to include them, however "me" is implied,
so I don't use it very often, I will use it when dealing with virtual
(overridable) functions to indicate at that point I am calling an overriable
method, however I have not decided if I like that convention or not.

In addition to Me & MyBase, don't forget about MyClass to refer to methods
in the current class itself, useful with overridable methods, when you need
to be certain to call the version of the method in your class.

Part of the reason I don't use Me, MyBase, and MyClass a lot is if I change
the design of the class, these keywords may "get in the way" of how the new
design is suppose to work. For example I made a method Overridable, that
previously was NotOverridable, If I used Me I should be fine, however if I
was using MyBase or MyClass it probably won't work as expected...

Also, I do not use a lot of modules, so the chance that the method is
associated with the current class (or base class) is very high.

Hope this helps
Jay
 
Thanks, guys!

Since there is no performance hit, It seems to me that this is one of those
"optional" programming styles. Like I mentioned before - for me, I like the
Me dot syntax. Not only does it make my code (to me at least - and I guess
that's what counts) more readable, it allows me to use the intellisense.
Since I'm a horrible typer - that in itself make it worth it LOL!

Steve
 
It was just explained to me that using the mybase keyword when it's not
necessary incurs a performance hit. I'd like to get other opinions on
this.

I was told that if procedure 'foo' is defined in a base class and is not
overridden or shadowed in the derived class, that using mybase.foo from the
derived class costs more then just using foo in the derived class.

The explanation was something like this:

if you use mybase.foo the clr looks at a pointer on the heap that
references a copy of foo already in the derived object

if you use foo directly, then there is no hop to the heap to look at a
pointer, foo is accessed directly from inside your derived object

What say ye all out there in dotnet land about this? Is it hogwash?

Vinnie
 
Vinnie,
Using ILDASM to look at the IL produced I don't get that impression.

If Test is only defined in the base class "Me.Test()" uses the callvirt IL
instruction. Where as "MyBase.Test()" uses the call IL instruction.

My understanding is that callvirt checks to make sure the "me" pointer is
not null (nothing), however call does not.

According to "The Common Language Infrastructure Annotated Standard" from
Addison Wesley.
- call : is designed to be used when the destination address is fixed at the
time the CIL is generated.
- callvirt : uses the exact type of an object (known only at runtime) to
determine the method to be called.

Which suggests to me suggests the opposite, MyBase.Test knows specifically
which routine is going to be called, where as Me.Test needs to "look it up".

Hope this helps
Jay
 
Back
Top