VB's With keyword

  • Thread starter Thread starter Timothy V
  • Start date Start date
"With" is considered by many to be poor programming practice because it
obscures the contained code purely for the benefit of reduced typing of one
object name during development (have you ever tried to see what object you're
"With"ing in the middle of a long "With"? - you can't) .

If you want to simulate "With", then you can use abbreviated variable names,
but this has also obscures your code (but it can be applied to more than one
object at the same time, while "With" can't).

Another alternative is to refactor so that what you were "With"ing is a new
method where you then can reduce the variable name length (without making it
obscure).

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter
 
rkc said:
Care to explain why "Good ridence I say"?

It makes the code less readable, it appears as an indented block but it's
not a block, it makes searching in your code difficult and it is actually
slower (it's a myth that it's faster).

Michael
 
The With keyword, like anything else, is a tool that can be used wisely
or unwisely. I think it can increase or decrease readability,
depending on how it's used. Although I don't use it much myself, I do
think that as a shortcut for initializing many properties of an object,
it's very useful and its use results in more readable code than
referencing the same variable over and over.

"it is actually slower (it's a myth that it's faster). "
Can you please point me to a benchmark or article that proves it? Just
an educated guess, but I would speculate that the With keyword compiles
to close to the same, if not identical, IL as multiple object
references.

Thanks,
Jared
 
JaredHite1 said:
The With keyword, like anything else, is a tool that can be used wisely
or unwisely. I think it can increase or decrease readability,
depending on how it's used. Although I don't use it much myself, I do
think that as a shortcut for initializing many properties of an object,
it's very useful and its use results in more readable code than
referencing the same variable over and over.

These cases are very rare and I think it's better just to use the same
variable over and over in the case.
"it is actually slower (it's a myth that it's faster). "
Can you please point me to a benchmark or article that proves it? Just
an educated guess, but I would speculate that the With keyword compiles
to close to the same, if not identical, IL as multiple object
references.

I'm talking about With in vb6 as this was what the OP was talking about.
Every article you find will say that With is faster but this is not true.
I've been through this several times in newsgroups before. Obvious with will
be faster in this situation

A.B.C.DoSomething
A.B.C.DoSomething
A.B.C.DoSomething
 
oops... pushed ctrl-enter...

JaredHite1 said:
The With keyword, like anything else, is a tool that can be used wisely
or unwisely. I think it can increase or decrease readability,
depending on how it's used. Although I don't use it much myself, I do
think that as a shortcut for initializing many properties of an object,
it's very useful and its use results in more readable code than
referencing the same variable over and over.

These cases are very rare and I think it's better just to use the same
variable over and over in the case.
"it is actually slower (it's a myth that it's faster). "
Can you please point me to a benchmark or article that proves it? Just
an educated guess, but I would speculate that the With keyword compiles
to close to the same, if not identical, IL as multiple object
references.

I'm talking about With in vb6 as this was what the OP was talking about.
Every article you find will say that With is faster but this is not true.
I've been through this several times in newsgroups before. Obvious With will
be faster in this situation

A.B.C.DoSomething
A.B.C.DoSomething
A.B.C.DoSomething

With A.B.C
.DoSomething
.DoSomething
.DoSomething
End With

but it's not a valid comparison as the original code should have been

dim o as WhatEver
set o = A.B.C
o.DoSomething
o.DoSomething
o.DoSomething

I can't point you to any code or benchmarks because they will all say with
is faster, but if you try it you'll see otherwise.

Michael
 

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

Similar Threads


Back
Top