Every time I remove a with statement from a snippet of code in my mind I can hear a crowd cheering.

Every time I remove a with statement from a snippet of code in my mind I can hear a crowd cheering.

Comments

  1. I wonder  - why isn't "with namespace pollution" a compiler warning?

    ReplyDelete
  2. I had to change something in some old Borland Pascal program today and I had to restraint myself very forcefully not to start removing with statements. Otherwise I would still sit at work trying to figure out all the bugs that would have occurred afterwards. With statements are evil but nested multi-with statements in object oriented code are the devils own invention:

    Procedure TSomeObject.doSomething;
    Begin
    With someProperty, someFieldOfThatProperty do begin
    { ... }
    With someOtherProperty do begin
    SomeField := someProperty.SomeField;
    End;
    End;
    End;

    ReplyDelete
  3. Lars Fosdal Can you imagine if Embarcadero snuck in an "ambiguous reference" hint or warning that was generated whenever an identifier inside a with conflicts with an identically named identifier defined outside the with's scope.

    It wouldn't be very pascalish though. The same ambiguity exists at the unit, class and function level.

    ReplyDelete
  4. With a, b, c do is Pascal's answer to MI. And evil for the same reasons.

    ReplyDelete
  5. Remember:  A Kitten dies each time you use WITH.  Don't do it and save a precious life - and the sanity of the poor schmuck that has to maintain your code. :-)

    ReplyDelete
  6. Kenneth Cochran Multiple Inheritance. Also plagued by name collision issues.

    ReplyDelete
  7. Every time I do it, God saves a kitten.

    ReplyDelete
  8. Bill Meyer  Aah, I see. Every language needs an abuse prone feature and this was Pascal's contribution.

    ReplyDelete
  9. Kenneth Cochran Well, that and FreeAndNil();

    ReplyDelete
  10. Bill Meyer
    Actually interfaces are the answer to MI, and they do rather fine job for hat purpose :-). With serves only evil developers :-D

    ReplyDelete
  11. Jeroen Wiert Pluimers So many entries, just to say "Don't!"  ;)

    ReplyDelete
  12. Bill Meyer every time it is fun to see the reactions.

    ReplyDelete
  13. I did a write up about it years ago on my own blog comparing it with similar features in other languages.

    http://codeelegance.blogspot.com/2011/08/with-statement.html

    ReplyDelete
  14. Kenneth Cochran I had to check, but Wirth kept "with" in Modula 2 and Oberon. However in Oberon, it appears that the "guard" cannot be a list, so with a, b do is not supported.

    ReplyDelete
  15. Maybe they should introduce a closing "without" - that way nested withs maintain order.

    ReplyDelete
  16. If it could not be nested, and did not accept a list, and was supported in the debugger, it would be less offensive. But that is a lot of ifs....

    ReplyDelete
  17. Bill Meyer
    It is more than offensive. The danger is that it allows changes in unrelated code to cause sudden bugs in it. Offensive in this case is probably the understatement of the century :-D

    ReplyDelete
  18. Bill Meyer Only place where 'with' doesn't pose any real danger are standalone functions and procedures that operate on records.

    The second you have class involved on any level, whether you just use 'with' on records in class method or you use with on class instances you are in trouble. It doesn't even have to be nested 'with'.

    ReplyDelete
  19. What do you think of slightly changed syntax:
    with a := Something do
    a.Foo
    ?

    I like that variation - it keeps the space-saving aspect but makes what it's operating on completely clear.

    ReplyDelete
  20. David Millington No. No. No. No. Noooooooo! That encourages people to use `with`. And it should be discouraged.

    See Hallvard's second link.

    The only compiler changes for it should be to mark it deprecated for a few versions, than kill it.

    ReplyDelete
  21. Why not mark only the "anonymous" (for lack of a better term) version as deprecated?

    To be honest though I too don't see it as a necessary keyword any more. I understand back in the day it was useful for saving typing when accessing a lot of fields in a record. These days records can have methods - a better way of accessing their fields, I think :)

    ReplyDelete
  22. David Millington because everytime you would write such a with, you should have written a method that can be unit-tested.

    ReplyDelete
  23. My coworker Martin Wienold always successfully slaps his own hand when he feels the temptation to use with ;)

    ReplyDelete

Post a Comment