Infamous WITH resolution

We all know that the WITH statement available in Delphi. A lot people cry that it’s evil and you should not use it. From my point of view using with is only reasonable for a single line or maybe just few lines of code in begin end block. Why is WITH bad? The main problem is the name space collision and the thing when you have big code sections in WITH block – you can make mistakes just because of the WITH and identifier name match.

There is one of the top voted report in Quality Central about “The infamous WITH resolution“.

Personally I dream about solution for the WITH like following:

...
with Long.Identifier.Name.OtherField as A, Other.Long.Field.Access as B do
  begin
    A.Value := B.Value;
    B.Value := 'Something other';
    ...
  end;

Of course as somehow conflicts with AS operator, but the idea is clear. Maybe it can be some other reserved word..

If such thing is done that will be a new life for the WITH statement.

3 comments

  1. Why you don’t refactor such code in a own method?

    ..
    doThings( Long.Identifier.Name.OtherField, Other.Long.Field.Access);
    ….

    procedure doThings(const a:TFoo; const b: TBar);
    begin
    A.Value := b.Value;
    B.Value := ‘Something
    end;

    Like

Leave a Reply