<p>I've heard many programmers, particularly Delphi programmers scorn the use of 'with'.  </p>

<p>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).</p>

<p>Here's an example:</p>

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

<p>I like using <code>with</code>. What's wrong with me?</p>

[{'answer_id': 71432, 'author': 'Ralph M. Rickenbach', 'author_id': 4549416, 'author_profile': 'https://Stackoverflow.com/users/4549416', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 71438, 'author': 'Dan F', 'author_id': 11569, 'author_profile': 'https://Stackoverflow.com/users/11569', 'pm_score': 2, 'selected': False, 'text': "<p>This debate happens in Javascript a lot too.</p>\n\n<p>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.</p>\n"}, {'answer_id': 71447, 'author': 'Blorgbeard', 'author_id': 369, 'author_profile': 'https://Stackoverflow.com/users/369', 'pm_score': 2, 'selected': False, 'text': "<p>There's nothing wrong with it as long as you keep it simple and avoid ambiguities.</p>\n\n<p>As far as I'm aware, it doesn't speed anything up though - it's purely syntactic sugar.</p>\n"}, {'answer_id': 71470, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 4, 'selected': False, 'text': "<p>I prefer the VB syntax in this case because here, you need to prefix the members inside the with block with a <code>.</code> to avoid ambiguities:</p>\n\n<pre><code>With obj\n    .Left = 10\n    .Submit()\nEnd With\n</code></pre>\n\n<p>But really, there's nothing wrong with <code>with</code> in general.</p>\n"}, {'answer_id': 71471, 'author': 'Lars Truijens', 'author_id': 1242, 'author_profile': 'https://Stackoverflow.com/users/1242', 'pm_score': 5, 'selected': False, 'text': '<p>One annoyance with using with is that the debugger can\'t handle it. So it makes debugging more difficult.</p>\n\n<p>A bigger problem is that it is less easy to read the code. Especially if the with statement is a bit longer.</p>\n\n<pre><code>procedure TMyForm.ButtonClick(...)\nbegin\n  with OtherForm do begin\n    Left := 10;\n    Top := 20;\n    CallThisFunction;\n  end;\nend;\n</code></pre>\n\n<p>Which Form\'s CallThisFunction will be called? Self (TMyForm) or OtherForm? You can\'t know without checking if OtherForm has a CallThisFunction method.</p>\n\n<p>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.</p>\n\n<p>Using multiple objects in the with multiplies the problems. See <a href="http://blog.marcocantu.com/blog/with_harmful.html" rel="noreferrer">http://blog.marcocantu.com/blog/with_harmful.html</a></p>\n'}, {'answer_id': 71479, 'author': 'jedediah', 'author_id': 6342, 'author_profile': 'https://Stackoverflow.com/users/6342', 'pm_score': 1, 'selected': False, 'text': '<p>It permits incompetent or evil programmers to write unreadble code. Therefor, only use this feature if you are neither incompetent nor evil.</p>\n'}, {'answer_id': 71494, 'author': 'SoftDeveloper', 'author_id': 11805, 'author_profile': 'https://Stackoverflow.com/users/11805', 'pm_score': 2, 'selected': False, 'text': "<p>What you save in typing, you lose in readability.\nMany debuggers won't have a clue what you're referring to either so debugging is more difficult.\nIt doesn't make programs run faster.</p>\n\n<p>Consider making the code within your with statement a method of the object that you're refering to.</p>\n"}, {'answer_id': 71498, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n\n<p>I have seen code like</p>\n\n<p>With a,b,c,d do  {except they are much longer names, just shortened here)\n  begin\n    i := xyz;<br>\n  end;</p>\n\n<p>It can be a real pain trying to locate where xyz comes from. If it was c, I'd much sooner write it as </p>\n\n<p>i := c.xyz;</p>\n\n<p>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!</p>\n"}, {'answer_id': 71512, 'author': 'Graza', 'author_id': 11820, 'author_profile': 'https://Stackoverflow.com/users/11820', 'pm_score': 3, 'selected': False, 'text': '<p>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.</p>\n\n<p>The main reason people don\'t like "with" is that it can introduce confusion about namespace scope and precedence.</p>\n\n<p>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").</p>\n\n<p>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.</p>\n'}, {'answer_id': 71532, 'author': 'Matt Lacey', 'author_id': 1755, 'author_profile': 'https://Stackoverflow.com/users/1755', 'pm_score': -1, 'selected': False, 'text': "<p>We've recently banned it in our Delphi coding stnadards.  </p>\n\n<p>The pros were frequently outweighing the cons.</p>\n\n<p>That is bugs were being introduced because of its misuse.  These didn't justify the savings in time to write or execute the code.</p>\n\n<p>Yes, using with can led to (mildly) faster code execution.</p>\n\n<p>In the following, foo is only evaluated once:</p>\n\n<pre><code>with foo do\nbegin\n  bar := 1;\n  bin := x;\n  box := 'abc';\nend\n</code></pre>\n\n<p>But, here it is evaluated three times:</p>\n\n<pre><code>foo.bar := 1;\nfoo.bin := x;\nfoo.box := 'abc';\n</code></pre>\n"}, {'answer_id': 71544, 'author': 'Flint', 'author_id': 11877, 'author_profile': 'https://Stackoverflow.com/users/11877', 'pm_score': 1, 'selected': False, 'text': '<blockquote>... run faster ...</blockquote>\n\n<p>Not necessarily - your compiler/interpreter is generally better at optimizing code than you are.</p>\n\n<p>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.</p>\n'}, {'answer_id': 2382602, 'author': 'Cruachan', 'author_id': 7315, 'author_profile': 'https://Stackoverflow.com/users/7315', 'pm_score': 2, 'selected': False, 'text': "<p>It's primarily a maintenance issue.</p>\n\n<p>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.  </p>\n\n<p>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.</p>\n\n<p>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.</p>\n"}, {'answer_id': 2384989, 'author': 'markus_ja', 'author_id': 192292, 'author_profile': 'https://Stackoverflow.com/users/192292', 'pm_score': 4, 'selected': False, 'text': "<p>It would be great if the <code>with</code> statement would be extented the following way:</p>\n\n<pre><code>with x := ARect do\nbegin\n  x.Left := 0;\n  x.Rigth := 0;\n  ...\nend;\n</code></pre>\n\n<p>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.</p>\n"}, {'answer_id': 3126973, 'author': 'mmmm', 'author_id': 85592, 'author_profile': 'https://Stackoverflow.com/users/85592', 'pm_score': 2, 'selected': False, 'text': "<p>You can combine with statements, so you end up with </p>\n\n<pre><code>with Object1, Object2, Object3 do\nbegin\n  //... Confusing statements here\nend\n</code></pre>\n\n<p>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 <code>with</code> block</p>\n"}, {'answer_id': 3342110, 'author': 'ijcro', 'author_id': 403174, 'author_profile': 'https://Stackoverflow.com/users/403174', 'pm_score': -1, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 12267500, 'author': 'Arnaud Bouchez', 'author_id': 458259, 'author_profile': 'https://Stackoverflow.com/users/458259', 'pm_score': 3, 'selected': False, 'text': "<p>In fact:</p>\n\n<pre><code>procedure TBitmap32.FillRectS(const ARect: TRect; Value: TColor32);\nbegin\n  with ARect do FillRectS(Left, Top, Right, Bottom, Value);\nend;\n</code></pre>\n\n<p>and</p>\n\n<pre><code>procedure TBitmap32.FillRectS(const ARect: TRect; Value: TColor32);\nbegin\n  FillRectS(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom, Value);\nend;\n</code></pre>\n\n<p>Will generate exactly the same assembler code.</p>\n\n<p>The performance penalty can exist if the value of the <code>with</code> clause is a function or a method. In this case, if you want to have good maintenance AND good speed, just do what the compiler does behind the scene, i.e. <em>create a temporary variable</em>.</p>\n\n<p>In fact:</p>\n\n<pre><code>with MyRect do\nbegin\n  Left := 0;\n  Right := 0;\nend;\n</code></pre>\n\n<p>is encoded in pseudo-code as such by the compiler:</p>\n\n<pre><code>var aRect: ^TRect;\n\naRect := @MyRect;\naRect^.Left := 0;\naRect^.Right := 0;\n</code></pre>\n\n<p>Then <code>aRect</code> can be just a CPU register, but can also be a true temporary variable on stack. Of course, I use pointers here since <code>TRect</code> is a <code>record</code>. It is more direct for objects, since they already are pointers.</p>\n\n<p>Personally, I used with sometimes in my code, but I almost check every time the asm generated to ensure that it does what it should. Not everyone is able or has the time to do it, so IMHO a <em>local variable</em> is a good alternative to with.</p>\n\n<p>I really do not like such code:</p>\n\n<pre><code>for i := 0 to ObjList.Count-1 do\n  for j := 0 to ObjList[i].NestedList.Count-1 do\n  begin\n    ObjList[i].NestedList[j].Member := 'Toto';\n    ObjList[i].NestedList[j].Count := 10;\n  end;\n</code></pre>\n\n<p>It is still pretty readable with with:</p>\n\n<pre><code>for i := 0 to ObjList.Count-1 do\n  for j := 0 to ObjList[i].NestedList.Count-1 do\n  with ObjList[i].NestedList[j] do\n  begin\n    Member := 'Toto';\n    Count := 10;\n  end;\n</code></pre>\n\n<p>or even</p>\n\n<pre><code>for i := 0 to ObjList.Count-1 do\n  with ObjList[i] do\n  for j := 0 to NestedList.Count-1 do\n  with NestedList[j] do\n  begin\n    Member := 'Toto';\n    Count := 10;\n  end;\n</code></pre>\n\n<p>but if the inner loop is huge, a local variable does make sense:</p>\n\n<pre><code>for i := 0 to ObjList.Count-1 do\nbegin\n  Obj := ObjList[i];\n  for j := 0 to Obj.NestedList.Count-1 do\n  begin\n    Nested := Obj.NestedList[j];\n    Nested.Member := 'Toto';\n    Nested.Count := 10;\n  end;\nend;\n</code></pre>\n\n<p>This code won't be slower than <code>with</code>: compiler does it in fact behind the scene!</p>\n\n<p>By the way, it will allow easier debugging: you can put a breakpoint, then point your mouse on <code>Obj</code> or <code>Nested</code> directly to get the internal values.</p>\n"}]
['https://Stackoverflow.com/questions/71419', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11781/']