views:

972

answers:

16

I've heard many programmers, particularly Delphi programmers scorn the use of 'with'.
I thought it made programs run faster (only one reference to parent object) and that it was easier to read the code if used sensibly (less than a dozen lines of code and no nesting).

Here's an example:

procedure TBitmap32.FillRectS(const ARect: TRect; Value: TColor32);
begin
  with ARect do FillRectS(Left, Top, Right, Bottom, Value);
end;

I like using 'with'. What's wrong with me?

+1  A: 

I do not like it because it makes debbuging a hassle. You cannot read the value of a variable or the like by just hovering over it with a mouse.

Ralph Rickenbach
That sounds like a problem with the debugger, not a problem with the language feature.
Greg Hewgill
+2  A: 

This debate happens in Javascipt a lot too.

Basically, that With syntax makes it very hard to tell at a glance which Left/Top/etc property/method you're calling on.You could have a local variable called Left, and a property (it's been a while since I've done delphi, sorry if the name is wrong) called Left, perhaps even a function called Left. Anyone reading the code who isn't super familiar with the ARect structure could be very very lost.

Dan F
+1  A: 

There's nothing wrong with it as long as you keep it simple and avoid ambiguities.

As far as I'm aware, it doesn't speed anything up though - it's purely syntactic sugar.

Blorgbeard
It can speed things up if there are multiple levels of indirection involved. Consider the following: with aControl.parent.parent.parent do...followed by multiple operations. The with statement only evaluates the triple-indirection once and uses that reference multiple times.
Mason Wheeler
How do you know the compiler doesn't do that without the with? I did some basic benchmarking a while ago, and couldn't find any difference at all.
Blorgbeard
+7  A: 

I prefer the VB syntax in this case because here, you need to prefix the members inside the with block with a . to avoid ambiguities:

With obj
    .Left = 10
    .Submit()
End With

But really, there's nothing wrong with with in general.

Konrad Rudolph
Agreed. This is basically the only thing I like better in VB over Delphi.
Mason Wheeler
That wouldn't work with nested "with". (Whether you like those or not.)
Ulrich Gerhardt
@Ulrich: the VB syntax *does* work with nested `with`: `With foo`…`With .bar`….
Konrad Rudolph
@Konrad: Then what does `.Left` in the inner `with` mean - `foo.Left` or `foo.bar.Left`?
Ulrich Gerhardt
@Ulrich: `foo.bar.Left`, of course. Otherwise, nesting `with` would make no sense, wouldn’t it?
Konrad Rudolph
Neither version makes much sense. :-) Inside the first `with`, you can qualify whether `Left` is a member of the "withed" variable or comes from the surrounding scope, by prefixing it with `.` or leaving it off. This distinction is gone if you nest your `with`s one level deeper. If the syntax were consistent `.Left` would mean `foo.Left` and `..Left` would mean `foo.bar.Left` or something like that.
Ulrich Gerhardt
@Ulrich: well, I think that the way VB solves it is an adequate trade-off between generality and usefulness. It’s true that the concept *could* have been generalized further. In fact, I don’t use `With` any more at all. Constructors or builders with fluent interfaces offer much better readability. But I don’t think that’s the point here.
Konrad Rudolph
+16  A: 

One annoyance with using with is that the debugger can't handle it. So it makes debugging more difficult.

A bigger problem is that it is less easy to read the code. Especially if the with statement is a bit longer.

procedure TMyForm.ButtonClick(...)
begin
  with OtherForm do begin
    Left := 10;
    Top := 20;
    CallThisFunction;
  end;
end;

Which Form's CallThisFunction will be called? Self (TMyForm) or OtherForm? You can't know without checking if OtherForm has a CallThisFunction method.

And the biggest problem is that you can make bugs easy without even knowing it. What if both TMyForm and OtherForm have a CallThisFunction, but it's private. You might expect/want the OtherForm.CallThisFunction to be called, but it really is not. The compiler would have warned you if you didn't use the with, but now it doesn't.

Using multiple objects in the with multiplies the problems. See http://blog.marcocantu.com/blog/with_harmful.html

Lars Truijens
+2  A: 

It permits incompetent or evil programmers to write unreadble code. Therefor, only use this feature if you are neither incompetent nor evil.

jedediah
And if you can guarantee that your code will never be read or edited by a programmer less competant than yourself.
Richard A
+1  A: 

What you save in typing, you lose in readability. Many debuggers won't have a clue what you're referring to either so debugging is more difficult. It doesn't make programs run faster.

Consider making the code within your with statement a method of the object that you're refering to.

SoftDeveloper
A: 

At work we give points for removing Withs from an existing Win 32 code base because of the extra effort needed to maintain code that uses them. I have found several bugs in a previous job where a local variable called BusinessComponent was masked by being within a With begin block for an object that a published property BusinessComponent of the same type. The compiler chose to use the published property and the code that meant to use the local variable crashed.

I have seen code like

With a,b,c,d do {except they are much longer names, just shortened here) begin i := xyz;
end;

It can be a real pain trying to locate where xyz comes from. If it was c, I'd much sooner write it as

i := c.xyz;

You think it's pretty trivial to understand this but not in a function that was 800 lines long that used a with right at the start!

+5  A: 

It is not likely that "with" would make the code run faster, it is more likely that the compiler would compile it to the same executable code.

The main reason people don't like "with" is that it can introduce confusion about namespace scope and precedence.

There are cases when this is a real issue, and cases when this is a non-issue (non-issue cases would be as described in the question as "used sensibly").

Because of the possible confusion, some developers choose to refrain from using "with" completely, even in cases where there may not be such confusion. This may seem dogmatic, however it can be argued that as code changes and grows, the use of "with" may remain even after code has been modified to an extent that would make the "with" confusing, and thus it is best not to introduce its use in the first place.

Graza
I once did some very limited testing on using with when optimising some often called code. It made no improvement to run time. If this was an issue, say for an array lookup, then a temporary variable is the solution. The compiler is just so smart, that it's usually best to leave it alone.
Richard A
+2  A: 

We've recently banned it in our Delphi coding stnadards.

The pros were frequently outweighing the cons.

That is bugs were being introduced because of its misuse. These didn't justify the savings in time to write or execute the code.

Yes, using with can led to (mildly) faster code execution.

In the following, foo is only evaluated once:

with foo do
begin
  bar := 1;
  bin := x;
  box := 'abc';
end

But, here it is evaluated three times:

foo.bar := 1;
foo.bin := x;
foo.box := 'abc';
Matt Lacey
Are you *sure* about that? I would be very surprised if the compiler wasn't smart enough to optimize the second case into the first case.
Blorgbeard
Quite sure. Delphi is the primary env. at my day job and I'm intimately familiar with it. Pascal's nature makes optimization easy due to pervasive static typing but the optimizations the compiler performs are by now technologically a few generations behind. This compiler is not as smart as we are.
Mihai Limbășan
If you store foo in a local variable, then you'll be getting the equivalent level of performance as "with".
Barry Kelly
That's perfectly true. The performance hit is there when accessing function-type properties (function call, non-inlined pre-Delphi10) and instance variables (vtable lookup.) If you use a local and take care to place the accesses one after the other, it's as fast as the with clause.
Mihai Limbășan
If Foo is a function, or a property called by a GetFoo method, the compiler MUST execute the function each time, as it may have other side effects (generally bad design, but it does happen).
Gerry
A: 
... run faster ...

Not necessarily - your compiler/interpreter is generally better at optimizing code than you are.

I think it makes me say "yuck!" because it's lazy - when I'm reading code (particularly someone else's) I like to see explicit code. So I'd even write "this.field" instead of "field" in Java.

Flint
A: 

In Delphi, there is no other way of declaring a local variable. Therefore, even the manuals recommend code like this:

WITH MyObject.Create DO BEGIN

My old Delphi manuals (D5 - maybe even D7 pdf's?) clearly state that, using WITH dereferencing makes code faster. I have it on good authority that this wasn't true anymore even in D5's time. The compiler optimizes dereferencing.

Used correctly (assuming CodeGear would finally fix it) it has the potential to make debugging much easier:

By adopting a generic naming convention, you can keep variables like Index, Count, Offset etc. in your watch window and they would display the correct value whenever you enter a WITH block.

As things are now, I need to (MANUALLY!!!) fully dereference every variable I want to watch! Instead of being a potential help, this is a royal pain in the butt.

The old DOS debuggers issued an error message stating the cannot display this variable, or words to that effect. Delphi instead goes to the root, finds a variable of the same name and displays that value. VERY, VERY WRONG!

This is the very minimum that urgently needs to be fixed in Delphi.

Ken Knopfli
There is, of course, a way to declare a local variable in Delphi.
gabr
What in the WORLD are you talking about?!? Local variables are declared in the **var** section above the code block.
Mason Wheeler
-1 as nearly everything in this reply is wrong, in just about every way possible.
Cruachan
I think he meant declaring a local variable inside the begin...end block without going to var block.
idursun
+1  A: 

It's primarily a maintenance issue.

The idea of WITH makes reasonable sense from a language point of view, and the argument that it keeps code, when used sensibly, smaller and clearer has some validity. However the problem is that most commercial code will be maintained by several different people over it's lifetime, and what starts out as a small, easily parsed, construct when written can easily mutate over time into unwieldy large structures where the scope of the WITH is not easily parsed by the maintainer. This naturally tends to produce bugs, and difficult to find ones at that.

For example say we have a small function foo which contains three or four lines of code which have been wrapped inside a WITH block then there is indeed no issue. However a few years later this function may have expanded, under several programmers, into 40 or 50 lines of code still wrapped inside a WITH. This is now brittle, and is ripe for bugs to be introduced, particularly so if the maintainer stars introducing additional embedded WITH blocks.

WITH has no other benefits - code should be parsed exactly the same and run at the same speed (I did some experiments with this in D6 inside tight loops used for 3D rendering and I could find no difference). The inability of the debugger to handle it is also an issue - but one that should have been fixed a while back and would be worth ignoring if there were any benefit. Unfortunately there isn't.

Cruachan
+2  A: 

It would be great if the with statement would be extented the following way:

with x := ARect do
begin
  x.Left := 0;
  x.Rigth := 0;
  ...
end;

You wouldn't need to declare a variable 'x'. It will be created by the compiler. It's quick to write and no confusion, which function is used.

max
A: 

You can combine with statements, so you end up with

with Object1, Object2, Object3 do
begin
  //... Confusing statements here
end

And if you think that the debugger is confused by one with, I don't see how anyone can determine what is going on in the with block

Mmarquee
A: 

For Delphi 2005 is exist hard error in with-do statement - evaluate pointer is lost and repace with pointer up. There have to use a local variable, not object type directly.

ijcro
Can you rephrase your answer? Its very difficult to understand what you are trying to say.
codeelegance