The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

The use of With


I have a question that has me wondering for some time now.
I got the habit of using the With operator in VB ever since I discovered that it existed.
I always write my code like this:

With myobject
    .Property1 =”SomeValue”
End With

Instead of

However it is rather rare to find examples using this syntax (from MS or otherwise).
Am I in error by doing this? Am I missing something?

Thanks in advance for your input.
António Louro Send private email
Friday, August 11, 2006
One reason I have found that MS doesn't use With.

The people writing that source code are not VB developers.  With is VB specific (I think...).  And folks who aren't familiar with it don't use it (big surprise).

It's the reason you see poor VB code samples at MS.  It's not the "native language" of the person writing the sample.  It could even be a bot that writes the samples based on a certain language sample provided.
Eric D. Burdo Send private email
Friday, August 11, 2006
Use With for a long series of object references used several times. This executes faster than if you repeat the series of objects in each statement.
(from for VB classic)
Friday, August 11, 2006
Try looking at some of the VBA examples in Excel, or recording a macro to do lots of things to one cell (e.g. change the formatting a lot) and look at the code there.  There you will see With being used a lot.
Friday, August 11, 2006
I don't write VB or Delphi code, but I find reading VB and Delphi code that uses WITH around lines containing other function calls and variables can be very hard to figure out -- trying to determine what is a member or global function/property and what goes with the WITH object. Otherwise if it is a tight group of assignments I think it is great. So I would just use the rule that it should only be done when all of the functions and properties referenced inside the WITH apply to that object.
Friday, August 11, 2006
The main goal of WITH was to reduce the amount of typing you had to do to call methods of a class, or access members of a record -- without having to declare a 'holder' variable of the proper type at the top of the routine.

Since C++ and Java now both let you declare a 'local' variable anywhere, the way to do it in those languages is to declare a 'holder' variable, use it, then discard it.
Friday, August 11, 2006
Why doesn't C# have a 'with' statement?

As far as I remember, there is an older text about Turbo-Pascal. There was the problem of local visibility, because a record member would hide a local variable of the same name, since the dot was not necessary.

But searching for "with-statement", there are a lot of texts and comments, e.g.:
Friday, August 11, 2006
Don't know about VB, but in dynamic languages like JavaScript, "with" might slow things down a little. Every time you reference a variable, it needs to look in the "with" scope as well as the normal scope.
Friday, August 11, 2006
I for once, use it and find that it makes reading my code easier. Now, VB 2005 lets you do it on a more complex way: With statetment inside a With statetment  :) Can't say if this has always been like this since I can't test it right now.
That can get pretty messy and error prone. I do understand the reasons why some languages have decided not to implement it but used with moderation and care it does make my life easier.
António Louro Send private email
Friday, August 11, 2006
Withs are okay when you do this:

With Customer
    .Name = "Acme Widgets Inc"
    .Zip = "20193"
    .AccountNumber = "210239342"
End With

But I have seen plenty of examples where With is easily abused. In VB6 you can nest withs, leading to unreadable code like this:

If .FooSet Then
  With .Fields
      DoSomethingWithBar .Bar .Baz

This seems particularly common in spaghetti code where the programmer couldn't be bothered to split a 400 line function into smaller more managable chunks. Can you tell me what classes do .FooSet, .Fields, .Bar and .Baz belong to? IIRC the compiler won't warn of method name clashes in nested withs either - it just takes the innermost one.

That's probably why you don't see withs in examples. Experienced developers have been burned so many times with "With" ambiguity that they've just got used to writing it out in full, to ensure they always get it right.
Ritchie Swann Send private email
Friday, August 11, 2006
Incidentally I read somewhere that it was Joel who introduced  With to VBA (then copied by the VB). I miss it sorely when working with C++ and C#.
JSD Send private email
Friday, August 11, 2006
It's much the same as using variables of local scope vs. global names.  I don't see a problem in nesting With blocks at all.  Where there might be ambiguity you always have the option of qualifying things explicitly.

My understanding is that caching the object reference improves performance even in scripting languages like VBScript, so I'd think Javascript would benefit as well.  Of course there is little point in that caching if you're only using the reference once.
Artad Gobeski
Friday, August 11, 2006
Worrying about the performance if the "with" operator is premature optimization at its finest.

A properly written perl object will let you do something like:

my $object = My::Object->new('stuff')

The return value for all the functions are the object itself, so basically you are doing a long chain of function calls.  I really dont know how I feel about doing stuff like that, but you can do it if you write your code for it!

Maybe you dont see "With" used much because with Visual Studio's auto completion, it really isn't so bad to write My.Really.Long.Prefix.To.My.Object.Function('hello world')?
Cory R. King
Friday, August 11, 2006
"With statement inside a With statement"

I'm pretty sure that's been around since VB6 days.  However, it's best used sparingly (or not at all).  Likewise with With in general; I've seen code where a few hundred lines of varying identation levels had a With scoped over the whole thing, but only actually used here and there.  Yuck.

With should be used to make your code prettier, not uglier.  :)
Kyralessa Send private email
Friday, August 11, 2006
Kyralessa, your correct.  Nested With has been around since at least VB6.

And it should be used to make code easier to read... not just  easier for the lazy programmer.

Before using With (back at a former employer) we would often use a short variable.


Dim f as frmPrefs
set f = frmPrefs

And then just use f to reference the Prefs form in the routine, instead of frmPrefs.  Of course, much of this has changed since the VB6 days.

But by using the short f syntax, we still got the speed of  a cached variable (not quite cached, but you know what I mean) and the easy readability to go along.
Eric D. Burdo Send private email
Monday, August 14, 2006
You shouldn't use With because the maintanence programmer who comes along later and tries to decipher your heavily nested, highly ambiguous spaghetti code might be waiting for you outside the office some dark night. 

I deal with a lot of that code right now.  Readability is not enhanced, nor is maintainability.
Clay Dowling Send private email
Monday, August 14, 2006
That's not due to With; that's due to poor use of With.  All things in moderation, With included.
Kyralessa Send private email
Monday, August 14, 2006
I know it was mentioned near the beginning of the reply chain, but I'll point it out again:

With is semi-significantly faster than repeated calls into the same object's properties, especially when looping is involved.
Wednesday, August 16, 2006
But you'd get the same speedup with a temporary variable for those objects, wouldn't you?
Chris Nahr Send private email
Thursday, August 17, 2006
Sean - Can you prove this with a test program? (I don't have VB6 installed anymore to try). I'd be interested in the results.

I seem to recall Bruce McKinney's seminal "Hardcore Visual Basic" had some timings on language features, but I can't remember if this was one of them.
Ritchie Swann Send private email
Thursday, August 17, 2006

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz